Solved

Remove all hyperlinks from an Excel worksheet without changing cell formatting

Posted on 2013-05-19
1
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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 article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

707 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