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

Quickly Dumping Array Contents

If you store information within a variable array, at some point in your macro you may want to get rid of that information. ...

Discover More

Counting Precedents and Dependents

Do you need to know how many precedents or dependents there are on a worksheet? You could count them manually, or you could ...

Discover More

Counting Employees in Classes

Excel is very good at counting things, even when those things need to meet specific criteria. This tip shows how you can do a ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Removing Hyperlinks without a Macro

If you have a whole slew of hyperlinks in a worksheet and you want to get rid of them, it's easier than you think. This ...

Discover More

Updating Links in Copied Files

When you copy workbooks that contain links, you may be at a loss as to how to update those links. There are a couple of ways ...

Discover More

Easily Changing Links

If you have linked information in your worksheets, you may want a way you can easily change the targets to which those links ...

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 3 + 8?

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.