使用“工作表”选项卡作为展开/折叠一组选项卡的按钮

时间:2015-07-31 06:25:04

标签: excel vba excel-vba

我正在使用以下代码(来自Experts Exchange site):

Private Sub Worksheet_Activate()
Dim sheet As Worksheet
Application.ScreenUpdating = False
If ShowHide.Name = "Show My Guts" Then
    'Make all sheets visible
    For Each sheet In ThisWorkbook.Sheets
        sheet.Visible = xlSheetVisible
    Next sheet
    'Change the sheet name to the "Collapse" name you want
    ShowHide.Name = "Hide My Guts"
    'Pick a sheet to display after the once hidden sheets are expanded
    Sheet4.Activate
Else
    'Hide all sheets except the ones you want to keep visible
    For Each sheet In ThisWorkbook.Sheets
        If (sheet.Name <> Results.Name And sheet.Name <> Run.Name And sheet.Name <> ShowHide.Name) Then
           sheet.Visible = xlSheetVeryHidden
        End If
    Next sheet
    'Change the sheet name to the "Expand" name you want
    ShowHide.Name = "Show My Guts"
    'Pick a sheet to display after the sheets to be hidden are collapsed
    Run.Activate
End If
Application.ScreenUpdating = True
End Sub

我已将“Show My Guts”名称重命名为我的工作表名称,并通过修改“Sheet4.Activate”选项选择了另一张要激活的工作表,但在保存宏时,会弹出一个对话框,说“运行时错误424对象需要“当我在对话框上单击”debug“时,行

If ShowHide.Name = "Show My Guts" Then

突出显示。

我尝试使用代码,没有任何修改,它仍然给我同样的错误。

代码有什么问题?

1 个答案:

答案 0 :(得分:2)

ShowHide是&#34; vba名称&#34;他给了报纸。 &#34;技术名称和#34;之间存在差异。和#34;显示名称&#34;

一个小截图不仅仅是单词: Difference between technical name and display name

所以在VBA中你必须重命名你想要的表格&#34; ShowHide&#34; as&#34; ShowHide&#34;或者您以不同的方式命名,并通过新选择更改VBA代码中的ShowHide

更新:您必须在代码中修改此内容

' ShowHide to rename (or rename your sheet under vba with ShowHide)
If ShowHide.Name = "Show My Guts" Then
    ...
    ' ShowHide to rename (or rename your sheet under vba with ShowHide)
    ShowHide.Name = "Hide My Guts"
Else
    For Each sheet In ThisWorkbook.Sheets
        ' ShowHide to rename (or rename your sheet under vba with ShowHide)
        If (sheet.Name <> Results.Name And sheet.Name <> Run.Name And sheet.Name <> ShowHide.Name) Then
            ...
        End If
    Next sheet
    ' ShowHide to rename (or rename your sheet under vba with ShowHide)
    ShowHide.Name = "Show My Guts"
    ...
End If