Solved

Remove all hyperlinks from an Excel worksheet without changing cell formatting

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel formula 6 23
Copy the formula in excel 8 31
Top 10 with duplicates in Excel Table 4 13
TT Formula Check 9 11
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

747 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

12 Experts available now in Live!

Get 1:1 Help Now