Automatically hiding Sheets based on Sheet name

kelsonbaird

New Member
Joined
Oct 14, 2019
Messages
9
So, I think that I see what the issue is but coming up blank with a work around. I THINK the issue is that I am trying to hide the sheet that I am on before moving to the next, but not sure how to make it work.

Basically, I want to automatically hide sheets that start with the word "Vacant" or start with "Aug" and keep any other sheet visible. here's what I have tried. It keeps getting caught up on the second to last line ("wsCurrent.Visible = False"):


Sub Sheet_hide()

Dim wsCurrent As Worksheet

For Each wsCurrent In ActiveWorkbook.Worksheets

If Left(wsCurrent.Name, 6) = "Vacant" Then
GoTo Hide
Else
GoTo Continue
End If

Continue:
If Left(wsCurrent.Name, 3) = "Aug" Then
GoTo Hide
Else
GoTo Skip
End If


Skip:
wsCurrent.Visible = True

Hide:
wsCurrent.Visible = False

Next wsCurrent

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this instead:
VBA Code:
Sub Sheet_hide()
    Dim wsCurrent As Worksheet
    
    For Each wsCurrent In ActiveWorkbook.Worksheets
        If wsCurrent.Name Like "Vacant*" Or wsCurrent.Name Like "Aug*" Then
            wsCurrent.Visible = False
        Else
            wsCurrent.Visible = True
        End If
    Next wsCurrent
End Sub
 
Upvote 0
Solution
thanks that worked, here is what I ended up using:

Sub sheet_hide()

Dim wsCurrent As Worksheet

For Each wsCurrent In ActiveWorkbook.Worksheets

If wsCurrent.Visible = xlSheetHidden Then
GoTo Skip
Else
GoTo Continue
End If


Continue:
If wsCurrent.Name Like "Vacant*" Or wsCurrent.Name Like "Aug*" Then
wsCurrent.Visible = False
Else
wsCurrent.Visible = True
End If
Skip:
Next wsCurrent

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,561
Members
449,038
Latest member
Guest1337

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