VBA: Unhide Sheet depending on cell value

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Gurus,

I am trying to create a macro where in if the cell value in the MAIN sheet matches one with the hidden sheet name, macro will unhide it.

By the way, those Data x Sheetswhere hidden because there's too many.
Example: In the MAIN sheet, if cell Value in A1 is "DATA 1" then it should unhide the tab with sheet name as "DATA 1"

' = = = This one is not working
Sub UnhideSheet()

Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
If sh.Name <> "MAIN" Then
If sh.Range("A1").Value = "DATA 1" Then sh.Visible = True
If sh.Range("A1").Value = "DATA 2" Then sh.Visible = True
If sh.Range("A1").Value = "DATA 3" Then sh.Visible = True
'Add more
'Add more
'Add more

End If
Next sh
End Sub

= = = =

Appreciate the help.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
only based in A1 Value in MAIN Sheet?

if A1 Value DATA 1 , then unhide DATA 1 sheet?

still need to check a2 a3 ?

VBA Code:
Sub TEST()
Dim ws As Worksheet
Set ws = Sheets("MAIN")
On Error Resume Next

shtname = ws.Range("a1").Value
Worksheets(shtname).Visible = True


End Sub
 
Last edited:
Upvote 0
only based in A1 Value in MAIN Sheet?

if A1 Value DATA 1 , then unhide DATA 1 sheet?

still need to check a2 a3 ?

VBA Code:
Sub TEST()
Dim ws As Worksheet
Set ws = Sheets("MAIN")
On Error Resume Next

shtname = ws.Range("a1").Value
Worksheets(shtname).Visible = True


End Sub
All good now. Thanks!
 
Upvote 0
pls try this code

Sub UnhideSheet()
Dim sh As Worksheet
For Each sh In ThisWorkbook.Sheets
If sh.Name <> "MAIN" Then
Select Case sh.Range("A1").Value
Case "DATA 1", "DATA 2", "DATA 3"
sh.Visible = xlSheetVisible
Exit For
' Add more conditions
End Select
End If
Next sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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