Error on "Hyperlink to a Hidden Worksheet" - Podcast #1729

esc5972

New Member
Joined
May 23, 2019
Messages
3
Good Morning,

I have followed step by step the above tutorial but am getting an error. The code that I typed is the following:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
LinkTo = Target.SubAddress
WhereBang = InStr(1, LinkTo, "!")
If WhereBang > 0 Then
MySheet = Left(LinkTo, WhereBang - 1)
Worksheets(MySheet).Visible = True
Worksheets(MySheet).Select
MyAddr = Mid(LinkTo, WhereBang + 1)
Worksheets(MySheet).Range(MyAddr).Select
End If



End Sub

The error that I am getting is on the line where the fonts are in blue. I would really like to have this working since I am working on a big project and require that the sheets are only accessible through a hyperlink and are hidden at all times unless the particular sheet is being used.

Thanking you in advance,

Emanuel (Sam) Camilleri
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to the board. When the code breaks, what is the value of "MySheet"? You can find this hovering over it with the mouse (whilst in break mode), or by typing "?MySheet" in the Immediate Window (VB Editor > View > Immediate Window) then hitting Enter
 
Upvote 0
Another way to accurately test the text string being created, including testing for hidden space characters, is to add the following code line above your blue line
Code:
debug.print "[" & mysheet & "]"

I'd also recommend - though I don't think it will help in this case - ensuring you declare all your variables properly, and using a standardised notation so you can tell what type of variables they are. For example I'd use
Code:
dim str[COLOR=#574123]MySheet as string[/COLOR]
Wherever I see this variable name I automatically know that it's a text string. If I try to use it incorrectly, VB will give me a meaningful error message
 
Upvote 0
Welcome to the board. When the code breaks, what is the value of "MySheet"? You can find this hovering over it with the mouse (whilst in break mode), or by typing "?MySheet" in the Immediate Window (VB Editor > View > Immediate Window) then hitting Enter

I think I just found the answer. I have just renamed a couple of sheets as per example and it actually worked. So what I am going to do is rename all my sheets without having any blank spaces in between.
 
Upvote 0
No need to remove blank spaces in the sheet names. Just change the MySheet = line to:
Code:
MySheet = Replace(Left(LinkTo, WhereBang - 1), "'", "")

Also, instead of the Worksheet_FollowHyperlink in the Menu sheet and the multiple Worksheet_FollowHyperlink in all the other sheets to return to the Menu sheet, you can do everything in a single event handler in the ThisWorkbook module:

Code:
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)

    Dim LinkTo As String, WhereBang As Long, MySheet As String, MyAddr As String
    
    If Sh.Name = "Menu" Then
    
        LinkTo = Target.SubAddress
        WhereBang = InStr(1, LinkTo, "!")
        If WhereBang > 0 Then
            MySheet = Replace(Left(LinkTo, WhereBang - 1), "'", "")
            Worksheets(MySheet).Visible = True
            Worksheets(MySheet).Select
            MyAddr = Mid(LinkTo, WhereBang + 1)
            Worksheets(MySheet).Range(MyAddr).Select
        End If
   
    Else
    
        Target.Parent.Worksheet.Visible = False
        Worksheets("Menu").Range("A1").Select
    
    End If
    
End Sub
And delete the Worksheet_FollowHyperlink code in the sheet modules.
 
Last edited:
Upvote 0
No need to remove blank spaces in the sheet names. Just change the MySheet = line to:
Code:
MySheet = Replace(Left(LinkTo, WhereBang - 1), "'", "")

Also, instead of the Worksheet_FollowHyperlink in the Menu sheet and the multiple Worksheet_FollowHyperlink in all the other sheets to return to the Menu sheet, you can do everything in a single event handler in the ThisWorkbook module:

Code:
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)

    Dim LinkTo As String, WhereBang As Long, MySheet As String, MyAddr As String
    
    If Sh.Name = "Menu" Then
    
        LinkTo = Target.SubAddress
        WhereBang = InStr(1, LinkTo, "!")
        If WhereBang > 0 Then
            MySheet = Replace(Left(LinkTo, WhereBang - 1), "'", "")
            Worksheets(MySheet).Visible = True
            Worksheets(MySheet).Select
            MyAddr = Mid(LinkTo, WhereBang + 1)
            Worksheets(MySheet).Range(MyAddr).Select
        End If
   
    Else
    
        Target.Parent.Worksheet.Visible = False
        Worksheets("Menu").Range("A1").Select
    
    End If
    
End Sub
And delete the Worksheet_FollowHyperlink code in the sheet modules.


Worked like a charm

Thanks for all your help
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
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