Keyboard shortcut inside a module to call itself - excel

I'm creating an excel module to export any table inside a workbook as .csv
My module generate a UserForm where the user can chose which table to export inside a combobox and then click 'export'
Everything is working perfectly, but I have to create a button in my workbook and call my module on click.
However I want to keep it very simple to setup inside any workbook by just import the module and run it with a keyboard shortcut '''Ctrl+R''' for example.
I search a lot about that but I never find something, Is it something possible?

Yes you can what you want to. First import your module to your new excel workbook and then assign keyboard shortcut to Workbook_Open event by copying code to ThisWorkbook area.
Sub MyMacro()
MsgBox ("ok")
End Sub
In ThisWorkbook area (which is in Microsoft Excel Objects / Project Window)
Private Sub Workbook_Open()
Application.MacroOptions Macro:="MyMacro", Description:="", ShortcutKey:= _
"a"
End Sub
This code assign Ctrl+"Defined_Letter" shortcut to your MyMacro macro. In this case, the code's shortcut is [Ctrl+a].

Related

Calling a macro when selecting a chart sheet

I'm using the following simple code to run a macro "mymacro" when clicking/selecting a sheet.
Private Sub Worksheet_Activate()
Call mymacro
End sub
When i execute the macro manually it's working very well but when i click on the sheet it is not.
Basically i'm using the macro to change a chart colors...so when i apply this to a normal sheet where i have a chart as object it's working but when i tried with a sheet where there is only a chart (created by using "Move Chart" on new sheet option) nothing happens
Thank you for the help
The name needs to be Private Sub Chart_Activate() since it is a Chart and not a Worksheet. As VBasic2008 pointed out, the code needs to be in the code module for the Chart. Press Alt+F11 to open the VB Project, CRTL+R to open the Project Explorer. Double click on the Chart, eg.Chart1.
Your code module should look like :
Private Sub Chart_Activate()
Call mymacro
End Sub
Ensure that mymacro is Public or is also in this code module.

Assign a keyboard shortcut to a Microsoft Excel Add-in

My friend shared a .bas file with me and told me to save it as .xlam in the vbEditor to have it as an Addin.
I've browsed in Add-ins and am able to enable it in my workbook.
Is there a way I can assign a keyboard shortcut to the Add-in? There's only one sub in that add-in file now.
I tried writing another sub with
Application.onKey "+^{C}" ,'Calculate'
But it doesn't trigger the sub to be executed.
You don't assign a shortcut to an add-in. Rather, you assign a shortcut to a macro - that is, a Public Sub procedure in your standard module.
So your code file might look like this:
Option Explicit
Public Sub Calculate()
'...code...
End Sub
Open it in Notepad. I'll look like this:
Attribute VB_Name = "Module1"
Option Explicit
Public Sub Calculate()
'...code...
End Sub
Under Public Sub Calculate(), you want to add an attribute so that the file looks like this:
Attribute VB_Name = "Module1"
Option Explicit
Public Sub Calculate()
Attribute Calculate.VB_ProcData.VB_Invoke_Func = "C\n14"
'...code...
End Sub
This is exactly how Excel's macro recorder assigns macro hotkeys: no need for any Application.OnKey work-arounds.
Save the file, import it into your VBA project: Ctrl+Shift+C will now invoke that macro.
If you're using Rubberduck, forget all of the above and just go to your module in the VBA editor, find the procedure and annotate it like so:
Option Explicit
'#ExcelHotkey("C")
Public Sub Calculate()
'...code...
End Sub
Where "C" will make the hotkey Ctrl+Shift+C; I'd warmly recommend not using "c" to avoid hijacking Ctrl+C (Copy).
Bring up the code inspections toolwindow, hit the "refresh" button; under "Rubberduck Opportunities" there should be an inspection result warning about annotations & attributes being out of sync - select "add member attribute" from the "Fix" menu, and you're done - no need to export/edit/import or deal with any obscure syntax, and if you want to change the hotkey, simply edit the comment accordingly and re-synchronize annotations & attributes.
See VB_Attribute annotations for more information.
As for saving your VBA project as an add-in, simply save your VBA project's host workbook as a .xlam add-in file, then close Excel altogether and re-open it - load your .xlam from the Developer Ribbon tab's "Excel Add-ins" button (hit "Browse" to locate your .xlam file if it's not in the list).

Excel Macro Auto Activate

I have a macro enabled workbook called import. When it opens a macro automatically runs; its main job is to import text files into each worksheet of a new workbook (this new workbook is called book1).
I have another macro that I required to run on book1 called runall.
Is it possible to configure Excel so that runall will then be activated when book1 is enabled?
Currently I have to go back into the original workbook called import and on the Developer tab activate runall.
What I have tried -
In the import workbook I have the following code:
Private Sub Workbook_Open()
Call CombineTextFiles
I have tried putting Call runall in the Private statement.
There are a lot of events you can tie your code to. One of them is Sheet Activate. In Book1 you can place the following code:
Private Sub Worksheet_Activate()
runall
End Sub

Running an existing Macro on a new Excel File automatically when opened

(Excel 2010 question)
I have a reporting application that exports To excel. I have a Macro created that does some functionality. I need to run this macro whenever I open this new Excel Workbook after I export it.
Since it's a brand new worksbook, the excel does not have the macro that I have created embedded into it. So need to know if this is possible and how to do it.
Thanks!
You can open both the workbook that contains the macro and then the NEW workbook.
Then hit F11 for the Developer's Console. Usually it opens with the Project Explorer open on the left. If it isn't you can open it in the View menu, or by hitting Ctrl+R.
You need to copy the module that contains the macro into the modules of the New Workbook. Either insert a module from the Insert menu, or right click on the Modules > Insert > Module, then copy and paste the code, or you can just INSERT the module.
Let's assume your original Macro is named "Macro1". Make sure it isn't private.
I made one for example:
Sub Macro1()
MsgBox("Welcome to the Workbook!")
End Sub
In the Project Explorer, select the New workbook, and there should be an object titled "ThisWorkbook". Right click on that and select "View Code".
Private Sub Workbook_Open()
Call Macro1
End Sub
Save it, exit, open it back up and see your macro execute.

How to call a macro from another worksheet.?

I have assigned a macro to a button in worksheet 2. I have created a new button in a new worksheet and now i want the same macro to run or call from worksheet 4.
...hmm....
Put your code in a module:
You can then call it from anywhere. If you were to add an ActiveX control to a sheet then you could just double click the button in design mode and amend the code to the following and it should work:
Private Sub CommandButton1_Click()
Call CallMeFromAnyWorkSheet
End Sub

Resources