hide & unhide sheets based on cell value

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to hide sheets based on sheets names in column B , if the cell C2 is zero then should hide sheets except MAIN sheet if if it's not then should show the all of sheets.
I have this code , but doesn't do anything !
VBA Code:
Sub VV()
Dim ws As Worksheet
Dim C As Range
Set ws = Sheets("MAIN")

        Application.ScreenUpdating = False
        For Each C In ws.Range("B2", Range("B" & Rows.Count).End(xlUp))
        If ws.Range("C2").Value = 0 Then
            Sheets(C.Value).Visible = False
            Else
            Sheets(C.Value).Visible = True
            End If
        Next C
       
        Application.ScreenUpdating = True
   
End Sub
I hope finding assistance
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Code seems to work for me (provided 'MAIN' is the active sheet when the code is run!). Are you sure that the sheet names in column B of 'MAIN' exactly match the actual sheet names?

Here is my sample workbook structure

1692089093534.png


and this is 'MAIN'

Hasson.xlsm
BC
1
2Sheet20
3Sheet3
4Sheet4
5Sheet5
6Sheet6
7
MAIN


After I run your code:

1692089159049.png


Change C2 value in 'MAIN'

Hasson.xlsm
BC
1
2Sheet23
3Sheet3
4Sheet4
5Sheet5
6Sheet6
7
MAIN


Run your code again:

1692089216515.png


Isn't that what you are trying to achieve?
 
Upvote 0
Hi,
first seem to I make mistake by I put zero in C1.
second the problem when C2 is empty , then the sheets keep hidden !
 
Upvote 0
Give it a try this way

VBA Code:
Sub VV_v2()
  Dim C As Range
  
  With Sheets("MAIN")
    For Each C In .Range("B2", .Range("B" & Rows.Count).End(xlUp))
      Sheets(C.Value).Visible = CStr(.Range("C2").Value) <> "0"
    Next C
  End With
End Sub
 
Upvote 0
Awesome !
can you do when write in C2 will directly run the code without click button, please?
 
Upvote 0
can you do when write in C2 will directly run the code without click button, please?
Sure, code below would go in the 'MAIN' worksheet's module.

1692093744714.png


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim C As Range
  
  If Not Intersect(Target, Range("C2")) Is Nothing Then
    For Each C In Range("B2", Range("B" & Rows.Count).End(xlUp))
      Sheets(C.Value).Visible = CStr(Range("C2").Value) <> "0"
    Next C
  End If
End Sub
 
Upvote 0
Solution
works perfectly !(y)
much appreciated for your help.;)
 
Upvote 0
You're welcome. Thanks for the confirmation.
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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