Jas*_*son 8 excel vba excel-vba
我有一个包含许多行数据的电子表格.我希望能够使用该行中的数据单击将运行宏的单元格.由于行数总是在变化,我虽然每行的超链接可能是最好的方法.
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
Run Code Online (Sandbox Code Playgroud)
有没有办法通过单击超链接运行vba宏,并能够知道单击超链接的单元格行?
Jea*_*nno 11
这对你有用
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
MsgBox "Row " & ActiveCell.Row & " is clicked"
End Sub
Run Code Online (Sandbox Code Playgroud)
运行宏的更有趣的超链接方式,看起来是下一个方法(超链接公式)。它不需要任何事件:
Sub testCreateHyperlinkFunction()
'Very important to have # in front of the function name!
Range("A1:A5").Formula = "=HYPERLINK(""#MyFunctionkClick()"", ""Run a function..."")"
End Sub
Run Code Online (Sandbox Code Playgroud)
一个更壮观的用途是下一个方法,能够保留处理范围的初始单元格值(例如“A1:A5”):
Sub testCreateHyperlinkFunctionBis()
Dim rng As Range, arr As Variant, c As Range, i As Long
Set rng = Range("A1:A5")
arr = rng.Value
For i = 1 To UBound(arr, 1)
Range("A" & i).Formula = "=HYPERLINK(""#MyFunctionkClick()"", " & _
IIf(IsNumeric(arr(i, 1)), arr(i, 1), """" & arr(i, 1) & """") & ")"
Next i
End Sub
Run Code Online (Sandbox Code Playgroud)
Function MyFunctionkClick()
Set MyFunctionkClick = Selection 'Do not miss this part!
MsgBox "The clicked cell addres is " & Selection.row
End Function
Run Code Online (Sandbox Code Playgroud)
MyFunctionkClick()运行...小智 7
是的,您可以按照以下简单步骤执行此操作:
步骤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
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我们比较单元格地址然后执行一组代码或功能.还有另一种方法可以做到这一点.我们可以与目标名称进行比较并执行代码.在上面的示例中,我已将超链接目标的名称指定为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
Run Code Online (Sandbox Code Playgroud)
我只想添加另一种受我过去使用过的答案之一启发的方法。这意味着您不必创建超链接或辅助列来启动处理该行,您只需双击要处理的行中的任何单元格即可:
在工作表上,您想要双击工作,请使用以下命令:
Private Sub Worksheet_Activate()
Application.OnDoubleClick = "Module1.ProcessRow"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnDoubleClick = ""
End Sub
Run Code Online (Sandbox Code Playgroud)
然后Module1有一个处理活动单元格的例程:
Sub processRow()
MsgBox "Row " & ActiveCell.Row & " on " & ActiveSheet.Name & " was clicked"
End Sub
Run Code Online (Sandbox Code Playgroud)
您还应该在工作簿事件中禁用 Excel 双击方法:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnDoubleClick = ""
End Sub
Run Code Online (Sandbox Code Playgroud)
我发现双击方法对于许多过程来说确实非常方便和直观,例如,使用从行填充的数据调出用户窗体,或者将该数据传输到另一个工作表(可能使用计算或格式化等)。