Excel 2010 Bible

(National Geographic (Little) Kids) #1

Chapter 43: Working with Excel Events


885


Note
The key codes are enclosed in brackets, not parentheses. For a complete list of the keyboard codes, consult
VBA Help. Search for OnKey. n

Tip
The preceding examples used On Error Resume Next to ignore any errors generated. For example, if the
active cell is in the first row, trying to move up one row causes an error. Furthermore, if the active sheet is a
chart sheet, an error occurs because no such thing as an active cell exists in a chart sheet.


By executing the following procedure, you cancel the OnKey events, and the keys return to their
normal functions.

Sub Cancel_OnKey()
Application.OnKey “{PgDn}”
Application.OnKey “{PgUp}”
End Sub

Caution
Contrary to what you may expect, using an empty string as the second argument for the OnKey method does
not cancel the OnKey event. Rather, it causes Excel to ignore the keystroke and do nothing. For example, the
following instruction tells Excel to ignore Alt+F4 (the percent sign represents the Alt key):

Application.OnKey “%{F4}”, “”
Free download pdf