0
votes

is there any way to assign AutoHotKey shortcut key to VBA Macro.

#IfWinActive, ahk_exe EXCEL.EXE
~Alt::vkFF
!h:: ->VBA procedure
#If

Can anyone help me? Thanks

1
Why use a 3rd-party script when the feature already exists? - Mathieu Guindon
This is because Alt+H cannot be used in the Macro Window (link u shared) and Alt+H will activate the home tab, thus not able to link my procedure. - BadBunny

1 Answers

1
votes

AHK code: (with error handling and debugging statements)

#IfWinActive, ahk_exe EXCEL.EXE
~Alt::vkFF
!h:: 
    try {
        XL := ComObjActive("Excel.Application")
    } catch {
        MsgBox, 16,, Cann't obtain Excel! 
        return
    }
    MsgBox, 64,, Excel obtained successfully!   ;for debugging purposes
    try {
        XL.Run("theSub")
    } catch {
        MsgBox, 16,, Cann't find "theSub" in the opened workbook!
    }
#If

VBA code:

' placed in VBAProject/Modules/Module1
Option Explicit

Sub theSub()
    MsgBox "It's running"
End Sub

After press Alt+h: (AHK script running, Excel running, the workbook with theSub procedure is open and active)
enter image description here