excel vba添加超链接在组中形成

我绝对不知道为什么我不能添加超链接的形状,但只有当它通过内嵌函数触发。 当我从即时窗口手动运行时,一切正常。 我也没有任何麻烦手动做。

我也不知道怎么写好vba的error handling

这是示例代码。

function testCase(xlLabelName as String) Dim sht As Worksheet For Each sht In ActiveWorkbook.Sheets If shapeExist(sht, xlLabelName) Then sht.Hyperlinks.Add Anchor:=sht.Shapes(xlLabelName), Address:="", SubAddress:="a1" testCase = "Bingo" End If Next sht End Function 

从即时窗口我得到Bingo答案,并在单元格中我#arg! 错误,这只是没有执行的行。 我试图先取消组合形状

 Dim gshp As Shape Dim shrng As ShapeRange Dim tmp As String Set gshp = sht.Shapes(getParentShape(xlLabelName)) tmp = gshp.Name Set shrng = gshp.Ungroup sht.Hyperlinks.Add Anchor:=sht.Shapes(xlLabelName), Address:="", SubAddress:="a1" shrng.Regroup shrng.name = tmp 

但它也失败了。 而且即时的窗口给了我正确的答案( bingo )所有其他的操作都是根据我的需要(绘画)完成的,只有这个失败。 如果我删除这个有问题的行,我在单元格中得到了正确的答复。 我试图处理的形状是msoFreeform
检查形状是否存在的函数只是循环遍历表和比较名称中的所有形状(Lame,但现在它的工作)

提前致谢。

如果我是你,我会分配一个macros到我的形状,以便它会运行下面的子点击时:

 sub hyperlinkShape ThisWorkbook.FollowHyperlink ("C:\temp\test.xlsx") End sub 

如果您正在查找工作簿中的超链接(如更改工作表),则可以编写

 sub hyperlinkShape Sheets("Desiredsheet").Select End sub 

这样的超链接将不会被附加到形状,但子。 点击形状可以启动子。

对于error handling,我build议你阅读这个线程的答案,我几个月后发回: VBA Excel简单的error handling