Visual Basic for Applications/Block Illegal Characters

Summary
'''This VBA example can be run in any of the commonly used Microsoft Office applications. The examples show how to exclude illegal characters from text boxes while the keys are being pressed. In each of the two examples, the insertion point simply remains where it was when an illegal character is entered; accepted characters appear in the usual way. Both examples make use of the TextBox_KeyPress event; then first accepting only integers and a few other characters, and the second only letters and some supporting characters.'''

For the UserForm1 Module

 * Make sure that the ShowModal property of the user form is set to False, to allow normal code working and study with an open form.
 * Copy the code below into the UserForm1 module, and type text into the boxes to see the results.
 * Setting the parameter KeyAscii in code to the ASCI value of a non-printing character (eg: zero), prevents the display of the character that originated the event. Otherwise, KeyAscii is found to contain the ASCI value of the key that was pressed, and it is displayed.
 * The KeyAscii value can be changed to any asci value, and provided that it is a printing character, it will be displayed.
 * In each case code is added to restrict the permitted positions in the text.  For example, a minus sign is valid only at the start of a number, and a hyphen is never expected at the start of a word.