I have a column of hyperlinks in an Excel file and I want to convert them to their respective HTML code:
<a href="http://www.example.com">Link Name</a>
I found ways to extract the link only (as text), but I need the whole HTML code as text to replace the hyperlink in the cell.
I’ve searched and searched but no one needed this answer, I guess. Can someone help?
It is actually a fairly straightforward method to yank the .Address
and optional .SubAddress
from the Hyperlinks collection object. The .TextToDisplay
property is simply the value or text of the cell.
Sub html_anchors()
Dim a As Range, u As String, l As String
Dim sANCHOR As String: sANCHOR = "<a href=""%U%"">%L%</a>"
For Each a In Selection
With a
If CBool(.Hyperlinks.Count) Then
l = .Text
u = .Hyperlinks(1).Address
If Right(u, 1) = Chr(47) Then u = Left(u, Len(u) - 1)
.Hyperlinks(1).Delete
.Value = Replace(Replace(sANCHOR, "%U%", u), "%L%", l)
End If
End With
Next a
End Sub
Select all of the cells you want to process and run the routine. If any cell in your selection does not contain a hyperlink, it will be ignored.