Add-in with predefined shortcut key? - vba

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?

Related

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

Assign keyboard shortcut to macro inside the code

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

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.

VBA Select a sheet when the workbook is opened

The following program doesn't work when I open my workbook. What are the possible reasons?
' Select the first sheet when the workbook is opened.
Private Sub Workbook_Open()
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
If you hit alt+F11 to go to the VBA code editor. On the left side, under the file name you will see the different sheets, and whatever modules you might have in there. If you go under the ThisWorkbook module
and place your code there, it will automatically run when you start up the Excel File.
you are using the "Select" Method without an Activating a Sheet First!
Yes, when you have closed your workbook last time, the current sheet will remain in the memory index and when you will again open the same workbook, the pointer search for the most recent sheet used based on the index no.
'Here is the code
Private Sub Workbook_Open()
Sheet4.Activate
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
using "Select Method" without Activating the Parent Object is a Crime. lol
Hope this will help you.

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.