Auto hide/show worksheets based on cell value?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
831
Office Version
  1. 365
  2. 2019
I've seen some examples of doing this when a cell value matches a worksheet name and only that worksheet is hidden or unhidden, but I haven't quite seem what I'm looking for.

I have 40 worksheets named R1, R2, and so on.

On a worksheet called Setup in Cell B3, the user enters a number between 1 and 40. This corresponds to how many worksheets are visible. If 1 is entered, only R1 is visible and the other 39 are hidden, if 40 is entered, they're all visible..... and all possibilities in between.

Is this possible?

Thanks
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
could try to add this code in the Setup module

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
If Not Intersect(Target, Me.Range("B3")) Is Nothing Then
    For Each ws In ThisWorkbook.Sheets
        If ws.Name > "R" & Target.Value Then
            ws.Visible = False
        Else: ws.Visible = True
        End If
    Next ws
End If
End Sub
 
Upvote 0
could try to add this code in the Setup module

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
If Not Intersect(Target, Me.Range("B3")) Is Nothing Then
    For Each ws In ThisWorkbook.Sheets
        If ws.Name > "R" & Target.Value Then
            ws.Visible = False
        Else: ws.Visible = True
        End If
    Next ws
End If
End Sub

That seems to be hiding every worksheet that starts with a letter > R plus if I enter 10 in B3 for instance, R1 and R10 are visible, but all others are hidden.
 
Upvote 0
fell asleep last night
try this in a copy

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myVal As Integer
Dim ws As Worksheet

Application.ScreenUpdating = False
If Not Intersect(Target, Me.Range("B3")) Is Nothing Then
    For Each ws In Worksheets
        If ws.Name Like "R*" Then
            If Val(Mid(ws.Name, 2, Len(ws.Name) - 1)) > Target.Value Then
                ws.Visible = False
            Else
                ws.Visible = True
            End If
        End If
    Next ws
End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,691
Messages
6,056,756
Members
444,889
Latest member
ibbara

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