VBA to link to hidden sheets with spaces in names

Outdoorsman80

Board Regular
Joined
Oct 4, 2014
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I have a large Excel file with many work sheets. I want to hide the data sheets and keep only certain dashboards open. I created an Index sheet to link to the hidden ones.

The code in the Index:
VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim ShtName As String
        ShtName = Left(Target.SubAddress, InStr(1, Target.SubAddress, "!") - 1)
        Sheets(ShtName).Visible = xlSheetVisible
        Sheets(ShtName).Select
End Sub

The code in the hidden sheets to re-hide them upon exit:
VBA Code:
Private Sub Worksheet_Deactivate()
    Me.Visible = xlSheetHidden
End Sub

It works fine for the worksheets that don't have spaces or & in the title; but for the ones that do, I get the following error:
Run-time error '9' Subscript out of range

The line that the debug hightlights is:
VBA Code:
Sheets(ShtName).Visible = xlSheetVisible

The sheet linking to is hidden, it's supposed to un-hide it while you're in it.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try
VBA Code:
        ShtName = Replace(Left(Target.SubAddress, InStr(1, Target.SubAddress, "!") - 1), "'", "")
 
Upvote 0
Solution
Try
VBA Code:
        ShtName = Replace(Left(Target.SubAddress, InStr(1, Target.SubAddress, "!") - 1), "'", "")
Thank you, I can confirm it works with spaces and &. I would not have come up with that.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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