2012-08-23 31 views
0

我想在一個特定的細胞如何定義範圍的OnKey事件?

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

定義逃生按鈕安其活動,並得到了錯誤彈出 - 宏不可用或禁用...
子單元()工作正常,作爲一個獨立的程序。

回答

2

明確referecce對我的作品,嘗試使用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 
+0

三江源,拉茲洛,它的工作原理。 – Alegro