如何在VBA for Excel中为动态选择的单元格定义ENTER按键事件

时间:2012-02-21 12:00:30

标签: excel events vba excel-vba

我有一个动态选择的Cell,它将填充一些我将要放置的信息以及当我将信息和 ENTER keypressed 放在该Cell时;

1 - 它应该触发一个宏

'macro(value)
macro1 myinfo

2 - 宏应该获取该单元格中的信息

myinfo = Cells( i, j )

那我该如何实现呢?

4 个答案:

答案 0 :(得分:18)

要捕获正在按下的特定键,您需要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 键时必须运行myMacroOnKey方法只需要调用一次。你可以把它放在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键。触发的第一件事是OnKey宏,然后触发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 problem。为什么要检测按下的 Enter 键?让我们知道,也许我们可以建议替代方案。

答案 1 :(得分:6)

  
    
      
        

导致在该单元格中输入股票代码并在excel中提供该股票的信息并且Worksheet_Change或更改命令只会导致它进入循环时导致错误启动宏导致当股票信息被解析到单元格中时将一次又一次地触发改变事件.. - BerkerYüceer31分钟前

      
    
  

的Berker,

为此,您无需捕获“ENTER”键。比方说,您键入股票代码,而不是按ENTER键,您单击另一个单元格。您是否也不希望在该场景中触发宏?如果是,请尝试下面的代码。我假设在单元格A1中输入股票代码时必须运行宏。

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

编辑:我看到您已经选择了答案:)

答案 2 :(得分:3)

使用工作表更改事件;

如下所示,

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$A$1" Then
        ' call your sub

    End If
End Sub

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

答案 3 :(得分:0)

非常感谢这一点,我会对其进行更改,如下所示:

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

LetsContinue:     随着应用         .ScreenUpdating = True         .EnableEvents = True     

结束
Exit Sub

哇:     MsgBox Err.Description     继续LetsContinue 结束子

这将使您有机会将范围内的任何单元格更改为特定值(我希望在单元格值更改后将单元格值乘以因子,并向我显示将给出旧值和新值的消息,

欢呼声 祝你好运