In Excel VBA, add a hyperlink with the Hyperlinks.Add method, specifying the cell or shape to hold it and the destination address. For example, a macro can turn a cell into a link to a website, another worksheet, or a file; the key details are the anchor, Address, and, for an internal link, SubAddress.
What Excel VBA hyperlinks do
A hyperlink is a clickable object associated with a worksheet cell, a chart, or another supported shape. In a cell, the link can display text such as “Open report” rather than exposing a long URL. Clicking it can open a web address or file, or move to a location inside the workbook. VBA is useful when links need to be created or changed repeatedly, when a list of records needs links generated from data, or when a workbook needs a navigation menu.
Excel’s VBA object model represents links with the Hyperlink object and collections such as a worksheet’s Hyperlinks collection. The Hyperlinks.Add method creates a link. Its commonly used named arguments are:
Anchor: the cell or shape that becomes clickable.Address: an external destination, such as a URL or file path. Use an empty string for an internal workbook destination.SubAddress: a location within a workbook or another destination, when applicable.TextToDisplay: the visible label for a cell hyperlink.ScreenTip: optional text shown when a user points at the link.
These arguments are not interchangeable. A website generally goes in Address; a worksheet-and-cell destination generally goes in SubAddress. Naming the arguments in VBA makes the intended destination clearer and helps avoid confusion about the method’s optional parameters.
Add a hyperlink to a website
In the following example, the worksheet named Links already exists in the workbook containing the macro. The macro creates a link in cell A2. Change the worksheet name, destination, and display label to suit the workbook.
Sub AddWebsiteLink()
Dim ws As Worksheet
' Use a worksheet in the workbook that contains this macro.
Set ws = ThisWorkbook.Worksheets("Links")
' Add a clickable link to cell A2.
ws.Hyperlinks.Add Anchor:=ws.Range("A2"), _
Address:="https://www.example.com", _
TextToDisplay:="Open the website", _
ScreenTip:="Visit the example website"
End Sub
ThisWorkbook means the workbook containing the VBA code, which is often safer than relying on ActiveWorkbook if several workbooks are open. The example uses a reserved example domain rather than pointing readers to a real service. Replace it with the intended URL. If the worksheet does not exist, the line assigning ws will fail, so check the tab name carefully; worksheet names must match, including spaces.
Before running a macro that changes a workbook, save a separate copy. VBA changes cannot be undone with Ctrl+Z, and closing without saving may be the only way to discard unintended changes. On Windows, open the Visual Basic Editor with Alt+F11 or use Developer > Visual Basic. On Mac, use Developer > Visual Basic; the keyboard shortcut can depend on the keyboard and macOS function-key settings.
Link to another sheet or cell
For a destination in the same workbook, leave Address empty and provide a worksheet-and-cell reference in SubAddress. A worksheet name containing spaces is enclosed in single quotation marks within that reference. For example, the next procedure links cell A2 to cell A1 on a sheet named Summary:
Sub AddInternalLink()
Dim ws As Worksheet
' The link is placed on the Links worksheet.
Set ws = ThisWorkbook.Worksheets("Links")
' An empty Address and a SubAddress point inside this workbook.
ws.Hyperlinks.Add Anchor:=ws.Range("A2"), _
Address:="", _
SubAddress:="'Summary'!A1", _
TextToDisplay:="Go to the summary"
End Sub
The destination sheet must exist, and the cell reference must be valid. For a sheet without spaces, the quoted form is still a clear way to write the sheet reference. A link to a named range can use the name as its subaddress, provided that the name is defined in the workbook and is available to the destination. Internal links are handy for contents pages, navigation buttons, and links from a data table to detail sections.
Create links from a list of URLs
A common task is to turn a column of URL text into clickable links. Decide first whether the URL itself should remain visible or whether each cell should show a shorter label. The following example expects URLs in A2:A20 on a worksheet named Links; it uses the URL text as both the destination and displayed text. Blank cells are skipped.
Sub LinkUrlList()
Dim ws As Worksheet
Dim cell As Range
Dim url As String
' Read URL text from the specified range.
Set ws = ThisWorkbook.Worksheets("Links")
For Each cell In ws.Range("A2:A20")
url = Trim$(CStr(cell.Value))
' Skip empty cells; add links to nonempty values.
If Len(url) > 0 Then
ws.Hyperlinks.Add Anchor:=cell, _
Address:=url, _
TextToDisplay:=url
End If
Next cell
End Sub
This straightforward loop assumes the cells contain usable web addresses. It does not validate whether a URL is correctly formed or whether the destination is reachable, and it should not be run repeatedly without considering existing links. If a cell already has a hyperlink, adding another may cause an error or leave an unintended result. Test on a copy, and adapt the procedure to remove or update existing links before adding replacements if that is part of the workflow. For clearer labels, store the label in one column and the destination in another, then use the label as TextToDisplay and the URL column as Address.
Edit, inspect, and remove existing links
When a link already exists, its Address, SubAddress, and TextToDisplay properties can be inspected or changed through its Hyperlink object. For example, if cell A2 contains a hyperlink on the Links sheet, a macro can change its address. Check that the cell actually contains a hyperlink before referring to the first item in its collection; otherwise, there is no item to edit.
Sub UpdateLinkInA2()
Dim ws As Worksheet
' Select the worksheet containing the existing link.
Set ws = ThisWorkbook.Worksheets("Links")
' Change the destination only when a hyperlink is present.
If ws.Range("A2").Hyperlinks.Count > 0 Then
ws.Range("A2").Hyperlinks(1).Address = _
"https://www.example.com/updated"
End If
End Sub
To remove links, Excel offers more than one approach. In the interface, select the relevant cells and use the context-menu option to remove hyperlinks. In VBA, the range’s Hyperlinks.Delete method removes hyperlink objects from the selected range. Be deliberate about the range: deleting links across a whole worksheet when only one column was intended is an easy way to create avoidable cleanup work. Removing a link is different from clearing the cell’s value; inspect the result if the cell’s text or formatting also matters to your workbook.
Choose VBA, a formula, or the ribbon
VBA is not required for every hyperlink. For one or two links, use the Excel interface: select a cell, press Ctrl+K on Windows or Command+K on Mac, enter or select a destination in the Insert Hyperlink dialog, and confirm. You can also find the hyperlink command on the Insert tab. The interface is usually the simplest choice for occasional manual links and does not require users to enable macros.
The HYPERLINK worksheet function is another option when the destination or label should be calculated from cell values. Unlike a VBA-created link, a formula-driven link updates in response to its inputs according to the formula. VBA is more suitable for automation that changes workbook objects, applies rules across many records, or runs as part of a larger macro. Consider maintainability too: a formula is visible in the worksheet, while VBA logic is stored in the workbook’s code and may be unfamiliar to users.
- Choose the ribbon for a small number of links added by a person.
- Choose a worksheet formula when links should be driven by changing cell data.
- Choose VBA when a repeatable process must create, update, or remove many links.
Workbooks containing VBA need a macro-enabled format such as .xlsm to preserve their macros. Saving as a macro-free workbook format can discard the VBA project. A workbook can still contain ordinary hyperlinks without macros, so using VBA to create a link does not mean the finished link itself requires the macro to be run each time it is clicked.
Common problems and how to diagnose them
If a macro stops with a “subscript out of range” error on a worksheet reference, verify that the sheet tab exists and that its name matches the code exactly. If the error occurs at Hyperlinks(1), first check whether the target range has a hyperlink. A broken or unavailable destination is a different issue: VBA can create a link without confirming that a web server, file, or network location will be accessible later.
- The link opens the wrong place: check whether the destination was assigned to
AddressorSubAddressappropriately. For an internal link, verify the sheet name, quotation marks, and cell reference. - The cell shows an unexpected label: set
TextToDisplaydeliberately, or check whether the procedure is using the URL as its visible text. - The macro fails on a repeated run: inspect existing links before adding new ones. The code may need to update or delete an existing link first.
- A file link works on one computer but not another: confirm the file path and access permissions. A local drive letter may not refer to the same location for other users; a shared, accessible path is generally more portable.
- The macro will not run: confirm that the file is macro-enabled and that Excel’s security settings permit the trusted macro to run. Do not enable all macros indiscriminately.
Excel may also display security warnings when a link points to a file or an unfamiliar external location. A hyperlink is a navigation mechanism, not proof that its destination is safe. Users should check unexpected destinations before opening them, especially links supplied by untrusted workbooks or messages.
Platform and macro security considerations
VBA macros run in the desktop versions of Excel for Windows and Mac, but Excel for the web does not run VBA macros. A workbook opened in a browser can still contain clickable hyperlinks, but users need desktop Excel to execute the VBA that creates or changes them. If a process must run in a browser, consider a non-VBA approach, such as creating links in advance or using a supported automation option for the organization’s environment.
On Windows, use trusted files and locations and follow your organization’s macro policy. Do not change security settings simply to run code from an unknown source. Macros can alter or delete workbook data, and Excel does not provide Ctrl+Z undo for macro actions. Keep a backup, test code on sample data, and use specific worksheet and range references instead of relying on whatever sheet happens to be active. This reduces both accidental edits and confusion when multiple workbooks are open.
Frequently Asked Questions
How do I add a hyperlink in Excel VBA?
Call the worksheet’s Hyperlinks.Add method. Set Anchor to the cell or shape, provide the destination in Address, and use TextToDisplay if the cell should show a label.
How do I make a VBA hyperlink to another sheet?
Use an empty Address and put the destination in SubAddress, for example 'Summary'!A1 for cell A1 on a sheet named Summary. Confirm that the destination sheet exists.
Can VBA hyperlink to a file?
Yes. Supply the file’s path as the external Address. The file must be available to the person clicking the link, and local paths may not work for other users or computers.
How do I remove a hyperlink without deleting the cell text?
Remove the hyperlink object rather than clearing the cell contents. You can use Excel’s remove-hyperlink command or the range’s Hyperlinks.Delete method in VBA, then check that the remaining text and formatting meet your needs.
Does VBA hyperlink code work in Excel for the web?
No. Excel for the web can display and follow existing hyperlinks, but it cannot run VBA macros. Use desktop Excel to execute the macro.
Why does Ctrl+Z not undo a hyperlink macro?
Excel’s undo history does not reverse VBA macro actions. Save a copy before running code that edits links or other workbook content, and test the macro on a disposable copy first.
Related
- How to Merge Cells in Excel: Steps and Safe Options
- Merge Day, Month, and Year in Excel
- Convert Text to Numbers in Excel in Bulk
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: Count Visible Rows After AutoFilterTo count rows still visible after an AutoFilter, loop through the filter’s data rows and count those whose…
- VBA Range Is Nothing? Meaning, Checks, and Common FixesIn Excel VBA, a Range is Nothing when an object variable does not currently refer to a range…
- Excel VBA Copy and Paste Cells: Examples and OptionsTo copy a cell with Excel VBA, use the source range’s Copy method and specify a destination: sourceCell.Copy…
- Excel VBA: How to Hide a SheetTo hide a worksheet with VBA, set its Visible property to xlSheetHidden, for example: ThisWorkbook.Worksheets("Report").Visible = xlSheetHidden. Use…
- Excel VBA Not Equal: Operators, Examples, and FixesIn Excel VBA, use the <> operator to test whether two values are not equal. For example, If…
- Excel VBA Array Maximum Size: Limits and Practical TipsThere is no single, guaranteed maximum number of elements for an Excel VBA array: its usable size depends…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.