VBA Excel ComboBox错误381

时间:2017-07-08 15:24:38

标签: excel vba excel-vba

我有两个潜艇:

1)

Sub InitializeComboBoxes()

Dim lastRowOfPositions As Integer, lastRowOfShifts As Integer

Application.ScreenUpdating = False

' POPULATE POSITIONS COMBOBOX
lastRowOfPositions = Module2.last_row("Menu", "_positions")
ActiveWorkbook.Worksheets("Menu").PositionsComboBox.List = ActiveWorkbook.Worksheets("Menu").Range(Cells(2, Range("_positions").Column), Cells(lastRowOfPositions, Range("_positions").Column)).Value
ActiveWorkbook.Worksheets("Menu").PositionsComboBox.ListIndex = 0

End Sub

2)

Sub InitializeEmployeeComboBox()

Dim lastRowOfEmployees As Integer
Dim sheetName As String
Dim ws As Worksheet

sheetName = ActiveWorkbook.Worksheets("Menu").ShiftsComboBoxRemove.Value
Set ws = ActiveWorkbook.Worksheets(sheetName)

Application.ScreenUpdating = False

Dim helperText As String

helperText = sheetName & "Name"
lastRowOfEmployees = Module2.last_row(sheetName, sheetName & "Name")

ActiveWorkbook.Worksheets("Menu").EmployeesComboBox.List = ws.Range(ws.Cells(2, ws.Range(helperText).Column), ws.Cells(lastRowOfEmployees, ws.Range(helperText).Column)).Value
ActiveWorkbook.Worksheets("Menu").EmployeesComboBox.ListIndex = 0

End Sub

问题在于: 1)ActiveWorkbook.Worksheets("Menu").PositionsComboBox.List = ActiveWorkbook.Worksheets("Menu").Range(Cells(2, Range("_positions").Column), Cells(lastRowOfPositions, Range("_positions").Column)).Value - 这是形式的第一个子,它工作正常,没有问题

2)ActiveWorkbook.Worksheets("Menu").EmployeesComboBox.List = ws.Range(ws.Cells(2, ws.Range(helperText).Column), ws.Cells(lastRowOfEmployees, ws.Range(helperText).Column)).Value - 来自第二个子。

对我来说,它们是同一段代码,就在这里我使用不同表单中的Range,不像在(1)中我使用同一张表中的Range

第一个工作正常,第二个给出错误381 请帮助,因为我找不到导致它的错误

1 个答案:

答案 0 :(得分:0)

当我尝试使用其他工作表中的范围填充ComboBox时,似乎存在问题。我通过创建一个数组,用范围值填充它然后将数组分配给ComboBox

来解决它