VBA Code for Hiding multiple sheets given a cell value

sophijo97

New Member
Joined
Aug 1, 2018
Messages
4
Hi,
I'm trying to make a spreasheet where if there is a "0" on the first sheet, it will hide a corresponding sheet. For example, if B2 has a "0", then Sheet2 will be hidden and same thing until B18 and Sheet18. I have this code right now that works for one sheet:

Private Sub Worksheet_Change(ByVal Target As Range)
If [B2] = "0" Then
Sheets ("Sheet2").Visible = False
Else
Sheets("Sheet2").Visible = True
End If
End Sub

Is there a way to modify this to apply to multiple sheets and cells rather than just one of each?

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
try

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim NumSheets As Integer


For NumSheets = 1 To 10
    If ActiveSheet.Range("B" & NumSheets).Value = "0" Then
    Sheets("Sheet" & NumSheets).Visible = False
    Else
    Sheets("Sheet" & NumSheets).Visible = True
    End If
Next NumSheets




End Sub
 
Upvote 0
How many sheets do you actually have?
 
Upvote 0
Ah I see so this works perfectly if the sheets are titled Sheet1, Sheet 2, etc. Is there a way to modify this code so the sheets can be named based on the cell next to the reference cell
Example
A B
1 Dogs
2
 
Upvote 0
like this?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim NumSheets As Integer
Dim CountSheets As Integer
Dim SheetID As String

CountSheets = Worksheets.Count
'Name in "B" followed by number
For NumSheets = 1 To CountSheets
    SheetID = ActiveSheet.Range("B" & NumSheets).Value
    If ActiveSheet.Range("A" & NumSheets).Value = "0" Then
       Sheets(SheetID & NumSheets).Visible = False
    Else
       Sheets(SheetID & NumSheets).Visible = True
    End If
Next NumSheets

End Sub
 
Upvote 0
Another option
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 1 Then
      Sheets(Target.Offset(, 1).Value).Visible = Target.Value = 0
   End If
End Sub
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...iding-multiple-sheets-given-a-cell-value.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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