Application.OnKey不起作用

时间:2016-10-05 09:09:21

标签: excel excel-vba keypress excel-2013 vba

我正在尝试使用Application.OnKey方法在Excel 2013中按特定键时执行sub。

我有以下代码

Public Sub Workbook_Open()
    Application.OnKey "{r}", "MyEvent"
    Application.OnKey "{t}", "MyEvent"
    Application.OnKey "{x}", "MyEvent"
    Application.OnKey "{z}", "MyEvent"
End Sub

Public Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnKey "{r}"
    Application.OnKey "{t}"
    Application.OnKey "{x}"
    Application.OnKey "{z}"
End Sub

Public Sub MyEvent()
    MsgBox "Hello"
End Sub

当我按下键时,子MyEvent不会执行。如何修改代码才能使其正常工作?

编辑:我希望按键为:rtxz而不是{{ 1}}。

1 个答案:

答案 0 :(得分:0)

这对我有用。将代码添加到模块,保存工作簿,然后重新打开它。

<!-- ========== Form Bean Definitions ================================== -->
<form-beans>

    <form-bean name="helloWorldForm" type="com.mkyong.common.form.helloWorldForm"/>

</form-beans>

<!-- ========== Action Mapping Definitions ============================= -->
<action-mappings>
    <action name="helloWorldForm" path="/helloWorldForm" type="com.PracticeProgram.common.action.HelloWorldAction" >
        <forward name="success" path="/HelloWorld.jsp" />
    </action>
</action-mappings>