OnKey还要按初始键

时间:2019-04-22 07:54:29

标签: excel vba

嗨,我正在尝试将一个宏附加到按键组合上,并同时运行初始的按键组合,但似乎不起作用。

与此有关,我将在第二秒解释为什么有一种方法可以将按下的键作为变量发送或以某种方式捕获?例如类似:         Application.OnKey“ {LEFT}”,“ testMessage({LEFT})” ,但这对我不起作用。 在下面的示例中,         SendKeys“ {RIGHT}” 实际上由于某种原因触发了NumLock,我听说这是Excel的一个错误,但是如果我可以将键作为变量发送,则可以使用它来模拟SendKeys的其他方式,例如offset。

亲切的问候, 丹尼尔

Sub onKeyToggle()
If ActiveSheet.CodeName = "WS_Deliverables" Then
    Application.OnKey "{LEFT}", "testMessage"
    Application.OnKey "{RIGHT}", "testMessage"
    Application.OnKey "{UP}", "testMessage"
    Application.OnKey "{DOWN}", "testMessage"
Else
    Application.OnKey "{LEFT}", ""
    Application.OnKey "{RIGHT}", ""
    Application.OnKey "{UP}", ""
    Application.OnKey "{DOWN}", ""
End If
End Sub

Sub testMessage()
SendKeys "{RIGHT}"
MsgBox "pressed"
End Sub

1 个答案:

答案 0 :(得分:0)

为了将按键重置为正常含义,您必须按以下步骤更改代码。而且,您还必须确保 codename 确实是 WS_Deliverables ,并且您没有在谈论工作表的可见名称。

Sub onKeyToggle()
    If ActiveSheet.CodeName = "WS_Deliverables" Then
        Application.OnKey "{LEFT}", "testMessage"
        Application.OnKey "{RIGHT}", "testMessage"
        Application.OnKey "{UP}", "testMessage"
        Application.OnKey "{DOWN}", "testMessage"
    Else
        Application.OnKey "{LEFT}"
        Application.OnKey "{RIGHT}"
        Application.OnKey "{UP}"
        Application.OnKey "{DOWN}"
    End If
End Sub