Excel VBA Get hyperlink address of specific cell

81,116

Solution 1

This should work:

Dim r As Long, h As Hyperlink
For r = 1 To Range("AD1").End(xlDown).Row
    For Each h In ActiveSheet.Hyperlinks
        If Cells(r, "AD").Address = h.Range.Address Then
            Cells(r, "J") = h.Address
        End If
    Next h
Next r

It's a bit confusing because Range.Address is totally different than Hyperlink.Address (which is your URL), declaring your types will help a lot. This is another case where putting "Option Explicit" at the top of modules would help.

Solution 2

Not sure why we make a big deal, the code is very simple

Sub ExtractURL()
    Dim GetURL As String
    For i = 3 To 500
        If IsEmpty(Cells(i, 1)) = False Then
            Sheets("Sheet2").Range("D" & i).Value = 
               Sheets("Sheet2").Range("A" & i).Hyperlinks(1).Address
        End If
    Next i
End Sub
Share:
81,116
user3682866
Author by

user3682866

Updated on February 16, 2021

Comments

  • user3682866
    user3682866 about 3 years

    How do I code Excel VBA to retrieve the url/address of a hyperlink in a specific cell?

    I am working on sheet2 of my workbook and it contains about 300 rows. Each rows have a unique hyperlink at column "AD". What I'm trying to go for is to loop on each blank cells in column "J" and change it's value from blank to the hyperlink URL of it's column "AD" cell. I am currently using this code:

    do while....
        NextToFill = Sheet2.Range("J1").End(xlDown).Offset(1).Address
        On Error Resume Next
        GetAddress = Sheet2.Range("AD" & Sheet2.Range(NextToFill).Row).Hyperlinks(1).Address
        On Error GoTo 0
    loop
    

    Problem with the above code is it always get the address of the first hyperlink because the code is .Hyperlinks(1).Address. Is there anyway to get the hyperlink address by range address like maybe sheet1.range("AD32").Hyperlinks.Address?

  • Jason K.
    Jason K. about 9 years
    In this case lnk is a cell of type range, not a hyperlink.
  • abhinov
    abhinov about 9 years
    No its a hyperlink if you observe closely..Sheet2.Range("AD" & Sheet2.Range(NextToFill).Row).Hyperlinks, a collection of hyperlinks from a given Range