How to call a function on ENTER key in Excel using vba

前端 未结 2 1988
庸人自扰
庸人自扰 2020-12-17 06:55

Google should provide me with ample examples but none of them seem to work

What I want: Everytime the user presses, and then releases, the ENTER key

2条回答
  •  余生分开走
    2020-12-17 07:40

    First, make an callback Sub that performs the logic you need. Put it into a new code module (NOT into worksheet code):

    Sub SomeActions()
    ...
    End Sub
    

    Then, subscribe to OnKey event, for example, when the user opens the workbook (this code goes into ThisWorkbook) module in VBA editor:

    Private Sub Workbook_Open()
        Application.OnKey "~", "SomeActions"
    End Sub
    

    "~" means Enter key. For numeric keypad key use "{ENTER}".

提交回复
热议问题