如何在VBA for Excel中为dynamicselect的单元格定义ENTER按键事件

我得到了一个dynamicselect的单元格,它将填充一些即将放置的信息,当我把这个单元格的信息和input按下时,

1 – 它应该触发一个macros

'macro(value) macro1 myinfo 

2 – macros应该获得该单元格中的信息

 myinfo = Cells( i, j ) 

那我该如何实现呢?

要捕获正在按下的特定键,您需要使用OnKey方法:

 Application.OnKey "~", "myMacro" ' for the regular enter key ' or if you want Enter from the numeric keypad: ' Application.OnKey "{ENTER}", "myMacro" ' Below I'll just assume you want the latter. 

上面说的是按下Enter键后myMacro必须运行。 OnKey方法只需要调用一次。 你可以把它放在Workbook_Open事件中:

 Private Sub Workbook_Open() Application.OnKey "{ENTER}", "myMacro" End Sub 

要停止捕获Enter键,

 Application.OnKey "{ENTER}" 

要检查单元格A1上的Enter是否被按下,您可以这样做:

 Sub myMacro() If Not Intersect(Selection, Range("A1")) Is Nothing Then ' equivalent to but more flexible and robust than 'If Selection.Address = "$A$1" Then MsgBox "You pressed Enter while on cell A1." End If End Sub 

现在要检测在特定的单元格中是否按下了Enter只有在该单元格已被编辑的情况下 ,我们必须有点聪明。 假设您编辑一个单元格值,然后按Enter键。 触发的第一件事是OnKeymacros,之后触发Worksheet_Change事件。 因此,您首先必须“保存OnKey的结果”,然后根据这些结果处理Worksheet_Change事件。

像这样启动OnKeyApplication.OnKey "{ENTER}", "recordEnterKeypress"

在你的代码模块中,你会有这样的:

 Public enterWasPressed As Boolean Sub recordEnterKeypress() enterWasPressed = True End Sub 

单元格编辑将被Worksheet_Change事件捕获:

 Private Sub Worksheet_Change(ByVal Target As Range) If enterWasPressed _ And Not Intersect(Target, Range("A1")) Is Nothing Then MsgBox "You just modified cell A1 and pressed Enter." End If enterWasPressed = False 'reset it End Sub 

现在,上面的代码可以解决问题中的问题,但是我想重申一遍:您的问题听起来像XY问题 。 为什么要检测按下的Enter键? 让我们知道,也许我们可以build议替代品。

当股票代码input到该单元格并给出该股票的信息在excel和Worksheet_Change或更改命令只会导致它得到一个循环的原因,当股票信息被parsing到它将触发更改单元格事件一次又一次.. – BerkerYüceer31分钟前

的Berker,

为此,您不需要捕捉“ENTER”键。 比方说,您键入股票代码,而不是按ENTER键,您单击另一个单元格。 你不希望macros在这种情况下被解雇吗? 如果是,请尝试下面的代码。 我假定在股票代码input单元格A1时macros必须运行。

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) On Error GoTo Whoa With Application .ScreenUpdating = False .EnableEvents = False End With '~~> This line ensure that the code will enter into the '~~> block only if the change happened in Cell A1 If Not Intersect(Target, Range("A1")) Is Nothing Then Application.EnableEvents = False ' ' ~~> Put your macro code here or run your macro here ' End If LetsContinue: With Application .ScreenUpdating = True .EnableEvents = True End With Exit Sub Whoa: MsgBox Err.Description Resume LetsContinue End Sub 

编辑 :我看你已经select了你的答案:)

使用工作表更改事件;

有些事情如下,

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$A$1" Then ' call your sub End If End Sub 

将此代码放入相应的工作表模块中。

非常感谢这个,我做了一些改变,如下所示:

Dim oldvalue As String Dim newvalue As String Private Sub Worksheet_Change(ByVal Target As Range)On Error GoTo Whoa

 With Application .ScreenUpdating = False .EnableEvents = False End With '~~> This line ensure that the code will enter into the '~~> block only if the change happened in Cell A1 If Not Intersect(Target, Range("A:D")) Is Nothing Then Application.EnableEvents = False ' ' ~~> Put your macro code here or run your macro here ' oldvalue = Range(Target.Address).Value Range(Target.Address).Value = Range(Target.Address).Value * 2.33 newvalue = Range(Target.Address).Value MsgBox ("value changed from " & oldvalue & " to " & newvalue) End If 

让继续:与应用.ScreenUpdating =真.EnableEvents =真结束与

 Exit Sub 

Whoa:MsgBox Err.Description Resume LetsContinue End Sub

这将给你一个机会来改变范围内的任何细胞一定的价值(我希望单元格的价值乘以因子一旦细胞价值改变,并给我一个信息,将给旧的和新的价值,

欢呼最好的运气