热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在Excel中,我可以使用超链接运行vba宏吗?

如何解决《在Excel中,我可以使用超链接运行vba宏吗?》经验,为你挑选了2个好方法。

我有一个包含许多行数据的电子表格.我希望能够使用该行中的数据单击将运行宏的单元格.由于行数总是在变化,我虽然每行的超链接可能是最好的方法.

   ROW MeterID   Lat    Long   ReadX  ReadY  ReadZ   CoeffA  CoeffB  CoeffC
   2   10f62gs   34.1   33.3   102.2  231.3  382.2   4.34    22.1    0.002
   3   83gs72g   34.4   31.4   109.2  213.1  372.1   2.23    12.7    0.023
   4   43gS128   33.3   32.2   118.8  138.7  241.8   1.94    5.08    0.107

有没有办法通过单击超链接运行vba宏,并能够知道单击超链接的单元格行?



1> Jeanno..:

这对你有用

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
        MsgBox "Row " & ActiveCell.Row & " is clicked"
End Sub



2> 小智..:

是的,您可以按照以下简单步骤执行此操作:

步骤1.选择要在何处制作超链接的单元格步骤2.右键单击 - >超链接...步骤3.输入要在其中创建超链接的相同单元格的地址,并为链接指定名称.见下图:

将宏指定给超链接

步骤4.单击"确定".步骤5.创建HyperLink.

注意:单击此超链接将不执行任何操作,因为它已分配给相同的单元格地址.

步骤6.现在按Alt + F11步骤7.复制粘贴以下代码

单击超链接运行Excel宏

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)  
    'Check if the Target Address is same as you have given  
    'In the above example i have taken A4 Cell, so I am  
    'Comparing this with $A$4  

    If Target.Range.Address = "$A$4" Then  
        'Write your all VBA Code, which you want to execute  
        'Or Call the function or Macro which you have  
        'written or recorded.  
        MsgBox "Write your Code here to be executed"  
        Exit Sub  
    End If  
End Sub  

在上面的代码中,我们比较单元格地址然后执行一组代码或功能.还有另一种方法可以做到这一点.我们可以与目标名称进行比较并执行代码.在上面的示例中,我已将超链接目标的名称指定为MyMacro.

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)  
    'Check if the Target Name is same as you have given  
    'In the above example i have given the Name of the HyperLink  
    'is MyMacro.  

    If Target.Name = "mymacro" Then  
        'Write your all VBA Code, which you want to execute  
        'Or Call the function or Macro which you have  
        'written or recorded.  
        MsgBox "Write your Code here to be executed"  
        Exit Sub  
    End If  
End Sub  


推荐阅读
author-avatar
喵喵方小幺
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有