'hide sheet that not in array

I made array with the name of sheets that I want to select. all the other sheets I want to hide. its need to by dynamic.

list say that my array is Sheets(Array("Sheets1", "sheet2")).Select and I have 4 sheets how I can hide all the others sheet that not in the array ?



Solution 1:[1]

Hide Sheets

  • The Sheets collection includes worksheets, charts, and whatnot.
  • Here is a similar solution for worksheets.
Option Explicit

Sub HideSheets()
    
    ' Do NOT hide:
    Dim Exceptions As Variant: Exceptions = Array("Sheet1", "Sheet2")
    
    Dim sh As Object
    
    For Each sh In ThisWorkbook.Sheets
        If IsError(Application.Match(sh.Name, Exceptions, 0)) Then
            On Error Resume Next ' when trying to hide last visible sheet
                If Not sh.Visible = xlSheetHidden Then
                    sh.Visible = xlSheetHidden
                End If
            On Error GoTo 0
        End If
    Next sh
    
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 VBasic2008