VBA to Hide/Unhide a Tab Based on Contents of Another Cell

V L

New Member
Joined
Jul 25, 2024
Messages
15
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello All:

I have a spreadsheet with multiple tabs. Range A2 through A1000 contains 5-digit numbers. These 5-digit numbers correspond to tabs of the same name. In column B, I would like to create a situation where placing an "X" in a cell (let’s say placing an X in B2) would cause the tab referenced in cell A2 to hide. Then again, removing the X from B2 would cause the tab referenced in A2 to unhide. I would like to run this as a macro at will. Any assistance is appreciated...
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
This will do the above...

VBA Code:
Sub HideSheet()

Dim ShtNm As String

For Each i In Range("B2:B1000")
ShtNm = i.Offset(0, -1).Value

If i.Value = "X" Then
    Sheets(ShtNm).Visible = False
Else
    Sheets(ShtNm).Visible = True
End If

Next i

End Sub
 
Upvote 0
This will do the above...

VBA Code:
Sub HideSheet()

Dim ShtNm As String

For Each i In Range("B2:B1000")
ShtNm = i.Offset(0, -1).Value

If i.Value = "X" Then
    Sheets(ShtNm).Visible = False
Else
    Sheets(ShtNm).Visible = True
End If

Next i

End Sub
Thank you for the assistance. Getting a "subscript out of range" error. Any suggestions?
 
Upvote 0
THis is how my test data looked....

1723736077938.png


Is the text in column A 'exactly' the same as the sheet names? For example if I have sheet name 222 in my list but no tab with this name then I get your error message.
 
Upvote 0
THis is how my test data looked....

View attachment 115474

Is the text in column A 'exactly' the same as the sheet names? For example if I have sheet name 222 in my list but no tab with this name then I get your error message.
I checked that. Tabs are named exactly as the sheet names. The code works otherwise...
 
Upvote 0
Can't find another way to get the same error as you are experiencing.

Can you load up an image of your sheet or host the workbook somewhere and I'll take a look.
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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