2

I need to be able to hide all sheets apart from the ones in the array. I have written the code for the array but am now stuck for the rest.

Sub ShowHideWorksheets()
arr = Array("Readme", "Compliance cert", "Cash Balances", "Occupancy Report", "ALPH", "BC", "Bish", 
"GC", "HS", "STB", "WOL", "GroupCo", "OpCos", "RCG_ALL")

For Each Value In arr


Next Value
End Sub

Any help will be appreciated.

2 Answers 2

2

Hide Worksheets

Option Explicit

Sub hideWorksheets()
    
    Dim arr As Variant
    arr = Array("Readme", "Compliance cert", "Cash Balances", _
                "Occupancy Report", "ALPH", "BC", "Bish", "GC", "HS", "STB", _
                "WOL", "GroupCo", "OpCos", "RCG_ALL")
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet
    
    For Each ws In wb.Worksheets
        If IsError(Application.Match(ws.Name, arr, 0)) Then
            ws.Visible = xlSheetHidden
        End If
    Next ws

End Sub

Sub showAllWorksheets()
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet
    
    For Each ws In wb.Worksheets
        If ws.Visible = xlSheetHidden Then ws.Visible = xlSheetVisible
    Next ws

End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

This worked for me

Sub ShowHideWorksheets()
    
    arr = Array("Readme", "Compliance cert", "Cash Balances", "Occupancy Report", "ALPH", "BC", "Bish", "GC", "HS", "STB", "WOL", "GroupCo", "OpCos", "RCG_ALL")
        
    Dim sh As Worksheet, foundSheet As Boolean
    For Each sh In Worksheets
        For Each Value In arr
            If sh.Name = Value Then
                foundSheet = True
                Exit For
            End If
        Next Value
        If Not foundSheet Then
            sh.Visible = xlSheetHidden
        End If
        foundSheet = False
    Next sh
    
End Sub

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.