我根据所选的下拉列表项编写了以下代码行,用于导航到工作表。它第一次工作但我得到运行时错误9 - 下标超出范围错误,当我尝试下次时。我在行中出错了 “ActiveWorkbook.Sheets(cboDependentList.Value).Activate”
以下是代码行:
'Populate dependent combo box with appropriate list items
'according to selection in cboCategoryList.
Sub test()
Dim rng As Range
Dim ws As Worksheet
Dim str As String
Set ws = Worksheets("Lists")
str = cboCategoryList.Value
Me.cboDependentList.Clear
On Error Resume Next
For Each rng In ws.Range(str)
Me.cboDependentList.AddItem rng.Value
Next rng
End Sub
Sub cboDependentList_Change()
ActiveWorkbook.Sheets(cboDependentList.Value).Activate
End Sub
Private Sub Worksheet_Activate()
'Populate combo box with inventory categories.
Dim rng As Range
Dim ws As Worksheet
Set ws = Worksheets("Lists")
Me.cboCategoryList.Clear
For Each rng In ws.Range("Category")
Me.cboCategoryList.AddItem rng.Value
Next rng
End Sub
答案 0 :(得分:0)
有时,excel的调试器不会突出显示实际触发错误的代码行,而是突出显示其中一个调用者(调用堆栈中的更高位置)。
在这种情况下,activate方法可能不直接导致运行时错误9 - 下标超出范围错误,但它在子例程Worksheet_Activate中的效果可能。我建议你在调用Activate的行上设置一个断点,然后在调试模式下逐步执行代码。如果这不起作用,则可能必须在子例程Worksheet_Activate的开头设置断点。