Microsoft KB Archive/58951

{|
 * width="100%"|

Using ON.KEY in Excel

 * }

Q58951

-

The information in this article applies to:


 * Microsoft Excel for Windows, versions 2.0, 2.01, 2.1, 2.10c, 2.10d, 3.0, 4.0, 5.0
 * Microsoft Excel for OS/2, versions 2.2, 3.0

-

SUMMARY
ON.KEY must be used in the following format:

  ON.KEY(&quot;&quot;,&quot;!&quot;)

For example:

  ON.KEY(&quot;%1&quot;,&quot;MACRO1.XLM!Restart&quot;)

The second argument must be an external reference to a defined name, not a cell reference. In the Windows versions of Microsoft Excel, the second argument must also be in quotation marks (the OS/2 version does not require the quotation marks).

Using a cell reference instead of a name (for example, &quot;MYMACROS.XLM!$B$32&quot; instead of &quot;MYMACROS.XLM!Return&quot;) or omitting the quotation marks in Windows versions will cause an error to be generated when the key sequence represented by &quot;key text&quot; is pressed. An error will also be generated if the filename contains any special characters, such as dollar signs.

MORE INFORMATION
A working example is as follows:

  A1: AutoSaver A2: =IF(ISERROR(Count>1),SET.NAME(&quot;Count&quot;,0)) A3: =ON.KEY(&quot;%1&quot;,&quot;MACRO1.XLM!Restart&quot;) A4: =RETURN A5: Restart A6: =SAVE.AS(&quot;WORK&quot;&SaveCount&&quot;.XLS&quot;) A7: =SET.NAME(&quot;SaveCount&quot;,SaveCount+1) A8: =RETURN

This macro, once executed, defines ALT+1 to automatically save the active document with the name WORK and a count number (for example, WORK27.XLS). Each time ALT+1 is pressed, the number increments itself, so a running set of work files is generated. This macro assumes that Restart has been defined on the macro sheet as a command macro at $A$5. If this macro is to be used over multiple work sessions, then the macro sheet should be saved at the end of each work session, which will save SaveCount so it won't need to be reset. Each time a new work session is begun, the macro must be reexecuted, or it should be defined as an AutoOpen macro.

One interesting use of ON.KEY in conjunction with SEND.KEYS allows a macro to send keystrokes to Microsoft Excel itself, just as if the user had typed in this information. For example:

. .  .   A14: =ON.KEY(&quot;{F13}&quot;,&quot;MACRO1.XLM!Continue&quot;) A15: =SEND.KEYS(&quot;{F2}^{RIGHT}{F9}~{F13}&quot;) A16: =RETURN A17: Continue A18: =ON.KEY(&quot;{F13}&quot;) .  ..

Cell A14 sets F13 [a useful designator for this purpose, since many keyboards don't have F13 keys, although Microsoft Excel will still recognize it when sent with SEND.KEYS] to run the Continue macro on sheet MACRO1.XLM. For this to work properly, Continue should be defined as a command macro at cell A17. Cell A15 sends the following key sequence to the keyboard buffer:

  &quot;F2 CTRL+RIGHT ARROW F9 ENTER F13&quot;

Cell A16 &quot;ends&quot; the macro, returning control to the user.

As soon as control returns to the user, Microsoft Excel checks the keyboard buffer. Since a key sequence is already present, Microsoft Excel begins interpreting the keys. In this example, F2 activates the formula bar. CTRL+RIGHT ARROW selects the entire formula in the formula bar. F9 converts the selected formula to a value. ENTER stores the value in the current cell. Then, F13 causes the Continue macro to begin execution, which is the continuation of our macro (cell A17) -- all as if the user had entered these keys from the keyboard.

The command in cell A18 is important. Without a second argument, the ON.KEY command &quot;undefines&quot; the key indicated, restoring it to its previous function. If this is not done, any time the user presses F13 (and a macro isn't running), the macro Continue will begin execution -- a jump to A17.

Note: the information above also applies to version 5.0 only when using version 4.0 macro language.