A VBA UserForm spin button (SpinButton control) is a small up/down arrow pair that lets users increase or decrease a numeric value by clicking, and you add it from the Toolbox, set its Min, Max, and Increment properties, then write a Change event to send the value to a text box or cell. This article shows you exactly how to insert one, wire it up, and troubleshoot the most common problems.
What a SpinButton Does in a UserForm
The SpinButton is one of the ActiveX controls you can place on a UserForm in the Visual Basic Editor (VBE). It has no visible text of its own—just two arrows. Its job is to change a number, usually shown in an adjacent TextBox or Label, so the user doesn’t have to type. Common uses are quantity selectors, date pickers (with a date format), or stepping through row numbers in a list.
Unlike a ScrollBar, which has a thumb you drag, a SpinButton only responds to clicks on the arrows. Each click changes its Value property by the amount in its Increment property (default is 1). The value is always a whole number if you set Increment to 1, but you can set it to 0.1 or 5 for different steps.
How to Add a SpinButton to a UserForm
- Press
Alt+F11to open the VBE (on Mac,Fn+Option+F11). - In the Project Explorer, right-click your workbook name and choose Insert > UserForm.
- If the Toolbox isn’t visible, click View > Toolbox.
- Click the SpinButton icon (it looks like a small up/down arrow) in the Toolbox, then click and drag on the form to draw it.
- Add a TextBox next to it (click the TextBox icon, then draw).
That’s the basic placement. The control appears with a default name like SpinButton1 and TextBox1. You can rename them in the Properties window (F4) by changing the (Name) property—for example, spnQty and txtQty.
Setting Min, Max, and Increment
Before writing any code, set the spin button’s range. In the Properties window, look for:
- Min – the lowest allowed value (default 0).
- Max – the highest allowed value (default 100).
- Increment – how much each click changes the value (default 1).
- Value – the starting value (default 0).
For example, to make a quantity selector from 1 to 50 in steps of 1, set Min=1, Max=50, Increment=1, Value=1. If you want decimal steps like 0.5, set Increment=0.5, but note that the Value will still be a number—you just need to format it in the text box.
Writing the Change Event to Update a TextBox
The most important part is the Change event. This fires every time the value changes (when a user clicks an arrow). Double-click the SpinButton in the VBE to open its code module, and you’ll see a stub like:
Private Sub SpinButton1_Change()
End Sub
Inside, assign the spin button’s value to the TextBox’s Text property. For a whole number, use:
Private Sub SpinButton1_Change()
' Send the spin value to the text box as a string
TextBox1.Text = SpinButton1.Value
End Sub
If you’re using decimal increments, you might want to format it:
Private Sub SpinButton1_Change()
' Show two decimal places
TextBox1.Text = Format(SpinButton1.Value, "0.00")
End Sub
You also need to handle the reverse: when the user types a number in the TextBox, the spin button should update. Put this in the TextBox’s Change or Exit event. The Exit event is safer because it fires only when the user leaves the box, avoiding conflicts while typing:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update the spin button if the typed value is a number
If IsNumeric(TextBox1.Text) Then
SpinButton1.Value = CLng(TextBox1.Text)
End If
End Sub
Note: if the typed value is outside Min/Max, VBA will raise an error. You can clamp it with Application.WorksheetFunction.Max or just let it error and handle it.
Using the SpinButton Value in Your Code
Once the user picks a value, you typically read it when a command button is clicked. For example, to put the value into cell A1:
Private Sub CommandButton1_Click()
' Write the spin button value to cell A1
Range("A1").Value = SpinButton1.Value
End Sub
If you want the spin button to control a cell directly (without a TextBox), you can skip the TextBox and just read the value. But most users expect to see the number, so the TextBox is standard.
One common pattern is to use the spin button to scroll through rows of data. For instance, if you have a list of names in column A, you can set Max to the last row and display the name in a Label:
Private Sub SpinButton1_Change()
' Show the name from column A at the spin value row
Label1.Caption = Range("A" & SpinButton1.Value).Value
End Sub
Common Problems and Fixes
- Spin button doesn’t change the TextBox – You forgot to write the Change event. Double-click the spin button and add the assignment line.
- Value jumps to Min or Max when typing – This happens because the TextBox’s Change event fires on every keystroke. Use the Exit event instead, or check if the text is numeric before assigning.
- Error “Invalid property value” when setting Value – You tried to set a value outside Min/Max. Always check the range first.
- SpinButton doesn’t appear in Toolbox – Right-click the Toolbox and choose Additional Controls, then check Microsoft Forms 2.0 SpinButton. It’s usually there by default.
- Spin button works in design mode but not when running – Make sure the form is shown with
UserForm1.Showand not just in the VBE. Also, ensure the form isn’t modal-locked by a breakpoint.
Alternatives: ScrollBar and Numeric Up-Down
If you need a larger range or a draggable thumb, use a ScrollBar instead. It has the same Min/Max/Value properties but also a LargeChange (for clicking the track) and SmallChange (for arrow clicks). The event handling is identical.
Excel doesn’t have a native “numeric up-down” control like some other languages, but the SpinButton is exactly that. If you’re using Excel 365, you could also use the Form Controls version (from the Developer tab, Insert > Form Controls > Spin Button), which works directly on a worksheet without VBA—just right-click to set cell link. That’s simpler if you don’t need a UserForm.
Frequently Asked Questions
How do I make the spin button show the value in a cell without a text box?
Set the spin button’s LinkedCell property (only for Form Controls on a worksheet) or use the Change event to write to a cell: Range("B1").Value = SpinButton1.Value. For UserForm spin buttons, there’s no LinkedCell property, so you must use code.
Can I use a spin button to change a date?
Yes. Set Min to a serial date (e.g., DateSerial(2025,1,1)) and Max to another, with Increment=1. Then in the Change event, format the value as a date: TextBox1.Text = Format(SpinButton1.Value, "mm/dd/yyyy"). Each click adds one day.
Why does my spin button sometimes skip numbers?
If you set Increment to something like 0.3, floating-point rounding can cause slight jumps. Use whole numbers or set Increment to a decimal that divides evenly (like 0.5). Also, ensure you’re not accidentally changing the value in code elsewhere.
How do I disable the spin button when a condition is met?
Set its Enabled property to False in code, e.g., SpinButton1.Enabled = False. You can do this in the UserForm’s Initialize event or based on a cell value.
Can I use a spin button on a worksheet without VBA?
Yes, from the Developer tab > Insert > Form Controls > Spin Button (the one with the up/down arrows). Right-click it, choose Format Control, and set the Minimum, Maximum, Incremental Change, and Cell Link. No code needed.
Final Tips for Reliable SpinButton Code
Always save a copy of your workbook before running macros that modify it—macros cannot be undone with Ctrl+Z. When you’re done, you can also use the UserForm_Initialize event to set the initial value and text box:
Private Sub UserForm_Initialize()
' Set starting values
SpinButton1.Min = 1
SpinButton1.Max = 100
SpinButton1.Value = 50
TextBox1.Text = SpinButton1.Value
End Sub
This ensures the form opens with a consistent state. If you’re using a Mac, the VBE shortcuts differ (Alt is Option), but the code is identical. For Excel for the web, UserForms are not supported—use a worksheet Form Control instead.
Related
- Generate Random Number NumPy: How to Use np.random
- Data Entry Computer Skills: A Practical Guide
- Power automate format string?
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Loop Through a Range: Examples and Best PracticesTo loop through cells in Excel VBA, use For Each cell In worksheet.Range("A1:A10").Cells and place the action you…
- How to Assign a Macro to a Toggle Button in Excel 365In Excel for Microsoft 365 on Windows, an ActiveX Toggle Button runs VBA from its Click event: insert…
- Excel VBA: Save a Worksheet as a New File SafelyTo save one Excel worksheet as a separate file with VBA, copy it to a new workbook with…
- Excel VBA: Open a Worksheet by Name or IndexTo open a worksheet in Excel VBA, activate it with a statement such as ThisWorkbook.Worksheets("Data").Activate; replace Data with…
- How to Create a Macro in Excel (Step-by-Step)Creating a macro in Excel lets you record a series of actions—like formatting, data entry, or calculations—and replay…
- VBA InputBox Integer: Get and Validate Whole NumbersVBA’s standard InputBox returns text, not an Integer. In Excel VBA, use Application.InputBox with Type:=1 to request a…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.