Consulting

Results 1 to 8 of 8

Thread: Solved: Hyperlinks to sheet with single quote in name

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location

    Solved: Hyperlinks to sheet with single quote in name

    Funny problem when inserting a hyperlink to another cell in the same workbook, but a different sheet

    If the .Subaddress contains spaces, Excel needs single quotes around the WS name. OK

    But if there's already a single quote in the WS name (e.g. User's Errors) , then the hyperlink doesn't work and doesn't take me to the cell and gives me a 'Reference not valid" error message

    Is there a workaround? I'd prefer not to have to disallow single quotes, since otherwise it is an allowed character


    [vba]
    Option Explicit
    Sub test()

    Dim rLink As Range, rError As Range


    Set rLink = Worksheets("Error Log").Range("A1")
    Set rError = Worksheets("User's Errors").Range("C5")


    Worksheets("Error Log").Hyperlinks.Add _
    Anchor:=rLink, Address:="", SubAddress:="'" & rError.Parent.Name & "'!" & rError.Address, _
    TextToDisplay:="This link does not work"



    Set rLink = Worksheets("Error Log").Range("A4")
    Set rError = Worksheets("Users Errors").Range("C5")
    Worksheets("Error Log").Hyperlinks.Add _
    Anchor:=rLink, Address:="", SubAddress:="'" & rError.Parent.Name & "'!" & rError.Address, _
    TextToDisplay:="This link does"
    End Sub
    [/vba]

    Paul
    Attached Files Attached Files

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Thats an odd one!, have you tried giving the workbook name a variable, maybe it will be treated differently like[VBA]Dim MyBook as String
    MyBook = "User's Error"[/VBA]

    To be honest it's bad practice to have illegal filename characters in worksheet names, as you've found out it causes many headaches down the line.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Hi Simon ---

    To be honest it's bad practice to have illegal filename characters in worksheet names, as you've found out it causes many headaches down the line.
    1. Possibly, but a single quote seems to be an acceptable file name character (see attachment)

    2. Doing it as a variable doesn't seem to make any difference

    [vba]
    Option Explicit
    Sub test()
    Dim sLinkAddress As String
    Dim rLink As Range, rError As Range
    Set rLink = Worksheets("Error Log").Range("A1")
    Set rError = Worksheets("User's Errors").Range("C5")
    sLinkAddress = "'" & rError.Parent.Name & "'!" & rError.Address
    Worksheets("Error Log").Hyperlinks.Add _
    Anchor:=rLink, Address:="", SubAddress:="'" & rError.Parent.Name & "'!" & rError.Address, _
    TextToDisplay:="This link does not work"

    Set rLink = Worksheets("Error Log").Range("A4")
    Set rError = Worksheets("Users Errors").Range("C5")
    sLinkAddress = "'" & rError.Parent.Name & "'!" & rError.Address
    Worksheets("Error Log").Hyperlinks.Add _
    Anchor:=rLink, Address:="", SubAddress:="'" & rError.Parent.Name & "'!" & rError.Address, _
    TextToDisplay:="This link does"
    End Sub
    [/vba]


    It seems to be because there is already a pair of single quotes bracketing the WS name with the spaces, so one more confuses it.

    Paul
    Attached Files Attached Files

  4. #4
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    If the tabname has a single quote in it (for example a'b ), you must use two single quotes in the hyperlink:



    [vba]

    Sub Macro1()
    ActiveSheet.Hyperlinks.Add
    Anchor:=Selection, Address:="", SubAddress:= _
    "'a''b'!B9", TextToDisplay:="'a''b'!B9"
    End Sub

    [/vba]

    Have no luck with the hyperlink worksheet function.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    GarysStudent --

    Thanks -- that seems to work very well


    [VBA]
    Option Explicit
    Sub test()
    Dim sLinkAddress As String
    Dim rLink As Range, rError As Range
    Set rLink = Worksheets("Error Log").Range("A1")
    Set rError = Worksheets("User's Errors").Range("C5")
    sLinkAddress = "'" & Replace(rError.Parent.Name, "'", "''") & "'!" & rError.Address

    Worksheets("Error Log").Hyperlinks.Add _
    Anchor:=rLink, Address:="", SubAddress:=sLinkAddress, _
    TextToDisplay:="Now this link DOES work"

    Set rLink = Worksheets("Error Log").Range("A4")
    Set rError = Worksheets("Users Errors").Range("C5")
    sLinkAddress = "'" & Replace(rError.Parent.Name, "'", "''") & "'!" & rError.Address

    Worksheets("Error Log").Hyperlinks.Add _
    Anchor:=rLink, Address:="", SubAddress:=sLinkAddress, _
    TextToDisplay:="This link does"
    End Sub
    [/VBA]

    Paul

  6. #6
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    You are very welcome!

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    If you insert a hyperlink manually you'll will be shown the correct reference (the extra single quote).

    As a worksheet function:

    PHP Code:
    =HYPERLINK("#'user''s errors'!C5";"illustration"
    Last edited by snb; 09-16-2012 at 12:24 PM.

  8. #8
    VBAX Contributor GarysStudent's Avatar
    Joined
    Aug 2012
    Location
    Lakehurst, NJ, USA
    Posts
    127
    Location
    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •