InputBox in VBA has no password or masking option: it displays whatever the user types. In Excel for Microsoft 365 on Windows, use a UserForm with a TextBox whose PasswordChar property is set to a masking character; this hides the characters on screen, but does not encrypt or secure the value.
Why the built-in InputBox cannot mask text
The VBA InputBox function is a quick way to ask for a value. Its arguments let you specify a prompt, title, default response, and—in some forms of the function—where the dialog appears. They do not include a password flag, a character-mask setting, or a way to change the dialog’s text field into a password field. For example, a prompt that says “Enter password” changes only the instruction. The typed characters remain visible.
Excel also provides Application.InputBox, which can restrict the kind of response returned—for instance, text, a number, or a range reference. Its Type argument is about the input’s data type, not how the characters are displayed. It does not add password masking either. This distinction matters because examples that pass a different Type value may look like a special password mode, but they are not.
There is no setting you can add to a call such as InputBox to make its built-in dialog conceal typing. If you need visible masking, the usual VBA solution is to create a small UserForm. If you only need a prompt and are comfortable with visible input, the built-in function remains simpler.
How a masked UserForm works
A VBA UserForm is a custom dialog that you design in the Visual Basic Editor. Add a TextBox for the response and set that control’s PasswordChar property to a character such as an asterisk. As the user types, the TextBox displays the chosen character instead of the actual letters. The TextBox still holds the original text in its Value property, so your VBA code can read it after the user selects OK.
The masking character is a display treatment, not a transformation of the value. If the user types Maple42, the form might show eight asterisks, while the control’s value remains Maple42. The program can use that value for a comparison or pass it to another procedure. A person who can inspect or control the running VBA project may still be able to access it.
For a one-off prompt, this is more setup than InputBox. The extra work is worthwhile when the input should not be plainly visible to someone looking at the screen, or when you want explicit OK and Cancel buttons and a clear distinction between cancelling and submitting an empty response.
Create the form and set its masking character
In desktop Excel, open the Visual Basic Editor and choose Insert > UserForm. If the Toolbox is not visible, show it from the editor’s View menu. Add a TextBox, an OK CommandButton, and a Cancel CommandButton. Use the Properties window to set their names to txtPassword, cmdOK, and cmdCancel. Set the form’s (Name) property to frmPassword. Names matter because the sample code refers to these controls by name.
Select txtPassword and set its PasswordChar property to * in the Properties window. You can set the property in code instead, as the example does during form initialization. Add a prompt label such as “Enter password” if it helps explain the request. A label is only a label; it does not affect whether input is hidden.
- Give the buttons clear captions, such as OK and Cancel, and make sure their names match the code.
- Do not put a real password in the TextBox’s
Valueproperty at design time. Initialize it as blank. - Choose a mask character that is easy to distinguish visually. The mask does not have to be an asterisk, but it should not reveal clues about the underlying characters.
Excel for the web does not run VBA macros or show VBA UserForms. A desktop version of Excel is required to use this approach. The Visual Basic Editor and some interface details differ on Mac, though desktop Excel for Mac supports VBA; check the controls and properties available in that installation if you are building a form for Mac users.
Example code for the UserForm
Double-click the form in the Visual Basic Editor and place this code in the form’s code module. It initializes a blank field, masks the displayed characters, and records whether the user cancelled. The QueryClose event handles the form’s close button so that closing the window is treated as cancellation.
Option Explicit
Public WasCancelled As Boolean
Public PasswordValue As String
Private Sub UserForm_Initialize()
' Start with an empty value and treat the form as cancelled until OK is clicked.
Me.WasCancelled = True
Me.PasswordValue = vbNullString
Me.txtPassword.Value = vbNullString
Me.txtPassword.PasswordChar = "*"
End Sub
Private Sub cmdOK_Click()
' Save the actual text, not the masking characters shown on screen.
Me.PasswordValue = CStr(Me.txtPassword.Value)
Me.WasCancelled = False
Me.Hide
End Sub
Private Sub cmdCancel_Click()
' Discard the response when the user chooses Cancel.
Me.WasCancelled = True
Me.PasswordValue = vbNullString
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
' Treat the window close button as Cancel.
If CloseMode = vbFormControlMenu Then
Me.WasCancelled = True
Me.PasswordValue = vbNullString
End If
End Sub
The comparison in UserForm_QueryClose checks whether the user closed the form with its window control. When the calling code unloads the form after a button click, that is a different close mode. The form is hidden after a button click so the calling procedure can read its public fields before unloading it.
Show the form and handle the response
Put this second procedure in a standard module. It creates an instance of the form, shows it modally, then checks cancellation separately from an empty response. Before running any macro, save a copy of the workbook: VBA actions cannot be undone with Ctrl+Z. Use Alt+F11 to open the Visual Basic Editor on Windows; the Mac shortcut is generally Option+F11, sometimes with Fn depending on the keyboard.
Option Explicit
Public Sub AskForPassword()
Dim dlg As frmPassword
Dim entered As String
Set dlg = New frmPassword
dlg.Show vbModal
If dlg.WasCancelled Then
Unload dlg
Exit Sub
End If
entered = dlg.PasswordValue
Unload dlg
If Len(entered) = 0 Then
MsgBox "No value was entered.", vbInformation
Exit Sub
End If
' Use entered here; do not display or store it unnecessarily.
MsgBox "A value was entered.", vbInformation
End Sub
This example intentionally does not show the entered text in a message box. Replace the final demonstration with the task that genuinely needs the value, such as checking it against an approved source. A real comparison needs a defined, appropriate way to obtain the expected value; putting a secret directly in a macro is not a secure way to protect it.
The code treats cancellation and an empty submission differently. A user can click OK without typing anything, so the value is empty but the form was not cancelled. That distinction is useful for validation: you might ask the user to try again when the response is blank, while allowing Cancel to end the operation. If your task permits blank input, remove or change the empty-value check.
Masking is not password security
Password masking addresses shoulder-surfing: someone nearby cannot read the characters merely by looking at the dialog while they are typed. It does not encrypt the text, verify a person’s identity, or stop someone with access to the workbook or VBA project from examining what the macro does. The actual value exists in memory and is available to the VBA code that reads the TextBox.
Do not treat worksheet protection, workbook structure protection, or a password on a VBA project as a substitute for secure authentication. These features serve different purposes and should not be assumed to protect a stored credential against a determined person. In particular, embedding a real password in VBA source code creates a value that can be exposed to someone who can inspect the project or its files.
If the workbook must restrict who can open or use sensitive information, consider the right security boundary rather than relying on a masked prompt. Excel’s file encryption for a password-protected workbook is different from protecting a sheet against edits, but it is still not a complete identity or access-management system for every workflow. For shared business data, use an approved authentication or access-control service and follow your organization’s security requirements. Avoid writing passwords to cells, logs, message boxes, or temporary files unless a reviewed design specifically requires it.
Common problems and practical checks
- The characters are still visible: Confirm that you set
PasswordCharon the TextBox, not on the form or a label. Check that the code is using the same control name as the one on the form. - The code says a control or form is missing: Compare the Properties window’s (Name) values with
frmPassword,txtPassword,cmdOK, andcmdCancel. A button’s displayed Caption is not the same as its control name. - Cancel behaves like OK: Confirm that the Cancel button has a
cmdCancel_Clickevent and that it setsWasCancelledto True. Also retain the QueryClose handler if clicking the window close button should cancel. - The form opens but the macro continues immediately: Show it with
vbModal. A modal form pauses the calling procedure until the form is hidden or unloaded. - The macro cannot run: Confirm that macros are allowed under your organization’s policy and that the file is saved in a macro-enabled format such as
.xlsm. Excel for the web cannot execute this VBA code.
When testing, use a harmless sample string rather than a real credential. Confirm that the mask appears, that OK returns the typed value to the code, and that both the Cancel button and the window close control end the request without treating it as an accepted response. If you change the form’s control names, update the code references as well.
Choosing between InputBox and a UserForm
Use the built-in InputBox when the response is not secret and you want the shortest possible prompt. It is convenient for a category, filename, or other ordinary value, but its simplicity comes with limited control over layout and response handling. Use a UserForm when masking, explicit buttons, validation, or a more tailored interaction matters. The trade-off is that a form needs to be designed, maintained, and checked across the desktop Excel versions your users rely on.
Do not build a UserForm solely to make a password seem protected. If the requirement is only to stop casual viewing during entry, the masking property is appropriate. If the requirement is to authenticate users or protect a valuable secret, a masked field is only a small part of the interface and is not the security mechanism. Choose the tool based on what must be protected, where the value will go, and who can access the workbook and its code.
Frequently Asked Questions
Can I add a password parameter to VBA InputBox?
No. The built-in VBA InputBox function has no password-mask parameter. Use a UserForm TextBox with PasswordChar when the typed characters need to be hidden on screen.
Does Application.InputBox support password masking?
No. Its Type argument controls the kind of response, such as text or a range reference. It does not change how the characters appear while the user types.
Can I use a bullet instead of an asterisk?
You can choose a mask character through the TextBox’s PasswordChar property, subject to the characters supported by the control and font. The selected symbol replaces the visible characters; it does not alter the value stored in the TextBox.
Does a masked UserForm encrypt the password?
No. Masking conceals characters in the form’s display only. The VBA code can still read the original value, so do not assume that the form protects credentials from someone who can inspect the workbook, code, or running process.
Will this 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 solution, or choose a different supported tool if the workbook must be used in a browser.
Related
- Power Query Editor: How to Split Text into Multiple Columns
- Power Query Union Tables: How to Append Queries
- Data Validation: Definition, Rules, and Examples
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel Macro Recorder: Keyboard Shortcuts and SetupYes. In Excel for Microsoft 365 on Windows, record a macro from Developer > Code > Record Macro…
- OpenOffice and Excel VBA: What Works and What Doesn’tApache OpenOffice Calc can open many Excel workbooks, but it does not reliably run Microsoft Excel VBA macros.…
- Filter a Dictionary in Excel VBA: Practical ExamplesExcel VBA has no built-in Filter method for a Scripting.Dictionary. The usual approach is to loop through the…
- Excel VBA Format Cell as Percentage: Full GuideTo format a cell as a percentage in Excel VBA, set the cell’s NumberFormat property to "0.00%" (or…
- VBA Code to Update a PivotTable in Excel AutomaticallyTo update one PivotTable in Excel for Microsoft 365 on Windows, call its RefreshTable method from a VBA…
- Excel VBA: Find Unique Entries in a ColumnTo find distinct entries in an Excel column with VBA, loop through the data, add each value only…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.