Built Hyperlink does not work in some cases

cuddling101

New Member
Joined
Feb 10, 2009
Messages
34
I have a working subroutine that creates a set of hyperlinks in a 'table of contents'.

Unfortunately the hyperlink that is created is not working, in a few cases.

Context

Example reduced in size for clarity

Workbook with five tabs and the table of contents tab

Workbook worksheet names (names changed for confidentiality)

Jenkins
Douglas
Aberthorn
Mountjoy of Windsor
Higgins_NI

Code:
Private Sub Create_GSUTPF_Index()
    
    Dim wb As Workbook
    Dim ws() As Worksheet
    Dim rng As Range
    Dim rngNow As Range
    Dim rngNowName As String
    Dim rngNowLink As String
    Dim wsCount As Integer
    Dim wsColor() As String
    Dim wsPrime As Worksheet

    Dim I As Long
    
    Set wb = ThisWorkbook
    Set wsPrime = wb.Worksheets("Coverage_Statistics") 'Targets Coverage_Statistics for placement of Table of Contents
    
    Application.ScreenUpdating = False 'Prevents seeing all the flashing as it updates the sheet
    
    wsCount = wb.Names("Main_Families_Count").RefersToRange.Value
        
    Set rng = wsPrime.Range("A1:B" & wsCount)
    ReDim ws(1 To wsCount)
    ReDim wsColor(1 To wsCount)
    
    For I = 1 To wsCount
    
        Set ws(I) = Worksheets(I)
        wsColor(I) = ws(I).Tab.Color
        Set rngNow = rng(I + 1, 1)
        rngNow.Interior.Color = wsColor(I)
[COLOR=#ff0000]         [B]rngNowName = ws(I).Name[/B][/COLOR]
        rngNow = ws(I).Name
        ActiveSheet.Hyperlinks.Add Anchor:=rngNow, Address:="", SubAddress:= _
          rngNowName & "!A2", TextToDisplay:=rngNow.Value
        rngNow.Font.ColorIndex = 0
        
    Next I
    
    Application.ScreenUpdating = True 'Restores screen update
    
End Sub

I tried concatenating CHR(34) either side of rngNowName but that produced an unusable hyperlink.

Problem

The hyperlinks for Jenkins, Douglas, Aberthorn, and Higgins_NI all work fine, as exampled below, for the case of 'Jenkins'. Unfortunately the hyperlink created for 'Mountjoy of Windsor' produces a Reference isn't valid error when I click on it.

Working generated hyperlink

Code:
file:///G:\(Old PC)\Family_History (G)\Jenkins Extended Family History\Genealogical_Service_Users_Tracing_Project_Families.xlsm - Jenkins!A2

Not Working generated hyperlink

Code:
file:///G:\(Old PC)\Family_History (G)\Jenkins Extended Family History\Genealogical_Service_Users_Tracing_Project_Families.xlsm - Mountjoy of Windsor!A2

I feel sure that it is something to do with inserting some single or double quotes somewhere and/or some square brackets, but I am just not sure what.

Please do not suggest changing tab names as that would involve too much work across multiple spreadsheets.

I look forward to receiving some advice, please.

With thanks in anticipation

Philip
Bendigo, Victoria
Australia
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The solution turned out to be the inclusion of a couple of CHR(39) statements, which inserted single quote characters in the correct place.

Code:
        ActiveSheet.Hyperlinks.Add Anchor:=rngNow, Address:="", SubAddress:= _
          Chr(39) & rngNowName & Chr(39) & "!A2", TextToDisplay:=rngNow.Value
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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