Hide Sheets Based On Criteria

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi,

I have a workbook that has sheets called week 1 through to 52 and a sheet called dump.

I need to automatically hide from a workbook_open, every sheet BUT the "Dump" and "Week " & X where X is current week number and then the next 3 weeks.

For some reason, the code I've created doesn't work using OR logic but does when it's without.

Also, how can I use W as an array or variable to store the current week number and next 3 weeks instead of using 4 different variables?

Thanks,

Code:
Sub HideNon4Weeks()

W = "Week " & Format(Date - 238, "ww")
NW = "Week " & Format(Date - 238, "ww") + 1
N2W = "Week " & Format(Date - 238, "ww") + 2
N3W = "Week " & Format(Date - 238, "ww") + 3


Application.ScreenUpdating = False


For Each sh In ThisWorkbook.Sheets


'    sh.Visible = True
    If sh.Name <> "Dump" Or sh.Name <> W Then sh.Visible = False


Next sh


Application.ScreenUpdating = True


End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
**Solved OR logic**

Code:
    If sh.Name = "Dump" Or sh.Name = W Or sh.Name = NW Or sh.Name = N2W Or sh.Name = N3W Then        sh.Visible = True
    Else
        sh.Visible = False
    End If

But is this able to be simplified still?
 
Upvote 0

Forum statistics

Threads
1,213,559
Messages
6,114,302
Members
448,564
Latest member
ED38

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