1

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 ?

0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.