Home > Structure > Workbook > Hyperlink Macros

Excel Hyperlink Macros

Debra Dalgleish - Contextures

Quickly add or remove multiple hyperlinks by using a macro. Make a clickable table of contents in an Excel workbook

macro prompts for the hyperlink text

1) Hyperlinks in Excel

In a Microsoft Excel worksheet, you can create a hyperlink in any cell. Then, click that hyperlink to go to the specified location.

  • Macros: This page has hyperlink macros that help you save time, when creating or removing hyperlinks

2) Macro to Remove Hyperlinks

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.

2.1) Selected Cells Only

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

2.2) All Hyperlinks on Sheet

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

3) Create Sheet List With Links (Macro)

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.

hyperlink drag 04

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

4) Extract Hyperlink Address

You can use a macro or a User Defined function (UDF) to extract the address from an Excel hyperlink.

  1. Macro to Extract to Adjacent Cell
  2. Create a Hyperlink UDF

4.1) Extract Address to Adjacent Cell

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.

  • Note:: Hyperlinks that you create with the HYPERLINK function are not recognized as worksheet Hyperlinks. Only hyperlinks that you type or insert are recognized.
Sub ExtractHL_AdjacentCell()
Dim HL As Hyperlink
For Each HL In ActiveSheet.Hyperlinks
    HL.Range.Offset(0, 1).Value = HL.Address
Next
End Sub

4.2) UDF to Extract Hyperlink Address

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.

UDF extracts hyperlink name and URL

4.2.1) Video: Create Hyperlink UDF

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.

  • NOTE: Hyperlinks that you create with the HYPERLINK function are not recognized as worksheet Hyperlinks.

4.2.2) Steps: Create a Hyperlink UDF

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

4.2.3) Use the UDF

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:

  • =HLink(B3)

The formula will return the URL for the hyperlink in cell B3

UDF extracts hyperlink name and URL

  • Note: If multiple cells are selected, the formula will show the hyperlink (if any) from the first cell in the selected range.

5) HYPERLINK Formula Creator

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:

  • Select the cell where you want to create a HYPERLINK formula
  • In the sample file, use the shortcut Ctrl+Shift+H to run the macro.
  • First, the macro prompts for the target cell

macro prompts for the target cell

  • Next, it prompts for the hyperlink text that you want to show.

macro prompts for the hyperlink text

  • Finally, click OK, and the HYPERLINK formula is added to the active cell on the worksheet.

HYPERLINK formula is added to active cell

6) Hide Sheets After Hyperlink

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.

6.1) Workbook Code

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

7) Get the Excel Files ✅

  1. Remove/Extract: To see hyperlink macros to remove and extract hyperlinks, you can download the Hyperlink Macros sample file. The file is in xlsm format, zipped, and contains macros. Enable macros if you want to use the sample code.
  2. Table of Contents: For the Sheet List code, download the Excel Table of Contents file. The file is in xlsm format, and zipped. Enable macros so that you can use the sample code.
  3. Hide Sheets: For the code to hide sheets after a hyperlink is clicked, download the Hide Sheets After Hyperlink Click file. The file is in xlsm format, and zipped. Enable macros so that you can use the sample code.
  4. Hyperlink Formula Macro: This workbook has a macro that inserts a hyperlink formula in the active cell. The macro prompts for the target cell and the hyperlink text to show. Use shortcut Ctrl+Shift+H to run the macro. The file is in xlsm format, and zipped. Enable macros so that you can use the sample code.

Get Monthly Excel Tips!

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!

More Tutorials

HYPERLINK Function

Workbooks, Tips

Workbooks, Backup Tool, Free

Worksheets, Tips

Worksheets, Macros

 

 

 

Last updated: August 18, 2026 4:03 PM