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
Solved

Remove all hyperlinks from an Excel worksheet without changing cell formatting

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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 use longer labels with horizontal bar charts instead of the vertical column chart.

856 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