如何为范围定义OnKey事件?

时间:2012-08-23 06:01:35

标签: excel vba excel-vba

我想为特定单元格上的转义按钮定义OnKey事件

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Value = "a" Or ActiveCell.Value = "b" Then
Application.OnKey "{ESC}", "unit"

弹出错误 - Macro无法使用或被禁用...
子单元()作为一个独立的过程可以正常工作。

1 个答案:

答案 0 :(得分:2)

显式参考对我有用,尝试使用sheetname.subname,如Sheet6.unit:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Debug.Print ActiveCell.Value
    If ActiveCell.Value = "d" Or ActiveCell.Value = "e" Then
        Application.OnKey "{ESC}", "Sheet6.unit"
        Else
        Application.OnKey "{ESC}", ""
    End If
End Sub


Public Sub unit()
    Debug.Print "Unit!"
End Sub