Making a number key run a macro?
Basically, i want to be able to hit 1 and have a macro run. I found this code for the onkey command, though i dont know how to bind it to a macro. 

So if i make a simple macro like;


Sub Adding()
'
' Adding Macro
'


'
ActiveCell.FormulaR1C1 = "=RC[-3]+RC[-2]"
Range("D2").Select
End Sub


Then make a new module i assume, and put in;

Sub test()
MsgBox "Hello"
End Sub

Sub Shortcut1()
Application.OnKey "1", "test"
End Sub

How do i bind the macro to this hotkey? 
May 23rd, 2015 12:22pm

For the numeric pad, you'll need to use the ASCII code...

Application.OnKey "{97}", "test"

Hope this helps!

Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2015 1:48pm

If you need to use this in all excel files then create a blank excel file->Press Alt+F11->Find "ThisWorkbook" module at left ->Double click-> paste below code

Private Sub Workbook_Open()

Application.OnKey "{97}", "test"
 
'or

Application.OnKey "1", "test"

End Sub

then return to excel. Save as excel addin. Click Excel Office Button at top left->Clcik "Excel Options" at bottom right. Select "Add-Ins" from left pane.Select "Go" button at bottom. Add ins dialog box will open, select the file which you just saved as add-in.

Restart excel and see.

If needed in particular excel file then pasting at ThisWorkbook module is all you need.

May 23rd, 2015 2:58pm

Thank you! Now, is there a way to attach a macro to the application.onkey "{97}"    
 So that when i hit  Numpad 1, it runs a simple macro like  =A1+A2 
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2015 5:28pm

I tried pasting a macro under 
Sub test()
MsgBox "Well, finally"

End Sub
Sub test2()

    Range("A1").Select

End Sub

Sub test3()
'
' Add Macro
'

'
    ActiveCell.FormulaR1C1 = "=R[1]C+R[1]C[1]"
    Range("A2").Select
End Sub
test 3 macro was copy and pasted from a recorded macro. It doesnt work.

 i dont know the syntax to make test3 run correctly.
May 23rd, 2015 5:55pm

Unclear.  Earlier, you used a relative reference.  So are you now looking to refer to A1 and A2 in absolute terms?  If so, try...

ActiveCell.FormulaR1C1 = "=R1C1+R2C1"
Otherwise, can you please clarify?

Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2015 6:58pm

Ok, ive figured it out up to this point on my own. 

"Thisworkbook" contains 

Sub Workbook_Open()
Application.OnKey "{97}", "test"
Application.OnKey "{98}", "test2"
Application.OnKey "{99}", "test3"
End Sub

Module one contains

Sub test()
MsgBox "Well, finally"
End Sub
Sub test2()
    Range("A1").Select
End Sub
Sub test3()
'
' Add Macro
'
'
    ActiveCell.FormulaR1C1 = "=R[1]C+R[1]C[1]"
    Range("A2").Select
End Sub



1. Test 3 doesnt work. Its a macro copied from the record macro module, and pasted into test3. I dont know how to make a hotkey use a macro. How would i go about that?


2. What difference does "Private Sub Workbook_Open()" make over "Sub Workbook_Open()"?

May 23rd, 2015 7:32pm

1) With your code, when the number 3 on the numeric pad is pressed, "test3" should run.  And, when it runs, it should add a formula to the active cell that adds the cell that's one cell below the active cell and the cell that's one cell below and one column to the right of the active cell.  Is this not the desired result?  If not, can you please clarify?

2) The keyword "Private" indicates that the sub procedure is only available to other procedures in the module where it's declared.  When the keyword "Public" is used (or the keyword is omitted), it means that the sub procedure is available to all procedures in all modules.

Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2015 8:01pm

Ok, ive figured it out up to this point on my own. 

"Thisworkbook" contains 

Sub Workbook_Open()
Application.OnKey "{97}", "test"
Application.OnKey "{98}", "test2"
Application.OnKey "{99}", "test3"
End Sub

Module one contains

Sub test()
MsgBox "Well, finally"
End Sub
Sub test2()
    Range("A1").Select
End Sub
Sub test3()
'
' Add Macro
'
'
    ActiveCell.FormulaR1C1 = "=R[1]C+R[1]C[1]"
    Range("A2").Select
End Sub



1. Test 3 doesnt work. Its a macro copied from the record macro module, and pasted into test3. I dont know how to make a hotkey use a macro. How would i go about that?


2. What difference does "Private Sub Workbook_Open()" make over "Sub Workbook_Open()"?

  • Edited by bluezero2x Saturday, May 23, 2015 11:34 PM
May 23rd, 2015 11:31pm

1) With your code, when the number 3 on the numeric pad is pressed, "test3" should run.  And, when it runs, it should add a formula to the active cell that adds the cell that's one cell below the active cell and the cell that's one cell below and one column to the right of the active cell.  Is this not the desired result?  If not, can you please clarify?


2) The keyword "Private" indicates that the sub procedure is only available to other procedures in the module where it's declared.  When the keyword "Public" is used (or the keyword is omitted), it means that the sub procedure is available to all procedures in all modules.

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2015 2:26am

To all of you here, you have helped me so much, i couldnt have gotten this far without your help! if i can give you positive rating, please tell me how!
May 24th, 2015 3:20am

In that case, try...

Sub test3()
    ActiveCell.FormulaR1C1 = "=RC[-2]+RC[-1]"      
End Sub

To limit the shortcut to a particular workbook, you can use the Workbook_Activate and Workbook_Deactivate events, in addition to the Workbook_Open and Workbook_BeforeClose events.  So, for example, you would have the following in the code module for "ThisWorkbook"...

Private Sub Workbook_Activate()
    Application.OnKey "{97}", "test"
    Application.OnKey "{98}", "test2"
    Application.OnKey "{99}", "test3"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnKey "{97}"
    Application.OnKey "{98}"
    Application.OnKey "{99}"
End Sub

Private Sub Workbook_Deactivate()
    Application.OnKey "{97}"
    Application.OnKey "{98}"
    Application.OnKey "{99}"
End Sub

Private Sub Workbook_Open()
    Application.OnKey "{97}", "test"
    Application.OnKey "{98}", "test2"
    Application.OnKey "{99}", "test3"
End Sub

Hope this helps!

Free Windows Admin Tool Kit Click here and download it now
May 24th, 2015 12:30pm

Hi bluezero2x,

Please try Domenic Tamburino's suggestion first, and this is the forum to discuss questions and feedback for Microsoft Excel, your question is more related to Excel DEV, if you have further question, I recommend you post it to the MSDN forum for Excel

http://social.msdn.microsoft.com/Forums/en-US/home?forum=exceldev&filter=alltypes&sort=lastpostdesc

The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

George Zhao
TechNet Community Support

May 24th, 2015 9:32pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics