我用列表框调用父表,父表应移动到“索引”表的右侧,然后带有数组表的案例语句(子表)应设置为可见,并且位于父表的右侧。理论上听起来不错,而且我已经学会了远离可见和非可见的工作表激活编码,因为它会导致错误失控。 我在未设置 targetSheetName 变量时收到错误。“对于 targetSheetNames 中的每个 targetSheetName”。
子案例数组()
Dim wsIndex As Worksheet
Dim selectedItem As String
Dim targetSheetNames As Variant
Dim targetSheet As Worksheet
Dim lastMovedSheet As Worksheet
Dim ws As Worksheet
Set ws = ActiveSheet
Set wsIndex = ThisWorkbook.Sheets("Index")
selectedItem = ListBox2.Value
Select Case selectedItem
Case "LXXXXXXB"
targetSheetNames = Array("LxxxxxxB-LxxxxxxT", "LxxxxxxB-LxxxxxxO", "LxxxxxxB-BxxxxxxO")
Case "LXXXXXXT"
targetSheetNames = Array("LxxxxxxT-LxxxxxxO","LxxxxxxV-LxxxxxxO")
Case "LxxxxxxO"
targetSheetNames = Array("LxxxxxxO-LxxxxxxT", "LxxxxxxO-BxxxxxxO")
Case Else
MsgBox "Invalid selection.", vbInformation
Exit Sub
End Select
For Each ws In ThisWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
For Each targetSheetName In targetSheetNames
Set targetSheet = ThisWorkbook.Sheets(targetSheetName)
Debug.Print "targetSheetName: " & targetSheetName
targetSheet.Move After:=lastMovedSheet
Set lastMovedSheet = targetSheet
Next targetSheetName
Worksheets(selectedItem).Activate
End Sub
Sub CaseArray3()
Dim wsIndex As Worksheet
Dim ParentSheetName As String
'Dim ChildArray As Variant
Dim targetSheetNames As Variant
Dim ws As Worksheet
Dim idx As Long, i As Long
Dim selectedItem As String
Set ws = ActiveSheet
Set wsIndex = ThisWorkbook.Sheets("Index")
ParentSheetName = ListBox2.Value
selectedItem = ListBox2.Value ' Assuming ListBox1 is where the selection is made
Select Case selectedItem
Case "NLXXXX61"
targetSheetNames = Array("NLXXXX61-NLXXXXAV", "NLXXXX61-JCXXXXCX")
Case "JCXXXXCX"
targetSheetNames = Array("JCXXXXCX-NLXXXX61", "JCXXXXCX-NLXXXXEP")
Case "NLXXXXEP"
targetSheetNames = Array("NLXXXXEP-LTXXXXTB", "NLXXXX61-JCXXXXCX")
Case Else
MsgBox "Invalid selection.", vbInformation
Exit Sub
End Select
idx = wsIndex.Index
With Worksheets(ParentSheetName)
.Visible = True
.Move After:=Worksheets(idx)
End With
For i = 0 To UBound(targetSheetNames)
idx = idx + 1
With Worksheets(targetSheetNames(i))
.Visible = True
.Move After:=Worksheets(idx)
End With
Next i
End Sub