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?
For the numeric pad, you'll need to use the ASCII code...
Application.OnKey "{97}", "test"
Hope this helps!
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.
So that when i hit Numpad 1, it runs a simple macro like =A1+A2
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.
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?
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 7 hours 54 minutes ago
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.
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
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.
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!
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 ZhaoTechNet Community Support