BadBunny
BadBunny

Reputation: 11

Use AutoHotKey to assign shortcut to VBA Macro

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

Upvotes: 0

Views: 433

Answers (1)

Алексей Р
Алексей Р

Reputation: 7627

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

Upvotes: 1

Related Questions