我在Excel工作表中有一个形状,我必须添加/删除它的超链接作为我的代码的一部分。我们如何检查形状是否包含超链接?类似下面的代码:
if shape.hyperlink.exists is True then
shape.hyperlink.delete
end if
答案 0 :(得分:1)
Public Sub TestMe()
Dim sh As Shape
For Each sh In ActiveSheet.Shapes
On Error Resume Next
sh.Hyperlink.Delete
On Error GoTo 0
Next sh
End Sub
这个想法是删除每个形状的超链接。如果形状没有,那就很好了,代码继续。在此解决方案中,超链接被声明为变量 - How do I refer to a shape's hyperlinks using Excel VBA - 作为可以使用类似的变通方法。
答案 1 :(得分:1)
可以遍历工作表上的所有超链接,并确定这些超链接是在单元格中还是在Shapes 中(这可以避免使用OnError
):
Sub HyperActive()
Dim h As Hyperlink, n As Long
If ActiveSheet.Hyperlinks.Count = 0 Then
MsgBox "no hyperlinks"
Exit Sub
End If
For Each h In ActiveSheet.Hyperlinks
n = h.Type
If n = 0 Then
MsgBox "in cell: " & h.Parent.Address
ElseIf n = 1 Then
MsgBox "in shape: " & h.Shape.Name
End If
Next h
End Sub
答案 2 :(得分:0)
要检查Shape是否有超链接,请在帖子中调用此函数(而不是' shape.hyperlink.exists'):
Public Function HasHyperlink(shpTarget As Shape) As Boolean
Dim hLink As Hyperlink: Set hLink = Nothing
On Error Resume Next: Set hLink = shpTarget.Hyperlink: On Error GoTo 0
HasHyperlink = Not (hLink Is Nothing)
End Function