I have a column containing hyperlinks and I want to do a programmatic search and replace of the hyperlink address – the address itself, not the text to display. How do I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By modifying the “Address” property of the Hyperlinks element of a range object. Hyperlinks is a collection, so you will pick the first item if your range is a single cell, but it allows you to easily loop through all the links contained in a given range or worksheet.
Here’s an example to change the link of the currently selected cell only (note: it must already have an hyperlink).
Here’s a variation that changes all links in the current worksheet to http://www.google.com, as long as they are not currently pointing to http://www.google.com (not necessary, but just for demonstration’s sake):
You can also add and delete links using the Hyperlinks.Add and .Delete methods.
The TextToDisplay property allows you to change the text, and the Range property allows you to access (or figure out) which cells a given link belongs to.
EDIT: As requested, here’s how to do it on all links in one specific column.
Most often I like to create a few variables to assign (Set) the objects I’m working with, such as the worksheet (calling “Sheet1” here), then the actual column which I will refer to by myColumn. If you get an error message, this allows you to pinpoint where it fails exactly.
Notice how the Hyperlinks collection adjusts itself nicely according to the object you call it from!
I also added a check that shows a message if there are no links in the specified column.