Launch an Excel Macro

with AutoHotkey

by Denis Lamarre
last updated September 7, 2016
difficulty.png Easy

by Denis Lamarre

Description

You have created the best Excel macros and would like to trigger some of them with AutoHotkey? This very simple script will show you a way to do it. Excel macros should be successfully triggered if:

Check the ‘’More Info’’ section below to see how to pass arguments (variables) to your Excel macros. This will give you a way to pass information from one application, file or anything else you want to your Excel worksheet without using copy/paste.

Note: AutoHotkey has no limits when it comes to automation in Windows, but for many users who may only need simple keyboard shortcuts, installing and learning AutoHotkey, and keeping up to date, can be overkill. ShortKeeper can be used (with and without an Enterpad) to manage an AutoHotkey-based system in a way most non-programmers will find remarkably user-friendly and and effective [learn more].

Steps

1

overlay-um-macro.gifPrepare your overlay
Choose a free key on the Enterpad for which you want to launch an Excel macro. Use a relevant name. This example uses "Macro1".

2

Code installation
Copy/paste the following script in your AutoHotkey script template (Enterpad.ahk) at the chosen key location.

001:
  IfWinNotActive, ahk_class XLMAIN
  {
    MsgBox % "Excel is not active. No action will be performed."
  Return
  }
  try
    epExcel := ComObjActive("Excel.Application")
  catch
  {
    MsgBox % "Unable to execute the shortcut."
    Return
  }
  try
    epExcel.Run("Macro1")
  catch
  {
    MsgBox % "Unable to run the macro. Are you editing a cell?"
  }
  epExcel :=
return

More Info

Some setups allow to run more than one Excel process at the same time. The script above is not intended to work with these kinds of setups. The following post (on the AutoHotkey forum) explains a more efficient way to run a macro while multiple Excel processes are active:

https://autohotkey.com/board/topic/77840-how-to-pass-com-object-pointer-to-a-script/#entry495039

If you would like to pass an argument (variable) to the called macro: just modify the line 15 of the script (step 2) from:

epExcel.Run("Macro1")
        

To

epExcel.Run("Macro1", "Arg1")
        

And replace Arg1 with the value of the argument.