我已经编写了以下代码行,用于根据所选的下拉列表项导航到工作表。它第一次工作但我得到运行时错误 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