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.
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.
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 SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickRemoveAllHyperlinksand click Run. - 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:
Sub RemoveHyperlinksInSelection()
If TypeName(Selection) = "Range" Then Selection.Hyperlinks.Delete
End SubDepending 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
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.
Related: VBA function to extract the URL from a hyperlink in Excel, VBA macro to convert formulas to values in Excel.