Hide Sheets Based On Criteria

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
251
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
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
251
**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?
 

Watch MrExcel Video

Forum statistics

Threads
1,095,814
Messages
5,446,653
Members
405,413
Latest member
AlainCar

This Week's Hot Topics

Top