Convert Hyperlinks to HTML code in Excel

五迷三道 提交于 2020-03-01 23:11:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33558387/convert-hyperlinks-to-html-code-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!