VBA to link to hidden sheets with spaces in names

Outdoorsman80

New Member
Joined
Oct 4, 2014
Messages
32
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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,632
Office Version
  1. 365
Platform
  1. Windows
Try
VBA Code:
        ShtName = Replace(Left(Target.SubAddress, InStr(1, Target.SubAddress, "!") - 1), "'", "")
 
Solution

Outdoorsman80

New Member
Joined
Oct 4, 2014
Messages
32
Office Version
  1. 365
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,632
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,734
Messages
5,638,052
Members
417,001
Latest member
MSteel

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
Top