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

Changing Portions of Many Hyperlinks

by Allen Wyatt
(last updated August 8, 2015)

6

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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11081) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel 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

Understanding Decimal Tabs

Word offers a variety of tabs that define different ways to align text. If you need to align numeric values, you'll become ...

Discover More

Calculating Week-Ending Dates

When working with dates, you may need to figure out all the dates on which weeks end in a given year. There are several ...

Discover More

Proper Comparisons for Dates in Merge Fields

How Word merges a data source with a document is controlled by merge fields. In those fields you may want to compare dates to ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Generating Automatic Links to Audio Files

Need to create links to many filenames on your system? Here are a couple of quick ways to get just the connection you need.

Discover More

Tying a Hyperlink to a Specific Cell

Make a hyperlink to a cell in your workbook, edit the structure of that workbook a bit, and you may find that the hyperlink ...

Discover More

Changing Huge Numbers of Hyperlinks

Need to change the various targets of a group of hyperlinks? Getting at the underlying link can seem challenging, but it is ...

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}] 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 7 - 0?

2016-08-24 20:34:27

Jim

I have around 1000 hyperlinks that MSExcel mysteriously changed from ...DocumentsSubfolder directory
to ...AppDataRoamingMicrosoft directory.
I attempted to use the macro you suggested in «Changing Portions of Many Hyperlinks» and also the one in «Changing Huge Numbers of Hyperlinks», of course changing the text/code appropriately, but running these macros on my worksheet changed nothing.
This is also a problem that happens periodically; I really wish Excel didn't do this switcharound.


2016-04-06 14:04:18

Willy Vanhaelen

@Kristina

Try to replace the backslashes with slashes:

sOld = "//safenet-Keystone.global.itcor.net/Keystone_encrypted/Payroll Taxation/US Tax/Tax Scanned Documents"
sNew = "//safenet-Keystone.global.itcor.net/Keystone_encrypted/Payroll Taxation/Tax Scanned Documents"


2016-04-05 09:41:17

Kristina

I have a spreadsheet that has hyperlinks to thousands of pdf files that are saved on a shared network. My manager just changed a bunch of folders on the network which moved the folder that the pdf files are housed in. I need to change the path but whenever I try the macro above, for some reason it doesn't work. This is my first time using a macro so I'm at a loss for where to start with troubleshooting.

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

sOld = "\safenet-Keystone.global.itcor.netKeystone_encryptedPayroll TaxationUS TaxTax Scanned Documents"
sNew = "\safenet-Keystone.global.itcor.netKeystone_encryptedPayroll TaxationTax Scanned Documents"

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

When I run it and check my spreadsheet, the US Tax folder is still in the hyperlink and the link won't open. Please help! I'm at my wits end and don't want to have to manually update these links or lose them entirely.


2015-08-09 13:34:10

Willy Vanhaelen

In this case a macro is unavoidable because FIND & REPLACE acts only on the displayed text and leaves the link addresses as is.


2015-08-09 13:17:02

Joe Wright

Tony,
Find and replace only works in the displayed text for the hyperlink, not the address it links to.


2015-08-08 05:17:14

Tony Davis

Is there any reason why Kerstine should not just use FIND & REPLACE ? Saves writing a macro.

I guess, depending on the size of the worksheet, it may be quicker as the macro zones in on just hyperlinks.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu 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.