【问题标题】:VBA Excel ComboBox Error 381VBA Excel 组合框错误 381
【发布时间】: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返回的价值吗?
  • 我不能完全确定它,但我隐约记得我曾经遇到过类似的问题,其原因在于代码所在的位置。尝试从标准代码模块运行您的代码。

标签: vba excel


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 2022-12-17
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    相关资源
    最近更新 更多