函数的超链接(在选定单元格下添加新行)VBA

问题描述 投票:0回答:1

我正在按照我在这里找到的教程之一进行操作,如何使用超链接来调用函数。 有人可以建议需要做什么才能完成这项工作吗?

我的超链接:=HYPERLINK("#addLineClick()","添加新行")

我的功能:

Function addLineClick()

Set addLineClick = Selection

ActiveCell.EntireRow.Copy
With ActiveCell.Offset(1).EntireRow
.Insert

End With

需要注意的一件事:当我在 VBA 窗口中运行函数时,它会正常工作。

如有任何帮助,我们将不胜感激。

我尝试了上述方法,但当我单击超链接时它没有执行任何操作。

excel vba hyperlink
1个回答
0
投票

如果从 HYPERLINK() 公式调用函数,它将被触发,但它受到与从任何其他工作表公式调用时 VBA 函数相同的限制(请参阅 https://support.microsoft.com /en-au/topic/自定义函数限制说明-in-excel-f2f0ce5d-8ea5-6ce7-fddc-79d36192b7a1).
这包括无法执行更改工作表的操作,例如插入行...

例如:

=HYPERLINK("#tester()", "Test")

称之为:

Function tester()
    With Application.Caller
        Debug.Print "clicked on", .Address 'works
        .EntireRow.Insert                  '## does nothing ##
    End With
    Set tester = Selection 'must return a valid range for the link destination
End Function

要解决此问题,您可以使用常规超链接(插入>>链接)。

例如:

在常规模块中:

'This is a dummy link destination
Function tester()
    Set tester = Selection
End Function

在工作表模块中使用

FollowHyperlink
事件来处理链接点击:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Select Case Target.TextToDisplay
        Case "Add Row": Target.Range.Offset(1).EntireRow.Insert
        'other actions handled here....
    End Select
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.