Name

Application.OnKey(Key, [Procedure])

Synopsis

Assigns a macro to run when a key is pressed. Can also be used to disable built-in Excel key combinations.

Argument

Setting

Key

The key combination to assign. The character codes are the same as for SendKeys. See Chapter 3 for the SendKeys codes.

Procedure

The name of the macro to run. Setting to "" disables any built-in action for those keys; omitting this argument restores the built-in action.

The following code demonstrates how to reassign, disable, and restore a built-in key assignment:

Sub TestOnKey(  )
    ' Reassign Ctrl+C
   Application.OnKey "^c", "CopyMsg"
   ' Disable Ctrl+C
   'Application.OnKey "^c", ""
   ' Restore Ctrl+C
   ' Application.OnKey "^c"
End Sub
 
Sub CopyMsg(  )
    MsgBox "You can't copy right now."
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.