Solved

Remove all hyperlinks from an Excel worksheet without changing cell formatting

Posted on 2013-05-19
1
700 Views
Last Modified: 2013-06-04
Hello,

Some time ago in another thread, expert (chris_bottomley) provided me with a VBA script to remove all hyperlinks from an Excel worksheet. The code is as follows:

Sub noHyper()
Dim lnk As Hyperlink
Dim addr As Range
Dim delText As Boolean 
    delText = MsgBox("Delete Cell Text as well as link?", vbYesNo, "Hyperlink Delete") = vbYes
    For Each lnk In ActiveSheet.Hyperlinks
        Set addr = ActiveSheet.Cells(lnk.Range.Row, lnk.Range.Column)
        lnk.Delete
        If delText Then addr = ""
    Next
End Sub

Open in new window

The code is great and does just as intended, i.e. removes every hyperlink in a worksheet in one step. However, I just noticed that it also removes all formatting for the cells which previously had hyperlinks.

Can someone provide a modification of the code so that all hyperlinks are removed but all formatting in the affected cells stays as it was?

Thanks
0
Comment
Question by:Steve_Brady
1 Comment
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 500 total points
ID: 39179609
Hi,

As far as I am aware, when a cell contains a hyperlink, the formatting (Style) is automatically changed to the "Hyperlink" Style ("Arial 10 Underline Blue", for example).

When a hyperlink is removed (either manually, or via the Visual Basic for Applications code you are using above), the formatting is automatically returned to the "Normal" Style.

Unless you store the Style of each individual cell prior to adding a hyperlink, then you will not be able to return the cell to the previous Style.

You could, perhaps, do this using the Worksheet_SelectionChange() event & the Worksheet_Change() event.

Store the currently active cell's Style when the Selection Changes & if a hyperlink already exists, & within the Worksheet_Change() event test the cell changed (the Target cell) to see if a Hyperlink is (now) present.

If a Hyperlink has been added, the previous Style should be stored elsewhere (say, in a hidden worksheet), so that when the Hyperlink is removed this Style can be re-applied.

It should be noted that the Worksheet_SelectionChange() & Worksheet_Change() events may be called with a selection of more than one cell, so such an instance should be taken into account.

BFN,

fp.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question