Hide/Unhide sheets modification for number of character in a sheet name

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi everyone,

I have the following code that works well for what I need it to do however do need to tweak it a slight bit to view a certain set of sheets. Right now I have sheets PP##, PP## Mod, PP## LD, PP## Pivot, and PP## Override and the ##'s go through 01 - 27 so there's literally 135 sheets plus several others. YTD Daily and YTD Monthly will never be hidden and the drop down menu that selects what sheets are viewed are in YTD Daily. At the moment there's options to select PP01 - PP27, LD and override, so if you select PP03 it will show YTD Daily, YTD Monthly and any PP03 sheet and similarly for any selection. I'd like to create a summary selection where it will show YTD Daily, YTD Monthly and any PP## sheet but not the others (Mod, LD, Pivot or Override). Is there a way to modify this code to base the hide/unhide on the number of characters?

Thanks in advance for your assistance!

VBA Code:
Sub HideShowSheets()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim arr As Variant
    Dim t As String
    Dim i As Integer
    Dim y As Integer
 
    Set wb = ThisWorkbook
 
    If wb.Worksheets.Count = 1 Then
        MsgBox "Workbook only contains one worksheet that cannot be hidden.", vbOKOnly, "Warning!"
        Exit Sub
    End If
 
    t = Worksheets("Lists").Range("BY1")
 
    ReDim arr(2, wb.Worksheets.Count)
 
    For i = 1 To wb.Worksheets.Count
        arr(1, i) = wb.Worksheets(i).Name
        If InStr(1, wb.Worksheets(i).Name, t) > 0 Then arr(2, i) = True
    Next
 
    On Error Resume Next
    For y = 1 To wb.Worksheets.Count
        Set ws = wb.Worksheets(arr(1, y))
        'Show Worksheets that meet the criteria
        If arr(2, y) Then
            If Not ws.Visible Then ws.Visible = True
        End If
        'Hide Worksheets that do not meet the criteria except for "YTD"
        If Not arr(2, y) And Not ws.Name = "YTD Daily" And Not ws.Name = "YTD Monthly" Then
            If ws.Visible Then ws.Visible = False
        End If
    Next
    On Error GoTo 0

    Sheets("YTD Daily").Activate
    ActiveSheet.Range("A1").Select
     
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,214,516
Messages
6,119,978
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top