by Allen Wyatt
(last updated October 11, 2014)
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...Discover More
Macros are really helpful, but you may want to actually delete a macro from within another macro. This is not as easy as ...Discover More
When developing a macro that others may use, you might want to test it out to make sure it works properly if an ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.