vba 将宏绑定到excel单元格onclick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1471741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind macro to excel cell onclick?
提问by Cheekysoft
In excel 2000, is it possible to bind a vba function to be executed when a cell is clicked with the mouse?
在excel 2000中,是否可以绑定一个用鼠标单击单元格时要执行的vba函数?
回答by Arjen
Found a solution:
找到了解决办法:
Make a named range for the cells you want to capture clickevent
为要捕获 clickevent 的单元格创建一个命名范围
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Application.ActiveSheet.Cells(1, 1).Value = Target.Address
If Not Intersect(Target, Range("MyNamedRange")) Is Nothing Then
' do your stuff
Range("A1").Select
Endif
Application.EnableEvents = True
End Sub
回答by JDunkerley
You can bind to a cell double click.
您可以绑定到单元格双击。
Open VBA, goto the worksheet you want to wire up the event Select WorkSheet in the dropdown on the top left and BeforeDoubleClick in the top right The check the Target.Address is equal to the address of the cell you care about and call the function you wish.
打开VBA,转到要绑定事件的工作表 选择左上角的下拉菜单中的WorkSheet和右上角的BeforeDoubleClick 检查Target.Address是否等于您关心的单元格的地址并调用您的函数希望。
Something like this:
像这样的东西:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address(True, True, xlA1) = "$A" Then
Call MyDemo
End If
End Sub
Private Sub MyDemo()
MsgBox "Hello"
End Sub