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.
by Allen Wyatt
(last updated August 16, 2017)
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 Eack 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.
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.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Connect your worksheets with other workbooks or with the world of the Internet. The ability to add hyperlinks makes this ...Discover More
Excel worksheets allow you to include hyperlinks that lead to resources on the Internet. Here's how to activate those ...Discover More
Got a bunch of hyperlinks you need to get rid of? Here's a handy (and simple) macro that can do the task for you.Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.