XL: Macros to Disable Keyboard Input

ID: Q101567


The information in this article applies to:
  • Microsoft Excel for Windows, versions 4.0, 5.0
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows
  • Microsoft Excel for the Macintosh, versions 4.0, 5.0


SUMMARY

In Microsoft Excel, to disable all the keys on the keyboard, set the Microsoft Visual Basic for Applications DataEntryMode property or use the Microsoft Excel 4.0 macro ENTER.DATA() function.

Note that you can also disable keys on the keyboard with the ON.KEY() function, but this function will not allow you to disable keys such as the SPACEBAR or the PLUS SIGN (+).


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

Sample Visual Basic Procedure

This code turns off keyboard entry for the active sheet only. To turn keyboard entry back on, press CTRL+D.

   ' Macro to turn off keyboard entry.
   Sub KeyboardOff()
 
      ' Sets CTRL+D to activate KeyboardOn.
      Application.OnKey "^d", "KeyboardOn"
      Application.DataEntryMode = True
 
   End Sub
 
   ' Macro to restore keyboard entry.
   Sub KeyboardOn()
 
      Application.DataEntryMode = False
 
   End Sub 

Microsoft Excel 4.0 Example

The following macro example turns on Data Entry mode. The keystroke CTRL+D initiates the second macro which turns Data Entry mode off. The macro assumes that all cells on the worksheet are locked.

   A1: =ON.KEY("^d","DataEntryOff")
   A2: =ENTER.DATA(2)
   A3: =RETURN()
   A4:
   A5: DataEntryOff
   A6: =ENTER.DATA(FALSE)
   A7: =RETURN() 
The function ENTER.DATA() turns on Data Entry mode and allows you to enter data into unlocked cells. This function does not allow you to enter data into locked cells on the worksheet.

NOTE: You can turn off Data Entry mode by pressing the ESC key. If you use the number 2 for the logical argument, you will not be able to turn off Data Entry mode with the ESC key.


REFERENCES

For more information about the DataEntryMode property, click the Index tab in Microsoft Excel version 7.0 and 97 Help, type the following text

DataEntryMode
and then double-click the selected text to go to the "DataEntryMode property" topic. For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
Q163435 VBA: Programming Resources for Visual Basic for Applications
"Function Reference," version 4.0, pages 132-133, 302-303

Additional query words: 4.0a 4.00a 5.0 5.0a 5.00a 5.0c 5.00c 7.00a 8.00 XL97 freeze

Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA
Version : MACINTOSH:4.0,5.0; WINDOWS:4.0,5.0,7.0
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.