VBA to Hide sheets based on Cell

5h1v

Board Regular
Joined
Oct 11, 2012
Messages
69
I am looking to hide/unhide every sheet in my workbook if the value in D1 is Yes then the sheet should be visible, if D1 is not yes then hide the sheet.

Seems simple enough but I am struggling, any help would be appreciated.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about
VBA Code:
Sub fiveh1v()
   Dim Ws As Worksheet
   
   For Each Ws In wotksheets
      If Ws.Range("D1").Value = "Yes" Then
         Ws.Visible = xlSheetVisible
      Else
         Ws.Visible = xlSheetHidden
      End If
   Next Ws
End Sub
 
Upvote 0
That gives me error 400 and I created a workbook with 5 sheets

Sheet 1 D1 = Yes
Sheet 2 D1 = No
Sheet 3 D1 = No
Sheet 4 D1 = No
Sheet 5 D1 = Yes

and it hides all bar sheet 5, any ideas?
 
Upvote 0
Nevermind I got it working now

VBA Code:
Sub Hide()
    Application.ScreenUpdating = False
    Dim wsSheet As Worksheet
    For Each wsSheet In ActiveWorkbook.Worksheets
        wsSheet.Visible = xlSheetVisible
    Next wsSheet
    Application.ScreenUpdating = True

   Dim Ws As Worksheet
   
   For Each Ws In Worksheets
      If Ws.Range("D2").Value = "Yes" Then
         Ws.Visible = xlSheetVisible
      Else
         Ws.Visible = xlSheetHidden
      End If
   Next Ws
    Application.ScreenUpdating = True
End Sub

This seems to work thanks for the help
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

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