Can I save a keyboard shortcut macro in an add-in module?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I just used the macro recorder to generate a macro that will perform the Center Across Selection operation that I seem to be using a lot. It put it in a code module in that workbook, which I then had to save as a macro enabled workbook. And, of course, that macro is not available to other workbooks.

I have an add-in module containing lots of UDSFs. I tried moving the macro to that add-in, but the keyboard shortcut no longer works.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
In Your VBA project window where you see your Module scripts. Drag the script into your Personal Folder. That way it will be available in all workbooks that are Macro enabled
Now to run the script just use your shortcut key
 
Upvote 0
In Your VBA project window where you see your Module scripts. Drag the script into your Personal Folder. That way it will be available in all workbooks that are Macro enabled
Now to run the script just use your shortcut key
I think my Add-in module may be messed up. There is no "Personal Folder" listed.

1644713982572.png

Can I just add a module named "Personal Folder"?

Thanks
 
Upvote 0
You are correct. VBA won't let me name a module "Personal Folder". I found a website that said to record a macro and choose "Personal Macro Workbook" in the "Store macro in:" field of the "Record Macro" dialog and that would create a Personal Folder, if none exists.

1644744551311.png


I did that and it worked. I now have a Personal Folder and my macro works. ??

1644745521796.png


Now my only problem is that I cannot find where Excel stored it. Another website instructed me to open the Trust Center and check the trusted locations for the location of the XLSTART folder. This is what I found:

1644745202172.png


I went to that location, but that folder is empty. And I do have hidden items checked to view, as you can see.

1644745367359.png


Any other clues?
 
Upvote 0
And why do you need to know where it is stored?

Just give your script a shortcut key and launch it like that.
Now like in your image you can see it. If you want to modify it or put a copy in your active workbook, just click on the module and drag it into your active workbook. Now to put the script into your add in module again I do not know how to do that. If you drag it into your active workbook it puts a copy in your active workbook. If you want to put it back give it a new name or right click on it and choose to delete.

I have been doing this for years and never really cared where it's being stored. And a script like this will work in any workbook you have now or in the future. Just use the shortcut key
 
Upvote 0
If you want to delete the macro in your personal folder.
Just right click on it and choose Remove.
 
Upvote 0
Can I save a keyboard shortcut macro in an add-in module?
Yes, you can, but it's quite a hassle and the shortcuts will not stick when the just recorded macro is moved to another module within the same Add-In. You've discovered that already.
I would go for the OnKey method. That way you can paste any macro code to any standard module within your Add-In.
You just need to add an appropriate line of code to the assign list in the macro below:

This goes in a standard module of your Add-In:
VBA Code:
Option Explicit

Public Enum ShortcutToMacroCmd
    Assign
    Cancel
End Enum

Public Sub MacroShortcuts(ByVal argCmd As ShortcutToMacroCmd)
    Const KEYALT   As String = "%"
    Const KEYCTRL  As String = "^"
    Const KEYSHIFT As String = "+"
    
    If argCmd = Assign Then
        '  assign shortcuts to some macros stored in this Add-In
        Excel.Application.OnKey KEYALT & KEYCTRL & "u", "SomeAddInMacro"        ' Alt Ctrl u
        Excel.Application.OnKey KEYALT & "{+}", "SomeOtherAddInMacro"           ' Alt +
        
    ElseIf argCmd = Cancel Then
        '  give shortcuts their original meaning
        Excel.Application.OnKey KEYALT & KEYCTRL & "u"
        Excel.Application.OnKey KEYALT & "{+}"
        
    Else
        ' this should not happen
    End If
End Sub


The code above needs to be invoked when your Add-In gets active. So ...

This goes in the ThisWorkbook module of your Add-In:
VBA Code:
Option Explicit

Private Sub Workbook_AddinInstall()
    MacroShortcuts Assign
End Sub
Private Sub Workbook_AddinUninstall()
    MacroShortcuts Cancel
End Sub
Private Sub Workbook_Open()
    MacroShortcuts Assign
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    MacroShortcuts Cancel
End Sub



If you wanted to use the PERSONAL.XLSB workbook for your macros assigned to keyboard shortcuts and still need to know its folder on disk ....
Now my only problem is that I cannot find where Excel stored it.

... then open its ThisWorkbook module, paste the code below in there and run it ...
VBA Code:
Sub WhereAmI()
    MsgBox Me.Path
End Sub


Hope this helps.
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

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