从单元格中删除超链接也会删除格式。
Selection.Hyperlinks.Delete
是否有一种方法可以保留格式,还是我需要在删除超链接后重新应用它?
Range.ClearHyperlinks
替代
Range.Hyperlinks.Delete
前者清除超链接并保留格式。参考链接:https://msdn.microsoft.com/zh-cn/library/office/ff194741.aspx
我知道这有点奇怪...不确定你是否会接受。试试这个:
Selection.Hyperlinks(1).Address = ""
这基本上就像没有超链接一样。唯一的区别是你的光标会变成手形,而不是默认的加号,但你仍然可以像点击其他单元格一样点击它。
如果您使用合并单元格,上述解决方案可能会出现问题。以下方法可以解决该问题。
data = Selection.value
Selection.Value = "" 'this removes the hyperlink
Selection.Font.Underline = xlUnderlineStyleNone 'remove the hyperlink underlining
With ActiveCell.Font 'replace hyperlink blue with black
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.Value = data
For Each cll In Selection
cll_val = cll.Value
cll.ClearContents
cll.Value = cll_val
With cll.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.Underline = xlUnderlineStyleNone
End With
Next
我无法想出如何防止格式被删除。我想到的解决方法是在删除超链接之前复制单元格,然后在删除超链接后将格式粘贴回单元格:
' copy cell to remote location to "remember" formatting
ActiveCell.Copy Sheets("Templates").Range("a1")
' remove hyperlink
ActiveCell.Hyperlinks.Delete
' reapply formatting
Sheets("Templates").Range("A1").Copy
ActiveCell.PasteSpecial Paste:=xlPasteFormats
ActiveCell.Style = "Normal"
Dim sStyleName as String
sStyleName = ActiveCell.Style 'Capture the current cellstyle
ActiveCell.Hyperlinks.Delete 'Remove the hyperlink
ActiveCell.Style = sStylename 'Reapply the cellstyle used before
您还可以将超链接应用于透明的形状,并覆盖您想要添加超链接的单元格。
这里的缺点是,当未链接时,光标会随鼠标悬停而改变。并且该形状可以被最终用户移动和删除。
Dim temp As Variant
temp = (RangeObject).Interior.Color
(RangeObject).Hyperlinks.Delete
(RangeObject).Interior.Color = temp