0

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

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

并得到错误弹出窗口 - 宏不可用或被禁用...
Sub unit() 作为独立程序工作正常。

4

1 回答 1

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
于 2012-08-23T10:24:11.333 回答