【发布时间】:2017-07-11 05:32:18
【问题描述】:
我有两个潜艇:
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 请帮忙,因为我找不到导致它的错误
【问题讨论】:
-
你检查过
ws.Range(ws.Cells(2, ws.Range(helperText).Column), ws.Cells(lastRowOfEmployees, ws.Range(helperText).Column)).Value返回的价值吗? -
我不能完全确定它,但我隐约记得我曾经遇到过类似的问题,其原因在于代码所在的位置。尝试从标准代码模块运行您的代码。