Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Changing Portions of Many Hyperlinks.

Changing Portions of Many Hyperlinks

Written by Allen Wyatt (last updated June 3, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


4

Kerstine has a worksheet with many, many different hyperlinks in it. She is wondering if there is a way she can replace just a part of each link. For instance, she might like to change any instance of http://www.mysite.com/ to c:/documents/mycopy/. If there is anything additional in the links, then that part should remain. So, for instance, if the original link is http://www.mysite.com/thispage.html, it would be changed to c:/documents/mycopy/thispage.html.

This can be easily done with a macro. The reason is because the hyperlinks can be examined and changed using regular string functions. The following macro provides a simple way to address the issue.

Sub EditHyperlinks()
    Dim lnkH As Hyperlink
    Dim sOld As String
    Dim sNew As String

    sOld = "http://www.mysite.com"
    sNew = "c:/documents/mycopy/"

    For Each lnkH In ActiveSheet.Hyperlinks
        lnkH.Address = Replace(lnkH.Address, sOld, sNew)
        lnkH.TextToDisplay = Replace(lnkH.TextToDisplay, sOld, sNew)
    Next
End Sub

This routine steps through all the hyperlinks in the current worksheet and makes modifications, if necessary, to each one. Both the hyperlink and the displayed text are changed, as appropriate. All you need to do is make changes to the sOld and sNew strings to specify what you are searching for and what you want to replace it with.

You should note that this macro uses the Replace function, which is built into the later versions of VBA. If you are using an older version that does not include the Replace function (you'll know because you'll get an error when you try to turn the macro) then you will need to create your own Replace function that replaces one portion of a string with another. Such functions have been covered in other issues of ExcelTips.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3358) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Changing Portions of Many Hyperlinks.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Different AutoText Entries in Header and Footer

With lots of AutoText entries defined in your system, you may wonder why you can't see all of the ones you need when ...

Discover More

Repeating Column Information on Each Page

When your table occupies lots of pages, you may want to have information in a particular column repeated on each page. ...

Discover More

Saving Grammar Preferences with a Document

The grammar checking tool in Word can be helpful in developing a finished, polished document. You may want to share your ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (menu)

Jumping to Text in Worksheet from an Index

In putting together a workbook, you may develop a worksheet that acts as an index, to contain links that lead to other ...

Discover More

Copying a Hyperlink to Lots of Worksheets

Copying information from one place to another in a worksheet is easy. Copying hyperlinks may not seem that easy, but you ...

Discover More

Deleting a Hyperlink

Hyperlinks can be helpful in some worksheets but bothersome in others. Here's how to get rid of any hyperlinks you don't ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2023-02-23 11:05:04

Lona

I am getting run-time error 7
out of memory


2019-02-17 08:28:10

Terje

Works well, thanks


2018-10-29 08:12:46

Gustav

you didn't answer the question correctly (replace just a part of each link)!


2018-09-03 11:23:40

Lewis

Hi chap, you might want to amend the above code. You have a typo:

"For Eack lnkH In ActiveSheet.Hyperlinks"

Should be:

"For Each lnkH In ActiveSheet.Hyperlinks"

Apart from that, this worked perfect thanks!


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.