Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get explicit sheet hyperlink from cell VBA excel

Tags:

excel

vba

I am looking for a way to extract the hyperlink of an excel cell with hyperlink to another sheet in the same workbook. Is there a way to do it? In example:

Cell A1: "HPI" (with hyperlink to Sheet HPI)
result -> Cell B1: HPI

I found this formula but it's not working with reference to another sheet.

Function GetURL(cell As range, Optional default_value As Variant)
'Lists the Hyperlink Address for a Given Cell
'If cell does not contain a hyperlink, return default_value
 If (cell.range("A1").Hyperlinks.Count <> 1) Then
      GetURL = default_value
Else
      GetURL = cell.range("A1").Hyperlinks(1).Address
End If
End Function
like image 754
Jordan_b Avatar asked Jul 04 '26 05:07

Jordan_b


1 Answers

A hyperlink within a document uses the .Subaddress property, not the .Address property:

Function GetDestination(Target As Range, Optional default_value As Variant) AS Variant
'Lists the Hyperlink Address or Subaddress for a Given Cell
'If cell does not contain a hyperlink, return default_value
    If (Target.Cells(1, 1).Hyperlinks.Count <> 1) Then
        GetDestination = default_value
    Else
        With Target.Cells(1, 1).Hyperlinks(1)
            If Len(.Address)>0 Then
                GetDestination = .Address
            Else
                GetDestination = .SubAddress
            End If
        End With
    End If
End Function
like image 86
Chronocidal Avatar answered Jul 05 '26 23:07

Chronocidal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!