简体   繁体   English

Excel中的VBA宏,用于隐藏所有未选中的工作表

[英]VBA Macro in Excel for hiding all worksheets that are not selected

I've been using the following VBA macro code below to hide all but the active worksheet: 我一直在使用以下VBA宏代码来隐藏除活动工作表以外的所有内容:

    Sub HideWorksheets()
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> ThisWorkbook.ActiveSheet.Name Then
            ws.Visible = xlSheetHidden
        End If
    Next ws
End Sub

Are there any options to extend it so that it will hide all but the selected worksheets? 是否有任何扩展选项,以便它可以隐藏除所选工作表之外的所有工作表?

You need to access the Windows(#).SelectedSheets . 您需要访问Windows(#)。SelectedSheets One way is to hide all except ActiveSheet, then unhide those Selected. 一种方法是隐藏除ActiveSheet之外的所有内容,然后取消隐藏“选定”内容。

Option Explicit

Sub HideWorksheets()
    Dim ws As Worksheet

    Application.ScreenUpdating = False
    ' Hide all except activeone
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> ThisWorkbook.ActiveSheet.Name Then ws.Visible = xlSheetHidden
    Next
    ' Unhide selected worksheets
    For Each ws In ThisWorkbook.Windows(1).SelectedSheets
        ws.Visible = xlSheetVisible
    Next ws
    Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM