【问题标题】:Use AutoHotKey to assign shortcut to VBA Macro使用 AutoHotKey 为 VBA 宏分配快捷方式
【发布时间】:2021-08-09 19:16:27
【问题描述】:

有没有办法将 AutoHotKey 快捷键分配给 VBA 宏。

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

谁能帮帮我?谢谢

【问题讨论】:

  • the feature already exists 时为什么要使用第三方脚本?
  • 这是因为 Alt+H 不能在宏窗口中使用(链接 u 共享)并且 Alt+H 将激活主页选项卡,因此无法链接我的程序。

标签: excel vba autohotkey


【解决方案1】:

AHK 代码:(带有错误处理和调试语句)

#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 代码:

' placed in VBAProject/Modules/Module1
Option Explicit

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

按 Alt+h 后:(AHK 脚本运行,Excel 运行,带有 Sub 程序的工作簿打开并处于活动状态)

【讨论】:

  • 您是否将“theSub”替换为您自己的过程名称?您的程序是否放置在 a) 打开的工作簿中,b) 在公共(非类)模块中?
猜你喜欢
  • 2014-11-25
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 2018-07-17
  • 1970-01-01
相关资源
最近更新 更多