A CommandButton on an Excel VBA UserForm is a clickable control that runs a macro when the user clicks it. You add it from the Toolbox, set its properties in the Properties window, and write code in its Click event to make it do something.
What Is a UserForm CommandButton?
A UserForm is a custom dialog box you build in the Visual Basic Editor (VBE). A CommandButton is one of the standard controls you can place on that form—it appears as a labeled button. When a user clicks it, VBA triggers the button’s Click event, and any code you’ve written in that event procedure runs.
CommandButtons are the primary way users interact with a UserForm. They can submit data, cancel an action, open another form, or close the form. Each button has properties like Caption (the text shown), Name (the identifier used in code), Enabled, and Visible.
How to Add and Configure a CommandButton
To insert a CommandButton, open the VBE (press Alt+F11 on Windows, Option+F11 on Mac), then go to Insert > UserForm. The Toolbox appears; if it doesn’t, click View > Toolbox. Click the CommandButton icon (a small rectangle with “AB” on it) and then draw it on the form.
Once placed, the Properties window (press F4 to show it) lets you set:
- Name – e.g.,
cmdSubmit. Use a meaningful prefix likecmdfor command buttons. - Caption – the text the user sees, e.g., “Submit”.
- Default – if
True, pressing Enter triggers this button. - Cancel – if
True, pressing Esc triggers it. - Enabled – set to
Falseto gray it out. - Visible – set to
Falseto hide it.
Writing Code for the Click Event
Double-click the button in the VBE to open its Click event procedure. You then write the code that should run when the button is clicked. For example, to close the form:
Private Sub cmdClose_Click()
Unload Me
End Sub
To collect a value from a text box and put it into a cell:
Private Sub cmdOK_Click()
' Assumes a TextBox named txtName and a cell A1 on Sheet1
Sheet1.Range("A1").Value = txtName.Value
Unload Me
End Sub
Save a copy of your workbook before running any macro—macros cannot be undone with Ctrl+Z.
Common Properties and Their Uses
Beyond Caption and Name, several properties affect behavior:
- ControlTipText – shows a tooltip when the mouse hovers.
- TabIndex – determines the order when the user presses Tab.
- Font – changes the text style.
- BackColor / ForeColor – change button appearance.
- Picture – lets you place an image on the button.
- TakeFocusOnClick – if
False, the button doesn’t steal focus when clicked (useful for toolbar-like buttons).
How to Choose Between a CommandButton and Other Controls
CommandButtons are for single actions. If you need a toggle, consider a CheckBox or ToggleButton. For a list of options, use a ListBox or ComboBox. If you want to trigger an action while typing, a TextBox has events like Change. Use a CommandButton when the user must explicitly click to confirm an action—like submitting a form or opening a file.
Common Problems and How to Fix Them
Several issues frequently arise:
- Button does nothing – Check that the code is in the button’s
Clickevent, not in a standard module. Double-click the button to verify. - Button is grayed out – The
Enabledproperty isFalse. Set it toTruein code or in the Properties window. - Button not visible – The
Visibleproperty isFalse, or the form is hidden. SetVisible = True. - Click event not firing – The form might be modal and another control has focus. Ensure the button is not covered by another control.
- Error “Object doesn’t support this property” – You’re using a property that doesn’t exist on CommandButton; check the spelling.
Comparing CommandButton with Other Button Types
Excel has other button-like elements. Form Controls and ActiveX Controls on worksheets are not on UserForms. On a UserForm, the CommandButton is the standard. A ToggleButton stays pressed when clicked; a SpinButton is for incrementing values. The CommandButton is best for one-off actions. If you need a button that stays in a “pressed” state, use a ToggleButton.
Alternatives to a CommandButton
If you need to trigger code without a visible button, consider:
- Worksheet buttons – Form Controls or ActiveX CommandButtons placed on a sheet.
- Keyboard shortcuts – Assign a macro to a key combination.
- Ribbon buttons – Customize the Ribbon with a button that runs your macro.
- Quick Access Toolbar – Add a macro button there.
These are useful when you don’t need a full dialog box, just a quick action.
Frequently Asked Questions
How do I make a CommandButton run a macro when clicked?
Double-click the button in the VBE to open its Click event, then write the code you want to execute. The code runs when the user clicks the button during runtime.
Can I change the button’s caption during runtime?
Yes, in code you can set cmdButton.Caption = "New Text" to update the displayed text.
How do I disable a CommandButton until a condition is met?
Set cmdButton.Enabled = False initially, then in the relevant event (like a TextBox’s Change event) set it to True when the condition is satisfied.
Why is my CommandButton not showing on the UserForm?
Check the Visible property—it must be True. Also ensure the button is not positioned outside the form’s visible area; drag it back into view.
How do I make the Enter key trigger a specific button?
Set that button’s Default property to True. Similarly, set Cancel to True for the button that should respond to the Esc key.
Can I have multiple CommandButtons on one UserForm?
Yes, you can add as many as you need. Each has its own name and Click event, so you can handle them separately.
Related
- Speed and Intersection Safety Cameras: How They Work
- Traffic Light Reaction Time: How Long It Takes
- How to Export FileZilla Settings (Sites & Passwords)
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- 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…
- Excel VBA: How to Open a Binary FileYes. In Excel VBA on Windows, open a file with Open ... For Binary Access Read and use…
- Excel VBA Macro to Filter Data: Code and ExamplesTo filter a worksheet with an Excel VBA macro, define the full range including its header row, then…
- Excel Value Macro: Convert Formulas to ValuesTo convert selected Excel formulas into their current results with a VBA macro, assign a range’s Value2 property…
- VBA Find the Last Used Cell When Filters Are OnTo find the last used cell in Excel VBA while a filter is active, search the worksheet with…
- Vba loop through rows?Learn how to loop through Excel rows with VBA, find the last row, handle conditions and deletions, and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.