|
Home > Structure > Workbook > Hyperlink Macros Excel Hyperlink Macros
|

In a Microsoft Excel worksheet, you can create a hyperlink in any cell. Then, click that hyperlink to go to the specified location.
If you have lots of hyperlinks to remove, you can you can use the following macro code. The macro deletes all hyperlinks in the selected cells, and leaves just the cell text.
Copy this code into a regular module in your workbook, then select the cells, and run the delHyperlinks macro.
Sub delHyperlinks() 'posted by Rick Rothstein 'select cells then run this macro Selection.Hyperlinks.Delete End Sub
Copy this code into a regular module in your workbook, then select the sheet where you want to remove macros. Then, run this macro, to remove all the hyperlinks on the active sheet.
Sub RemoveHyperlinksOnActiveSheet() 'posted by Rick Rothstein 'removes all hyperlinks on active sheet Cells.Hyperlinks.Delete End Sub
The following code will create a Table of Contents in an Excel file, with a list of all the visible worksheets, and a hyperlink to cell A1 on each sheet. You can get this code in the Table of Contents sample file.

Tip: On each worksheet, put a hyperlink back to the Table of Contents sheet.
Sub CreateTOC()
Dim wsA As Worksheet
Dim ws As Worksheet
Dim wsTOC As Worksheet
Dim lRow As Long
Dim rngList As Range
Dim lCalc As Long
Dim strTOC As String
Dim strCell As String
lCalc = Application.Calculation
On Error GoTo errHandler
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
strTOC = "TOC"
strCell = "A1"
Set wsA = ActiveSheet
On Error Resume Next
Set wsTOC = Sheets(strTOC)
On Error GoTo errHandler
If wsTOC Is Nothing Then
Set wsTOC = Sheets.Add(Before:=Sheets(1))
wsTOC.Name = strTOC
Else
wsTOC.Cells.Clear
End If
With wsTOC
.Range("B1").Value = "Sheet Name"
lRow = 2
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = xlSheetVisible _
And ws.Name <> strTOC Then
.Cells(lRow, 2).Value = ws.Name
.Hyperlinks.Add _
Anchor:=.Cells(lRow, 2), _
Address:="", _
SubAddress:="'" & ws.Name _
& "'!" & strCell, _
ScreenTip:=ws.Name, _
TextToDisplay:=ws.Name
lRow = lRow + 1
End If
Next ws
Set rngList = .Cells(1, 2).CurrentRegion
rngList.EntireColumn.AutoFit
.Rows(1).Font.Bold = True
End With
Application.ScreenUpdating = True
wsTOC.Activate
wsTOC.Cells(1, 2).Activate
exitHandler:
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = lCalc
Set rngList = Nothing
Set wsTOC = Nothing
Set ws = Nothing
Set wsA = Nothing
Exit Sub
errHandler:
MsgBox "Could not create list"
Resume exitHandler
End Sub
You can use a macro or a User Defined function (UDF) to extract the address from an Excel hyperlink.
You can use a macro to extract the address from each hyperlink on a worksheet, and store it in the cell to the right of each hyperlink cell. Thanks to Fabio Puccinelli for this example.
Sub ExtractHL_AdjacentCell()
Dim HL As Hyperlink
For Each HL In ActiveSheet.Hyperlinks
HL.Range.Offset(0, 1).Value = HL.Address
Next
End Sub
There's no built-in function to extract the URL or email address from a worksheet hyperlink. As a workaround, you can make your own function!
In the video and steps below, I show how you can create your own Excel function -- a User Defined Function (UDF) -- to extract a hyperlink's address.

To see a quick overview of how you can create your own UDF to extract a hyperlink location, you can watch this short video. The written instructions are below the video.
First, copy the following code into a regular module in your workbook.
Function HLink(rng As Range) As String
'extract URL from hyperlink - posted by Rick Rothstein
If rng(1).Hyperlinks.Count Then
HLink = rng.Hyperlinks(1).Address
End If
End Function
After the HLink function is in a workbook, you can use it just like any other Excel function.
In the screenshot below, the following formula is in cell D3:
The formula will return the URL for the hyperlink in cell B3

In the HYPERLINK Formula Creator sample file, there's a macro that helps you insert HYPERLINK function formulas.
After you download the sample file, be sure to enable macros, if you want to see how it works
Then, to create a HYPERLINK formula, follow these steps:



In a large workbook, you can use hyperlinks as a table of contents, to quickly go to a specific sheet. Then, on each worksheet, have another hyperlink, to go back to the table of contents.
To keep things looking tidy, you can use code that hides each sheet, after you click a hyperlink on that sheet. Thanks to Jim Williams for suggesting this technique.
The following code is stored on the ThisWorkbook module.
The code runs automatically when a hyperlink is clicked on any worksheet in that workbook.
Private Sub Workbook_SheetFollowHyperlink _
(ByVal Sh As Object, ByVal Target As Hyperlink)
'shows hidden target sheet and
'hides sheet where hyperlink was clicked
Dim strWs As String
Dim strTgt As String
Dim strRng As String
Dim strMsg As String
Dim lCut As Long
On Error GoTo errHandler
strMsg = "Problem with follow hyperlink code"
Select Case Sh.Name
Case "Instructions", "MyLinks"
GoTo exitHandler
Case Else
strWs = Target.Parent
If ActiveSheet.Name <> strWs Then
'get the target cell/range
strTgt = Target.SubAddress
lCut = InStr(1, strTgt, "!")
strRng = Right(strTgt, Len(strTgt) - lCut)
With Sheets(strWs)
strMsg = "Could not select the target"
.Visible = True
.Activate
.Range(strRng).Activate
End With
End If
strMsg = "Could not hide the sheet"
Sh.Visible = False
End Select
exitHandler:
Exit Sub
errHandler:
MsgBox strMsg
Resume exitHandler
End Sub
Don't miss my monthly Excel newsletter! You'll get quick tips, article links, and a bit of fun. Add your email, and click Sign Up.
Next, when you get my reply, click the Confirm button. I add this step to protect you from spam!
Last updated: August 18, 2026 4:03 PM