Running a Macro up Pressing the Numeric Enter Key

DMumme

New Member
Joined
Jun 14, 2014
Messages
23
Th following code performs flawlessly. The code is in a sheet module as I do not wish for it to run throughout the entire workbook. The code calls for cell selection anytime a change is made to certain cells. I would like for the cell selections to be made upon pressing the numeric enter key instead as sometimes the cells will not require changing. Ideas?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    On Error GoTo Whoa


    Application.EnableEvents = False


    If Not Target.Cells.CountLarge > 1 Then
        If Not Intersect(Target, Columns(6)) Is Nothing Then ' Column F to G
            Target.Offset(, 1).Select
        ElseIf Not Intersect(Target, Columns(7)) Is Nothing Then ' Column G to O
            Target.Offset(, 8).Select
        ElseIf Not Intersect(Target, Columns(15)) Is Nothing Then ' Column O to U
            Target.Offset(, 6).Select
        ElseIf Not Intersect(Target, Columns(21)) Is Nothing Then ' Column U to A Down 1
            Target.Offset(1, -20).Select
        End If
    End If


Letscontinue:


    Application.EnableEvents = True
    Exit Sub


Whoa:


    MsgBox Err.Description
    Resume Letscontinue
    
End Sub
[end code]
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
.
Paste this code in the sheet module :

Code:
Option Explicit


Private Sub Worksheet_Activate()
    Application.OnKey "{ENTER}", "RunIt"
End Sub


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


In a regular module paste this macro :

Code:
Option Explicit


Sub RunIt()
    MsgBox "Hi"
End Sub


You can change the name of the sub to whatever you want so long as the same name is shown in the macro in the sheet module.
Replace the MsgBox command with your macro code.
 
Upvote 0
Thank you for your quick response. Your answer was clear and easy to understand and follow. I really appreciate that.

I created a new Workbook with two sheets: Sheet1 and Sheet2. I copied and pasted your first code into the Sheet2 Module. I copied and pasted your second code into Module1. I went to Sheet2 and selected random cells and hit the numeric enter key and received the MsgBox every time. I then went to Sheet1 and the numeric enter key did not behave as normal. Depressing that key should have selected the row below but it remained on the selected cell.

SIDEBAR - If "{ENTER}" refers to the numeric enter key, how does one refer to the main enter key?
 
Upvote 0
Got it. Just needed to delete the "" at the end of your second code. Found it while researching the SIDEBAR question, the answer of which turns out to be "{~}".
Could not have done it without your help. Thanks again!
 
Upvote 0
.
The primary ENTER key is the tilde sympbol ~


Code:
Option Explicit


Private Sub Worksheet_Activate()
    Application.OnKey "~", "RunIt"
End Sub


Private Sub Worksheet_Deactivate()
    Application.OnKey "~", ""
End Sub

To turn the numeric ENTER key back to normal try this code:

Code:
Private Sub Worksheet_Deactivate()
    Application.OnKey "{ENTER}"
End Sub

If that doesn't work, you may need to create another OnKey macro for that specific sheet and assign the OnKey event to the other macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,485
Messages
6,113,931
Members
448,533
Latest member
thietbibeboiwasaco

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top