VBA macros · Utilities

VBA function to extract the URL from a hyperlink in Excel

A user-defined function, LinkAddress, that returns the web address behind a hyperlinked cell, plus a macro that writes every address next to its link.

Windows + Mac

Data copied from web pages often arrives as clickable text: the cell says Product page, but the address behind it is what you need. Excel has no function that reads a hyperlink's address. This small user-defined function, =LinkAddress(A2), returns it, and a second macro below writes every address on the sheet next to its link in one go.

VBA
Option Explicit

Function LinkAddress(cell As Range) As String
    ' Returns the address behind a hyperlink inserted with Ctrl+K (or pasted from the web)
    Dim c As Range
    Set c = cell.Cells(1, 1)

    If c.Hyperlinks.Count > 0 Then
        With c.Hyperlinks(1)
            LinkAddress = .Address
            ' Links to a place in a workbook also have a SubAddress (sheet and cell)
            If Len(.SubAddress) > 0 Then LinkAddress = LinkAddress & "#" & .SubAddress
        End With
    End If
End Function
Return the address behind a hyperlinked cell

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module and paste the code. A function must live in a normal module, not in a sheet module or ThisWorkbook.
  3. Go back to Excel and type the function into a cell like any built-in function, for example =LinkAddress(A2). It also appears in the function list when you start typing =LinkAddress.
  4. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

A function stored in PERSONAL.XLSB works too, but then every formula has to call it as =PERSONAL.XLSB!LinkAddress(...), and the formula breaks when you send the file to someone else. For functions it is usually better to keep the code inside the workbook that uses it, or to save it as an add-in (.xlam). See User-defined functions (UDFs): your own worksheet functions for more about writing your own functions.

How it works

Every cell has a Hyperlinks collection. For a normal hyperlink it holds one item, and .Address is the target: a web address, a mailto: address or a file path. If the cell has no hyperlink, the count is 0 and the function returns an empty string, so the formula shows nothing instead of an error.

Links that jump to a place inside a workbook keep the target in .SubAddress (for example Sheet2!A1), and their .Address is empty for the same workbook or a file name for another one. The function joins the two with #, which is also how Excel writes such links. cell.Cells(1, 1) makes sure only the first cell is used if someone passes a larger range.

The function is not volatile: it recalculates when you edit the cell it points to, but not when you only change the link through Edit Hyperlink. To refresh it after changing only the link, press Ctrl+Alt+F9 on Windows (a full recalculation), or simply re-enter the formula. More in User-defined functions (UDFs): your own worksheet functions.

Change it

If you need the addresses as fixed values, for example to clean up an import, this macro writes every link's address into the cell to the right of it, on the active sheet:

VBA
Sub WriteLinkAddresses()
    Dim h As Hyperlink
    Dim n As Long
    For Each h In ActiveSheet.Hyperlinks
        If h.Type = msoHyperlinkRange Then        ' links in cells, not on shapes
            h.Range.Offset(0, 1).Value = h.Address
            n = n + 1
        End If
    Next h
    MsgBox n & " address(es) written.", vbInformation
End Sub
Write every link's address next to it

Make sure the column to the right is empty first; the macro overwrites it. After extracting the addresses you can remove the links themselves with VBA macro to remove all hyperlinks in Excel. To get only the domain from a web address, use Split(Split(address, "//")(1), "/")(0) in VBA, or TEXTBEFORE and TEXTAFTER on the sheet in Microsoft 365.

What can go wrong

Watch out. Links created with the HYPERLINK function are not in the Hyperlinks collection, so the function returns an empty string for them. The address is already visible in the formula; read it from c.Formula, or simply refer to the cell that the HYPERLINK formula uses.

Cells with several links (possible only through code) return the first. Links on pictures and shapes have no cell, which is why the macro checks h.Type. Addresses longer than 255 characters are returned complete by VBA, but Excel may truncate them when they are displayed in some older versions.

Tip. Works on Windows and Mac. Save the workbook as .xlsm; in a plain .xlsx the formula shows #NAME? after reopening.

Related: VBA macro to remove all hyperlinks in Excel, HYPERLINK, User-defined functions (UDFs): your own worksheet functions.

More vba macros