by Allen Wyatt
(last updated October 9, 2019)
Andrew is writing a macro, and he wants to give users the opportunity to enter their password prior to permitting them to use certain functions provided by the macro. He is using the InputBox function, and wonders if there is a way to "strike out" whatever someone enters, so the password is kept private as it is typed. (This is done in many programs, where whatever is typed is replaced on-screen with asterisks or some other character.)
There is no direct way to do this using the InputBox function; it doesn't include the needed functionality. There are folks who have done it using API calls and the like, but that gets rather involved and—in all likelihood—beyond the scope of ExcelTips.
An easier approach is to create your own UserForm in VBA. The form can contain a TextBox, and the control includes a property you can set to function as a masking character when someone enters a password. If you display the property window for the TextBox control, you'll see a property named PasswordChar. Set this to whatever character you want used for the masking. For instance, you could put a single asterisk in the property.
When it comes time to check whether the user entered the correct password, then all you need to do is check the value in the TextBox control; it will be "clear" (unmasked), while the on-screen version remains masked. In other words, if someone enters "MyPass" as their password, then that is the value associated with the control itself. However, what shows on the screen is six asterisks (or whatever masking character you specified), one for each letter typed.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3120) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Excel allows you to use worksheet functions from within macros. This is helpful, especially when you are trying to ...Discover More
Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros, ...Discover More
Sometimes a macro command line can get very, very long. This can make it hard to understand when you look at it a month ...Discover More
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.