将实时超链接更改为文本URL的代码无效

时间:2015-02-01 02:30:10

标签: excel-vba excel-2007 vba excel

我需要将实时超链接更改为文本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

2 个答案:

答案 0 :(得分:2)

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

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

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

请注意,某些类型的超链接可能会保存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

答案 1 :(得分:1)

超链接是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