Solved

Remove all hyperlinks from an Excel worksheet without changing cell formatting

Posted on 2013-05-19
1
682 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now