将实时超链接更改为文本 URL 的代码不起作用

Code to Change Live Hyperlinks into Text URLs not working

我需要将实时超链接更改为文本 URL 我有以下代码,但在 运行 之后超链接仍然有效

Internet and networkpaths with hyperlink 未选中

谢谢

Sub RemoveHyperLink()
Dim ws As Worksheet
Dim Rng As Range, Cell As Range
Dim LC As Long, LR As Long

Set ws = ThisWorkbook.Sheets("Updated_UnMatched")
With ws
  LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
  LR = .Cells.Find("*", [a1], , , xlByRows, xlPrevious).Row
  Set Rng = .Range("A2").Resize(LR, LC)
End With

For Each Cell In Rng
  If Cell.Hyperlinks.Count > 0 Then
      Cell.Value = Cell.Hyperlinks.Item(1).Address
  End If
Next Cell
End Sub

超链接是 Rangesheet 的 属性,而不是单个单元格的。

那么这个怎么样

//..your original code for getting the Range Rng

//..then use this For Each loop:
Dim HL as Hyperlink
For Each HL In Rng.Hyperlinks
HL.Delete 'This will effectively replace the cells contents with the URL 
Next

对于某些类型的超链接,覆盖单元格值会删除超链接(例如指向工作簿中位置的链接),而对于其他类型的超链接,则不会。所以需要主动删除超链接

此外,您可以迭代范围或工作的超链接集合sheet,比迭代所有单元格更高效和方便(假设您要处理sheet上的所有超链接)

这是为解决这些问题而重构的代码。此代码适用于任何类型的超链接。

请注意,某些类型的超链接可能包含 SubAddress 属性 中感兴趣的文本。在任何一种情况下,另一个 属性 都是空白的,因此将它们连接起来适用于这两种情况。

Sub demo()
    Dim hl As Hyperlink
    Dim rng As Range
    Dim cl As Range
    Dim txt As String
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Updated_UnMatched")

    For Each hl In ws.Hyperlinks
        Set cl = hl.Parent
        txt = hl.Address & hl.SubAddress
        hl.Delete
        cl.Value = txt
    Next
End Sub