VBA macros · Formatting

VBA macro to remove all hyperlinks in Excel

Remove every hyperlink from all sheets of a workbook, or only from the selection, while keeping the cell text. Includes a count and a confirmation.

Windows + Mac

Text pasted from websites and emails often comes with dozens of hyperlinks, and clicking a cell to edit it opens a browser instead. Excel can remove links from a selection (right-click › Remove Hyperlinks), but not from every sheet at once. This macro removes all hyperlinks from every worksheet of the active workbook after asking for confirmation, and keeps the text in the cells.

VBA
Option Explicit

Sub RemoveAllHyperlinks()
    Dim ws As Worksheet
    Dim n As Long
    Dim skipped As String

    If MsgBox("Remove all hyperlinks from every sheet of this workbook?", _
              vbOKCancel + vbQuestion, "Remove hyperlinks") <> vbOK Then Exit Sub

    Application.ScreenUpdating = False

    For Each ws In ActiveWorkbook.Worksheets
        If ws.ProtectContents Then
            skipped = skipped & vbLf & ws.Name
        Else
            n = n + ws.Hyperlinks.Count
            ws.Hyperlinks.Delete                ' removes the links, keeps the text
        End If
    Next ws

    Application.ScreenUpdating = True

    If Len(skipped) > 0 Then
        MsgBox n & " hyperlink(s) removed. Protected sheets skipped:" & skipped, vbExclamation
    Else
        MsgBox n & " hyperlink(s) removed.", vbInformation
    End If
End Sub
Remove every hyperlink in the active workbook

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. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick RemoveAllHyperlinks and click Run.
  5. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

Every worksheet has a Hyperlinks collection with all the links on it: links in cells and links attached to shapes and pictures. ws.Hyperlinks.Count is read before deleting so the macro can report how many were removed, and ws.Hyperlinks.Delete removes the whole collection in one call, which is much faster than deleting the links one by one.

Deleting a hyperlink does not delete the cell's content: the display text stays in the cell as normal text. Protected sheets refuse the change, so they are skipped and listed. The confirmation at the start uses MsgBox with vbOKCancel; anything except OK stops the macro (see Talking to the user: MsgBox and InputBox).

Screen updating is switched off because removing thousands of links can otherwise redraw the screen many times.

Change it

To remove links only from the selected cells, a single line is enough:

VBA
Sub RemoveHyperlinksInSelection()
    If TypeName(Selection) = "Range" Then Selection.Hyperlinks.Delete
End Sub
Remove hyperlinks from the selection only

Depending on how the cells were formatted, the text may keep its blue, underlined look after the links are gone. To reset it, add ws.UsedRange.Font.Underline = xlUnderlineStyleNone and ws.UsedRange.Font.ColorIndex = xlColorIndexAutomatic after the delete line; note that this also resets any underline or font colour you applied on purpose. To keep links in shapes, loop through the collection instead and delete only those with h.Type = msoHyperlinkRange, as shown in VBA function to extract the URL from a hyperlink in Excel.

To stop Excel from turning typed web addresses into links in the first place, switch off File › Options › Proofing › AutoCorrect Options › AutoFormat As You Type › Internet and network paths with hyperlinks.

What can go wrong

Watch out. If you need the addresses later, extract them first with VBA function to extract the URL from a hyperlink in Excel; once the links are deleted, the targets are gone. A macro cannot be undone with Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data.

Links created with the HYPERLINK function are formulas, not entries in the Hyperlinks collection, so they stay. Convert those cells to values with VBA macro to convert formulas to values in Excel if you want plain text there too.

Tip. Works on Windows and Mac.

Related: VBA function to extract the URL from a hyperlink in Excel, VBA macro to convert formulas to values in Excel.

More vba macros