How to Extract URLs from Hyperlinks in Excel Using VBA

スポンサーリンク
スポンサーリンク

How to Extract Only the URL Address from a Hyperlink?

In Excel, there are situations where you might want to extract only the URL address from a hyperlink inserted in a cell, rather than the link text. This article explains how to extract just the URL address using Excel’s features.

Rather than removing the hyperlink (right-click → Remove Hyperlink), this guide focuses on extracting the URL address.

Method 1: Extracting the URL Address Using a VBA Macro

Since Excel’s standard functions cannot directly retrieve only the URL from a hyperlink, using a VBA macro is a convenient method. With the VBA code provided below, you can extract the URL of a hyperlink from a specified cell and display it in another cell.

We will create a function called GetURL to extract the URL from a hyperlink.

Steps

  1. Click the “Developer” tab and open the VBA editor by clicking “Visual Basic”.
  2. Select “Insert” → “Module” and enter the following code:

VBA Code:

Function GetURL(Cell As Range) As String
    If Cell.Hyperlinks.Count > 0 Then
        GetURL = Cell.Hyperlinks(1).Address
    Else
        GetURL = ""
    End If
End Function

Explanation of the VBA Code

  • Cell.Hyperlinks.Count > 0: Checks if the specified cell contains a hyperlink.
  • Cell.Hyperlinks(1).Address: Retrieves the URL address from the hyperlink.
  • GetURL = “”: Returns an empty string if no hyperlink is found.

How to Use

  1. Close the VBA editor and return to the Excel sheet.
  2. When a cell (e.g., A2) contains a hyperlink, enter the following formula in another cell (e.g., B2):

=GetURL(A2)

This will display only the URL of the hyperlink in cell A2 in cell B2.

Example

In the table below, column A contains hyperlinks, and column B uses the VBA function to display only the URL addresses.

A B
1 Link Text URL Address
2 Google =GetURL(A2)
3 Example =GetURL(A3)

Result

  • Cell B2: The URL “https://www.google.com” from the hyperlink in cell A2 (Google) is displayed.
  • Cell B3: The URL “https://www.example.com” from the hyperlink in cell A3 (Example) is displayed.

Summary

Using a VBA macro is a convenient way to extract only the URL address from a hyperlink in Excel. By creating and using the GetURL function, you can easily extract just the URL portion from any cell containing a hyperlink.