Assign keyboard shortcut to macro inside the code - vba

I wrote a macro that change data in my excel, and I wrote other macro that
insert the first macro (I made) inside 1000+ different excels that don't have this macro. In regular situation I know how to assign keyboard shortcut to this macro: via "option" in Macro windows.
but my goal is that the keyboard shortcut for Sub Single in Macro1 will remains also in all other excels, I think the best way is to do it in my code.
I read the following articles but cant figure out where they want me to put the following code, so it will actually work?
Thanks in advance for all helprs ..
https://msdn.microsoft.com/en-us/library/office/ff197461.aspx
and that one:
Excel VBA: Assign keyboard shortcut to run procedure
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.OnKey "^b", "Macro1"
End Sub

Related

How to assign keyboard shortcuts to multiple VBA macros

I've currently got a list of about 10 macros that each do something different. I basically would like to include VBA code in each of them (or as a separate list) which has the keyboard shortcut to activate each.
I know you can assign a shortcut when you create a macro in the options, but once you've saved the macro as an add-in and closed the file, you can no longer change the shortcut.
I've also tried the application.onkey function, but that only seems to work for 1 macro and I don't see how to add to the list.
Hopefully this makes sense but please ask for clarification if needed
Thanks,
Thomas
This is the code I'm using for the only Application.OnKey that works:
Sub Workbook_Open()
Application.OnKey "^+{\}", "IF_Error_Wrap"
End Sub
Sub Workbook_BeforeClose()
??????
End Sub
You need to repeat the OnKey line for each assignment, and be sure to clear the shortcuts when the file closes using BeforeClose like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^+{\}"
End Sub
If it's an add-in, you could alternatively, and probably more safely, use the Workbook_AddinUninstall() event to clear the keys.

Is there a way to hide macros in Excel?

I just finished some VBA and I was wondering if there is a way to hide certain macros on Excel.
I need the user to run a certain macro and only that one, but it shows all the sub macros in Excel. I want to hide the unnecessary macros from the user so that way the user doesn't accidentally click on the wrong one.
You can also do this by placing the macros you want to hide in a separate module and using Option Private Module at the top of the module before the code. The macros will still be available to your project but will not appear in the Macros seen by the user when he clicks the Macros button.
You can either create a button in the ribbon to run the macro, or you can add "Private" before each "Sub" in the VBA editor that you don't want the user to easily access.
To subjectively 'hide' certain sub procedures (i.e. 'macros') from the (Alt+F8) Developer, Macros dialog use an optional non-variant parameter that means nothing.
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub
The meh macro will not show up in the list of macros to run. If you dim the parameter as variant it will show in the list. This is likely due to a optional variant parameter being able to use the IsMissing function. It will also not be able to be run from the VBE with F5 or stepped through with F8.
The test sub procedure will run the code correctly.
Sub test()
meh
End Sub
Sub meh(Optional w As Worksheet)
Debug.Print "hello world"
End Sub

Programming vba excel to move cursor

I am trying to figure out how to move a cursor from one excel cell to another with a specific shortcut using vba for excel.
say from any cell if I press Alt+e it bring me to A5 for exemple. Alt+d would bring me to E3 for example etc... Any help?
First, create a VBA macro like shown below
Sub MoveToA5()
Range("A5").Select
End Sub
Then, press ALT+F8, select "Options" and assign the shortcut combination, for example CTRL+Shift+a.
In order to use ALT+ HotKey combination (for example, ALT+F5), place the Macro shown above into code Module (you have to add it to your Workbook VBAProject using Modules->Insert->Module) and also add the following code snippet to ThisWorkbook code module:
Private Sub Workbook_Open()
Application.OnKey "%{F5}", "MoveToA5"
End Sub
Hope this will help.

How to run automatically Excel macros?

I have created a cricket sheet in excel. I have also created a macro which is used to display a message when overs bowled are equal to 20 (A T-20 Match). My problem is that when the over get to 20 no message is shown unless I press the shortcut key. Is there anything which I can do so that whenever overs reach to 20 a message automatically displays.
My code is:
Sub Innings()
If Range("H43") = 20# Then
MsgBox "Innings Completed"
End If
End Sub
Include the following macros in the worksheet code area:
Private Sub Worksheet_Calculate()
Call Innings
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H43")) Is Nothing Then
Call Innings
End If
End Sub
Because they are worksheet code macros, they are very easy to install and automatic to use:
right-click the tab name near the bottom of the Excel window
select View Code - this brings up a VBE window
paste the stuff in and close the VBE window
If you have any concerns, first try them on a trial worksheet.
If you save the workbook, the macros will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx
To remove the macros:
bring up the VBE windows as above
clear the code out
close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
Gary's Student's code is working well. You just need to write Worksheet_Change and Worksheet_Calculate to the List1 not to the Module1. I add a picture.

Add-in with predefined shortcut key?

Firstly, this is all in Excel 2007, FYI.
I'm trying to set up an Add-in that will use a shortcut key to run a simple macro. I want to define that shortcut key so that I can simply tell my users to press "ctl+alt+l" to run the macro, hopefully keeping explanations to a minimum on my team since the user interface would be unified.
My code to set up the shortcut key is right here:
Sub LiteralizeShortcutKey()
Application.OnKey "%^l", "Literalize"
End Sub
When I run the macro it works just fine.
I then create a Workbook macro to autoload when the excel file opened.
Private Sub Workbook_Open()
LiteralizeShortcutKey
End Sub
This works fine when I open the workbook. But it all falls apart when I export the workbook to a .xlam and then load it into another workbook. My little LiteralizeShortcutKey() Sub won't load, so I can't use my predefined shortcut key.
Thoughts?