I am trying to use VBA to open hyperlinks from my excel using the following code:
numRow = 1
Do While WorksheetFunction.IsText(Range("E" & numRow))
ActiveSheet.Range("E" & numRow).Hyperlinks(1).Follow
numRow = numRow + 1
Loop
However, I keep getting Runtime Error 9: Subscript out of range
at the point in the code where I follow the hyperlinks.
I'm pretty new to VBA Macro-making (as in-'never done it before'), so help would be appreciated. (And if there's a better way to open a link from each cell in a single column, I'd appreciate learning about that too)
EDIT (To add more Info)
The hyperlink in question has been created using HYPERLINK Worksheet function and the text does not display the link URL. Sample of worksheet data is something like this:
What It Looks Like
Case ------ Link
Case1----- Summary
Case2----- Summary
Case3----- Summary
The Cells showing the text "Summary", however, contain a formula
=HYPERLINK("whateverthebaseurlis/"&[@[Case]]&"/Summary", "Summary")
And this is the link that has to be followed. The link works, it can be followed manually. But I need to do it via macro
Thanks
Probably, you are getting error because you have some cells with text but no link!
Check for link instead of whether or not cell is text:
A cleaner way of getting cells hyperlinks:
Using
Range.Value(xlRangeValueXMLSpreadsheet)
, one can get cell hyperlink in XML. As so, we only have to parse XML.So you can use this function in your code as this:
If you don't need
numRow
, you can just:For
FollowHyperlink
, I suggest below code - you have other options from another answers:TRIED AND TESTED
Assumptions
I am covering 3 scenarios here as shown in the Excel file.
=HYPERLINK("www."&"Google"&".Com","Google")
. This hyperlink has a friendly namewww.Google.com
Normal hyperlink=HYPERLINK("www."&"Google"&".Com")
This hyperlink doesn't have a friendly nameScreenshot:
Logic:
"www."&"Google"&".Com"
from=HYPERLINK("www."&"Google"&".Com","Google")
and then store it as a formula in that cellShellExecute
Code:
If it is throwing the error where you try to open hyperlinks, try and explictly open it using explorer.exe
the reason
Hyperlinks(1).Follow
not working is that is no conventional hyperlink in the cell so it will return out of rangeCheck this post for a similar problem: http://www.mrexcel.com/forum/excel-questions/381291-activating-hyperlinks-via-visual-basic-applications.html