1

I have the following code that should open multiple links in excel all at once

Sub OpenLink()
    Dim xHyperlink As Hyperlink
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "Select Area"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    For Each xHyperlink In WorkRng.Hyperlinks
        xHyperlink.Follow
    Next
End Sub

Which, however, it is not working and I cannot understand why.

My links are of the form =HYPERLINK("http://example.com"; "Wording") and they work standalone.

smaa
  • 11
  • 1
  • 2

1 Answers1

0

I believe this is not work as you are using the hyperlink function (instead of right click > hyperlink..)

You can prove that this is the issue by right click assigning a hyperlink to a cell and testing your code there.

Are you getting the link address from another cell? if so you could go straight there and open the link using ActiveWorkbook.FollowHyperlink or you could use a technique like this reading the hyperlink value from a formula:

https://stackoverflow.com/a/42564907/359135

Some working (hopefully) code that extracts the url from the formula and opens it:

Sub OpenLinks()
    Dim xHyperlink As Hyperlink
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "Select Area"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    For Each c In WorkRng.Cells
      Dim sFormula As String
      Dim l As String
      sFormula = c.Formula
      l = Mid(sFormula, WorksheetFunction.Search("""", sFormula) + 1, WorksheetFunction.Search(",", sFormula) - WorksheetFunction.Search("""", sFormula) - 2)
      ActiveWorkbook.FollowHyperlink l
    Next
End Sub

I had to tweak the code from that example to not include the quotation marks when extracting the url from the formula text.