Hyperlink with VBA: Reference is not valid

bclaredal

Board Regular
Joined
May 3, 2013
Messages
98
I have the following code. It lists the sheets names in a sheet called "Index" and then hyperlinks each sheet name back to their respective sheet. However, when I go to click on the hyperlink it tells me that the reference is not valid. Does anyone know why that error would be happening?

Thanks for you help.

Sub IndexSheetNames()
'This will create an index of all the tab names

Dim SheetNames

SheetNames = Sheets.Count
Sheets.Add
ActiveSheet.Name = "Index"
Sheets("Index").Move After:=Sheets(SheetNames + 1)

For i = 1 To SheetNames

Range("A" & i) = Sheets(i).Name

Next i

Call Hyperlink

End Sub


Sub Hyperlink()
Dim cell As Range, ws As Worksheet

With Sheets("Index") 'Sheet with the hyperlink sheet names
On Error Resume Next

For Each cell In .Range("A1", .Range("A" & Rows.Count).End(xlUp)) 'Loop for each used cell in column A

If cell.Value <> "" Then

Set ws = Nothing
Set ws = Sheets(cell.Value)

If Not ws Is Nothing Then

.Hyperlinks.Add anchor:=cell, Address:="", SubAddress:=cell.Value

End If

End If
Next cell

On Error GoTo 0

End With
End Sub
 

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