VBA to rehide a worksheet when hyperlink to the main sheet is clicked - Excel 16

dreamsinpixels

New Member
Joined
Sep 16, 2022
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I am looking for some code that will automate the re-hiding of a worksheet when a hyperlink to the main sheet is clicked. Is this even possible? I've attempted several different lines of code but am failing miserably. If there is any help out there it would be greatly appreciated!

Thank you and take good care.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Not enough info about the hyperlink to be specific. Maybe in worksheet_followhyperlink event (where the sheet to be hidden is the displayed text of the link)

Sheets(Target.Name).Visible = True (or false as the case may be)
I think that would require the hyperlink subaddress to be (or be on) the sheet you want to show/hide.
There are other properties of the hyperlink that you might need instead, but as stated, info is sketchy.
 
Upvote 0
I would do it all through VBA, not with a hyperlink. When you click in the cell where the hyperlink would be, it runs the code to hide the sheet and activate the index page.

Put this in the code page for each page where the link is:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("A1"), Target) Is Nothing Then ' change to the cell where the link would be
    ThisWorksheet.Visible = xlSheetHidden 'or xlSheetVeryHidden, depending on how bad you want to hide it.
    Sheets("Sheet1").Activate ' change to your index sheet name.
End If
End Sub

If you already have the hyperlinks installed, you'll need to remove them.
 
Upvote 0
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Range("A1"), Target) Is Nothing Then ' change to the cell where the link would be ThisWorksheet.Visible = xlSheetHidden 'or xlSheetVeryHidden, depending on how bad you want to hide it. Sheets("Sheet1").Activate ' change to your index sheet name. End If End Sub
This is great and it worked once...

What about just hiding the sheet when the primary sheet is selected? I think this might be a more straightforward way to go about it.
 
Upvote 0
What about just hiding the sheet when the primary sheet is selected?
Makes sense. Put this in the code for your primary page. This will hide _all_ the sheets other than the primary page.

VBA Code:
Private Sub Worksheet_Activate()
Dim shts As Worksheet
For Each shts In ActiveWorkbook.Worksheets
    If Not ActiveSheet.Name = shts.Name Then
        shts.Visible = xlSheetVisible
    End If
Next
End Sub
 
Upvote 0
Makes sense. Put this in the code for your primary page. This will hide _all_ the sheets other than the primary page.

VBA Code:
Private Sub Worksheet_Activate()
Dim shts As Worksheet
For Each shts In ActiveWorkbook.Worksheets
    If Not ActiveSheet.Name = shts.Name Then
        shts.Visible = xlSheetVisible
    End If
Next
End Sub
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: hide a ws with either hyperlink or when main sheet is activated
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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