Excel’s built-in VBA InputBox cannot hide a password as the user types; it displays the entered characters. To show dots or asterisks, create a VBA UserForm and set its TextBox control’s PasswordChar property. Masking only conceals characters on screen—it does not make a password-based workbook secure.
Why the built-in InputBox does not mask text
The VBA InputBox function is a simple prompt that returns what the user typed as a string. It has arguments for the prompt, title, default value, position, and help information, but no argument for a password mode or masking character. The separate Application.InputBox method can constrain the kind of response, such as text or a number, but it also does not provide a password-masking option.
This is an important distinction: changing the prompt to say “Password” does not hide the response. The user can still see the characters while typing. If the prompt is for a password that should not be visible to people nearby, use a form control with masking rather than either built-in prompt.
Use InputBox for a visible response
A normal InputBox can still be appropriate when the response is not confidential—for example, asking for a report name or a non-sensitive lookup code. With Application.InputBox, the return value is a Variant. When the user clicks Cancel, it returns the Boolean value False; when the user enters text, the response is a string. The following example asks for text and distinguishes cancellation from an entered response:
Option Explicit
Public Sub AskForVisibleText()
Dim answer As Variant
' Type 2 requests text; Cancel returns the Boolean value False.
answer = Application.InputBox(Prompt:="Enter a report label:", _
Title:="Report label", Type:=2)
If VarType(answer) = vbBoolean And answer = False Then
MsgBox "You cancelled the prompt."
Exit Sub
End If
MsgBox "You entered: " & CStr(answer)
End Sub
This example deliberately echoes the answer, which is suitable only for ordinary text. Do not use it to handle a real password. The simpler VBA InputBox function returns an empty string both when the user cancels and when they submit a blank response, so it cannot reliably distinguish those two actions.
Create a masked password UserForm
A VBA UserForm gives you a TextBox whose PasswordChar property replaces typed characters visually. In Excel for Microsoft 365 on Windows, open the VBA editor with Alt+F11, choose Insert > UserForm, then add a TextBox and two CommandButtons from the Toolbox. In the Properties window, set the form’s (Name) to frmPassword, the TextBox’s name to txtPassword, and the buttons’ names to cmdOK and cmdCancel. Give the buttons useful captions such as OK and Cancel.
Double-click the form to open its code window and add this code. It masks the characters and treats closing the form or clicking Cancel as cancellation:
Option Explicit
Public WasCancelled As Boolean
Public EnteredPassword As String
Private Sub UserForm_Initialize()
' Start in the cancelled state unless the user confirms.
Me.WasCancelled = True
Me.EnteredPassword = vbNullString
Me.txtPassword.Value = vbNullString
Me.txtPassword.PasswordChar = "*"
End Sub
Private Sub cmdOK_Click()
' Save the response in memory and close the prompt.
Me.EnteredPassword = CStr(Me.txtPassword.Value)
Me.WasCancelled = False
Me.Hide
End Sub
Private Sub cmdCancel_Click()
' Leave WasCancelled True and close the prompt.
Me.Hide
End Sub
The form must be shown modally so execution waits for the user’s choice. Put this procedure in a standard module, adjusting the form name only if you named it differently:
Option Explicit
Public Sub ShowPasswordPrompt()
Dim prompt As frmPassword
' Create a fresh form for this request.
Set prompt = New frmPassword
prompt.Show vbModal
If prompt.WasCancelled Then
MsgBox "No password was submitted."
Else
' Use prompt.EnteredPassword for your intended check here.
' Avoid displaying it or saving it in a worksheet.
MsgBox "A response was submitted."
End If
' Remove the form and its entered text from this object.
Unload prompt
Set prompt = Nothing
End Sub
Save a copy of the workbook before running a macro: VBA changes cannot be undone with Ctrl+Z. Save the working file as a macro-enabled workbook (.xlsm) if it needs to retain VBA. The example confirms that a response was submitted; it does not authenticate anyone. Add a comparison or authorization step only after deciding where a legitimate credential should come from and how it will be protected.
Handle cancellation and blank passwords deliberately
A robust prompt should define what happens for each outcome. In the form example, WasCancelled starts as True, and only clicking OK changes it to False. That lets the calling procedure tell cancellation apart from an intentionally blank entry. Decide whether a blank password is allowed; if not, validate it before accepting the form. For example, the OK button can keep the form open and display a message when the TextBox is empty.
Do not use a blank string alone as a cancellation signal. A user might deliberately submit an empty value, and the built-in InputBox function also uses an empty string for Cancel. Keeping a separate status value prevents those cases from being confused. If the application needs more than one kind of outcome, such as Cancel, blank, or accepted, represent those outcomes explicitly rather than inferring them from the text.
What password masking does—and does not—protect
PasswordChar is a visual privacy feature. It helps prevent someone looking at the screen from reading the characters as they are typed, but it does not encrypt the value or verify the user’s identity. Once entered, the value is available to the macro in memory. Other VBA code, debugging tools, or a person with access to the workbook and its code may be able to inspect how the prompt is used.
For that reason, avoid embedding a real password directly in VBA source code, storing it in a visible worksheet cell, or writing it to a log. Locking a VBA project for viewing can deter casual inspection, but it is not a substitute for strong security. Worksheet protection and workbook-structure protection are also intended to restrict editing, not to keep confidential data secret from someone who has the file.
If the aim is to protect the workbook file itself, use file encryption rather than a homemade VBA password check. In Excel for Microsoft 365 on Windows, the encryption command is under File > Info > Protect Workbook > Encrypt with Password. Keep the password safe: losing it can prevent access to the encrypted file. A macro prompt is better suited to a workflow gate or a lightweight convenience check than to protecting sensitive information.
Common problems and practical fixes
- The form appears but characters are visible. Confirm that the control is a TextBox and that the code sets
txtPassword.PasswordChar. Check the control’s actual name in the Properties window; a name mismatch can cause a compile error or leave the wrong control unchanged. - The macro continues before the user responds. Show the form with
vbModal. A modeless form lets the calling code continue while the prompt remains open, which is usually not the intended password-entry behavior. - Cancel is treated as an accepted blank response. Use a separate Boolean such as
WasCancelled, initialized to True and changed only by the OK button. Decide separately whether blank entries are allowed. - The VBA editor says the form or control is not defined. Check the UserForm and control names, and make sure the code is in the correct form or standard module. If code refers to a control on the form, use that form’s control name exactly.
- The macro will not run in the browser. Excel for the web can open and edit many workbook contents, but it cannot run VBA macros. Open the file in desktop Excel to use VBA. Macro security settings may also require the user to enable trusted macros.
Windows, Mac, and Excel for the web
The steps above target desktop Excel for Microsoft 365 on Windows. On Windows, Alt+F11 opens the Visual Basic Editor. On a Mac, the editor is available in desktop Excel, but the keyboard shortcut can depend on the keyboard and function-key settings; use the Visual Basic Editor command from Excel’s Tools or Developer menu if the shortcut does not work. The Ribbon and VBA controls can differ between platforms, and Windows ActiveX controls are not supported in the same way on Mac. Use standard VBA UserForm controls and check the form on the actual platform where it will run.
Excel for the web does not execute VBA, so a UserForm password prompt cannot run there. If a workbook must work in a browser, VBA is not the right mechanism; consider an approved web-based workflow or an organization’s identity and access controls instead. Do not assume a macro prompt creates access control for a file that can also be opened outside the macro-enabled desktop workflow.
Alternatives to a VBA password prompt
Choose the method based on what you are trying to protect, not just on whether the prompt displays dots:
- Prevent shoulder-surfing: Use a UserForm TextBox with
PasswordChar. This masks typing but does not secure the workbook or credential. - Protect a workbook’s contents at rest: Use Excel’s file-encryption feature and manage the password carefully. This is different from protecting a sheet against accidental edits.
- Control access for a team: Store the file in a managed location and use the organization’s access permissions or identity system. A shared hard-coded VBA password is difficult to revoke safely.
- Protect a form or application workflow: Use an approved system designed for authentication if the data is sensitive or access needs to be audited. Excel macros are not a replacement for a secure authentication service.
These approaches solve different problems. A masked prompt addresses what is visible during typing; file encryption addresses unauthorized opening of an encrypted workbook; permissions address who can access a shared file. Combining them may be appropriate, but asterisks alone should never be described as encryption.
Frequently Asked Questions
Can I make VBA InputBox show asterisks?
No. Neither the VBA InputBox function nor Application.InputBox has a password-mask setting. Use a UserForm TextBox and set its PasswordChar property instead.
Can I use a password character other than an asterisk?
Yes. Set PasswordChar to the single character you want displayed, such as a bullet where the control and font support it. The character changes only the on-screen appearance; it does not change how the entered value is stored.
Does a masked UserForm make my Excel file secure?
No. It conceals typed characters from view, but it does not encrypt the workbook or securely authenticate a user. Use workbook file encryption or managed access controls when the goal is to protect confidential information.
Can an Excel password prompt distinguish Cancel from a blank entry?
A UserForm can, if you track cancellation separately from the TextBox value. The VBA InputBox function returns an empty string for both Cancel and a blank response; Application.InputBox returns Boolean False on Cancel, so inspect the Variant’s type when using a text prompt.
Will a VBA password prompt work in Excel for the web?
No. Excel for the web does not run VBA macros or display VBA UserForms. Use desktop Excel for a VBA workflow, or choose a browser-compatible access method if users need to work online.
Related
- Excel Spreadsheet Not Responding? Causes and Fixes
- Excel Suddenly Not Responding? Causes and Fixes
- Excel Spreadsheet Grouping Rows: How to Create and Use It
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: How to Break Out of an Endless LoopTo stop a running Excel VBA loop on Windows, press Ctrl+Break (or Ctrl+Pause on keyboards with that key),…
- Excel VBA: Get the Day of the Week from a Date ValueIn Excel VBA, use Weekday to get a date’s weekday number and WeekdayName to turn that number into…
- VBA MsgBox If: Check a User’s Choice with an If StatementIn VBA, use MsgBox inside an If statement to show a prompt and run different code according to…
- VBA Range Properties: Values, Formatting, and NavigationIn Excel VBA, a Range property either returns information about cells or another range object, or changes a…
- Excel VBA Error 91: Why an Object Variable Is Not SetExcel VBA Run-time error 91 means your code tried to use an object variable that does not currently…
- Excel VBA Get Current Workbook Name: Code and TipsTo get the name of the workbook currently active in Excel VBA, use ActiveWorkbook.Name. For example, assign Set…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.