Binding a macro to a number key.

Bluezero2x

New Member
Joined
May 21, 2015
Messages
5
Hi all,

I'm new on this forum, I've been looking at videos on youtube and googling this issue, to no effect.
I've been tasked by my employer to "make a number key perform some random action." I asked him if he mean macros and he said yes, but not to make this using the record macro feature?

Ive looked around, and i cant find a way to make the number keys do anything but produce numbers. Macro wont let you use numbers either for some reason. Help?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
To make the number 1 key on the top of the keyboard run the test macro, run the Shortcut1 macro first. To reset normal functionality run the Reset1 macro

Code:
Sub test()
MsgBox "Hello"
End Sub

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

Sub Reset1()
Application.OnKey "1"
End Sub
 
Upvote 0
Yes, but you have to run the code that sets it to do that. You could probably call it in the Workbook Open Event and turn it off in the Workbook BeforeClose event.
 
Upvote 0
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, 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? or rather, how do i put these two together?
 
Last edited:
Upvote 0
Update:

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


"Thisworkbook" contains



Code:
Sub Workbook_Open()
Application.OnKey "{97}", "test"


Application.OnKey "{98}", "test2"


Application.OnKey "{99}", "test3"


End Sub


Module one contains
Code:
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()"?
 
Upvote 0

Forum statistics

Threads
1,213,529
Messages
6,114,155
Members
448,554
Latest member
Gleisner2

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