选择 'master' 个工作表时自动 hiding/unhiding 个 'sub' 个工作表
Automatically hiding/unhiding 'sub' worksheets when 'master' worksheet is selected
问题
我是 运行 一个宏,它会根据用户输入创建一些新作品sheet;至少一个 'master' 作品sheet,和几个 'sub' 作品sheet。它们的格式类似于 "Group 1 Master"、"Group 1 Sub 1"、"Group 2 Sub 2"、"Group 2 Sub 3" ...... "Group n Master"、"Group n Sub 1" ......
我想找到一种隐藏子 sheet 的方法,直到它们的主人 sheet 是 selected/activated,此时它们应该变得可见,然后当主人sheet 未选中,应再次隐藏它们。
接近
我发现 sources 指示如何通过将类似下面的子内容插入给定作品中来手动实现此目的sheet,但是我的 sheet 是由一个宏,我不想手动检查并将其添加到所有宏中。
Private Sub Worksheet_Activate()
For Each SubSheet in SubSheets
SubSheet.Visible = False
Next SubSheet
End Sub
问题
我如何隐藏所有子 sheet,除了他们的主人被选中?
谢谢
想象一下所有 sheet 都可见时的样子:
那么这是隐藏所有子 sheet 的样子:
这就是它如何与自动 hide/show 子 sheets 一起用于活动主 sheet:
我使用了以下代码:
Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'find master left of selected sheet
Dim ActiveMaster As Object
Dim iSht As Long
For iSht = Sh.Index To 1 Step -1 'loop backwards until a master is found
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") > 0 Then
Set ActiveMaster = ThisWorkbook.Sheets(iSht)
Exit For
End If
Next iSht
If ActiveMaster Is Nothing Then
'no master sheet left of current sheet found
MsgBox "No 'Master' sheet found.", vbCritical
Exit Sub
End If
'find last sub sheet
Dim LastSub As Object
For iSht = ActiveMaster.Index + 1 To ThisWorkbook.Sheets.Count
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") > 0 Then
Set LastSub = ThisWorkbook.Sheets(iSht - 1)
Exit For
End If
Next iSht
'if no last sub sheet was found it is the very last sheet
If LastSub Is Nothing Then
Set LastSub = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
'hide all SUB-sheets left of master sheet and right of LastSub sheet
If ActiveMaster.Index > 1 Then
For iSht = 1 To ThisWorkbook.Sheets.Count
If iSht < ActiveMaster.Index Or iSht > LastSub.Index Then
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") = 0 Then
ThisWorkbook.Sheets(iSht).Visible = xlSheetHidden
End If
Else
ThisWorkbook.Sheets(iSht).Visible = xlSheetVisible
End If
Next iSht
End If
End Sub
Public Sub ShowAllSheets()
Dim iSht As Long
For iSht = 1 To ThisWorkbook.Sheets.Count
ThisWorkbook.Sheets(iSht).Visible = xlSheetVisible
Next iSht
End Sub
Public Sub HideAllSubSheets()
Dim iSht As Long
For iSht = 1 To ThisWorkbook.Sheets.Count
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") = 0 Then
ThisWorkbook.Sheets(iSht).Visible = xlSheetHidden
End If
Next iSht
End Sub
问题
我是 运行 一个宏,它会根据用户输入创建一些新作品sheet;至少一个 'master' 作品sheet,和几个 'sub' 作品sheet。它们的格式类似于 "Group 1 Master"、"Group 1 Sub 1"、"Group 2 Sub 2"、"Group 2 Sub 3" ...... "Group n Master"、"Group n Sub 1" ......
我想找到一种隐藏子 sheet 的方法,直到它们的主人 sheet 是 selected/activated,此时它们应该变得可见,然后当主人sheet 未选中,应再次隐藏它们。
接近
我发现 sources 指示如何通过将类似下面的子内容插入给定作品中来手动实现此目的sheet,但是我的 sheet 是由一个宏,我不想手动检查并将其添加到所有宏中。
Private Sub Worksheet_Activate()
For Each SubSheet in SubSheets
SubSheet.Visible = False
Next SubSheet
End Sub
问题
我如何隐藏所有子 sheet,除了他们的主人被选中?
谢谢
想象一下所有 sheet 都可见时的样子:
那么这是隐藏所有子 sheet 的样子:
这就是它如何与自动 hide/show 子 sheets 一起用于活动主 sheet:
我使用了以下代码:
Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'find master left of selected sheet
Dim ActiveMaster As Object
Dim iSht As Long
For iSht = Sh.Index To 1 Step -1 'loop backwards until a master is found
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") > 0 Then
Set ActiveMaster = ThisWorkbook.Sheets(iSht)
Exit For
End If
Next iSht
If ActiveMaster Is Nothing Then
'no master sheet left of current sheet found
MsgBox "No 'Master' sheet found.", vbCritical
Exit Sub
End If
'find last sub sheet
Dim LastSub As Object
For iSht = ActiveMaster.Index + 1 To ThisWorkbook.Sheets.Count
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") > 0 Then
Set LastSub = ThisWorkbook.Sheets(iSht - 1)
Exit For
End If
Next iSht
'if no last sub sheet was found it is the very last sheet
If LastSub Is Nothing Then
Set LastSub = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
'hide all SUB-sheets left of master sheet and right of LastSub sheet
If ActiveMaster.Index > 1 Then
For iSht = 1 To ThisWorkbook.Sheets.Count
If iSht < ActiveMaster.Index Or iSht > LastSub.Index Then
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") = 0 Then
ThisWorkbook.Sheets(iSht).Visible = xlSheetHidden
End If
Else
ThisWorkbook.Sheets(iSht).Visible = xlSheetVisible
End If
Next iSht
End If
End Sub
Public Sub ShowAllSheets()
Dim iSht As Long
For iSht = 1 To ThisWorkbook.Sheets.Count
ThisWorkbook.Sheets(iSht).Visible = xlSheetVisible
Next iSht
End Sub
Public Sub HideAllSubSheets()
Dim iSht As Long
For iSht = 1 To ThisWorkbook.Sheets.Count
If InStr(1, ThisWorkbook.Sheets(iSht).Name, "Master") = 0 Then
ThisWorkbook.Sheets(iSht).Visible = xlSheetHidden
End If
Next iSht
End Sub