The ScrollBar property in VBA UserForms is a control you add from the Toolbox that lets users select a numeric value by dragging a thumb or clicking arrows; you set its range with .Min and .Max, and read the current value from .Value. This article explains how to add, configure, and code a ScrollBar control, with working examples and common pitfalls.
What Is a ScrollBar Control in a UserForm?
A ScrollBar is an ActiveX control (not a form control) available in the VBA Toolbox when you are designing a UserForm. It displays a horizontal or vertical track with a thumb that the user drags, plus arrow buttons at each end. Unlike a TextBox or Label, a ScrollBar does not show its value directly; you typically link it to a Label or TextBox to display the selected number.
In the VBA editor, you insert a UserForm by right-clicking the project in the Project Explorer and choosing Insert > UserForm. The Toolbox appears automatically; if it does not, click View > Toolbox. The ScrollBar icon looks like a small vertical scroll bar. Click it, then click on the form to place the control.
Key Properties of the ScrollBar Control
After adding a ScrollBar, select it and press F4 to open the Properties window. The most important properties are:
- Name – the identifier you use in code (e.g.,
ScrollBar1). - Min – the minimum value the ScrollBar can return (default 0).
- Max – the maximum value (default 32767).
- Value – the current position; set this to initialize the control.
- SmallChange – the amount changed when the user clicks an arrow (default 1).
- LargeChange – the amount changed when the user clicks the track (default 1).
- Orientation – sets horizontal or vertical (0 = horizontal, 1 = vertical).
- Enabled – set to False to make it non-interactive.
- Visible – set to False to hide it.
Note that Min and Max are Long integers; you cannot use decimal values directly. To get decimals, multiply your range by a factor (see the example below).
How to Write Code for the ScrollBar
The ScrollBar fires a Change event whenever the user moves the thumb, clicks an arrow, or clicks the track. You write code in that event to update other controls. Here is a simple example that ties a ScrollBar to a Label:
' Assume a UserForm with ScrollBar1 and Label1
Private Sub ScrollBar1_Change()
Label1.Caption = ScrollBar1.Value
End Sub
When the form runs, dragging the ScrollBar updates the label with the current value. To set an initial value, put this in the UserForm’s Initialize event:
Private Sub UserForm_Initialize()
ScrollBar1.Min = 0
ScrollBar1.Max = 100
ScrollBar1.Value = 50
Label1.Caption = ScrollBar1.Value
End Sub
If you need decimal values, use a scale factor. For example, to represent values from 0 to 10 in 0.1 steps, set Min=0, Max=100, and divide by 10 in the display:
Private Sub ScrollBar1_Change()
Label1.Caption = Format(ScrollBar1.Value / 10, "0.0")
End Sub
Choosing Between ScrollBar and Other Controls
VBA offers several controls for numeric input. The ScrollBar is best when you want a compact, visual way to select a value without typing. Alternatives include:
- SpinButton – similar but has no track; only arrows. Good for small ranges.
- TextBox – allows direct typing but requires validation.
- ComboBox – for a fixed set of choices, not a continuous range.
- Slider – not available in the standard VBA Toolbox; you would need an additional ActiveX control.
Use a ScrollBar when the range is large (e.g., 0–1000) and you want quick coarse adjustments. For fine adjustments, set SmallChange to a small value and LargeChange to a larger one.
Common Problems and How to Fix Them
- ScrollBar does not show a value – It never displays its value; you must link it to a Label or TextBox.
- Value jumps unexpectedly – Check
SmallChangeandLargeChange; if they are 0, the control may behave oddly. Set them to positive integers. - Can’t set Min greater than Max – VBA enforces that Min <= Max; set Min first, then Max.
- Decimal values not working – As noted, use a scale factor because Min and Max are integers.
- ScrollBar not responding – Ensure
Enabledis True and the form is not in design mode. - Event not firing – Make sure you placed the code in the correct event (e.g.,
ChangenotClick).
Example: Building a Temperature Selector
Let’s build a small form that lets the user pick a temperature from 0 to 100°C in 1-degree steps. Add a ScrollBar, a Label, and a CommandButton. In the UserForm’s Initialize event:
Private Sub UserForm_Initialize()
ScrollBar1.Min = 0
ScrollBar1.Max = 100
ScrollBar1.Value = 20
Label1.Caption = "20 °C"
End Sub
In the ScrollBar’s Change event:
Private Sub ScrollBar1_Change()
Label1.Caption = ScrollBar1.Value & " °C"
End Sub
When the user clicks the OK button, you can read the value:
Private Sub CommandButton1_Click()
MsgBox "Selected temperature: " & ScrollBar1.Value
End Sub
This example works exactly as written in Excel for Microsoft 365 on Windows. On a Mac, the VBA editor and UserForm controls behave the same; the only difference is that some keyboard shortcuts differ (e.g., F4 may open the Properties window, but on Mac you might use ⌘+F4 or the View menu).
Frequently Asked Questions
How do I make a ScrollBar show a percentage?
Set Min to 0 and Max to 100, then in the Change event divide the value by 100 and format as percent: Label1.Caption = Format(ScrollBar1.Value / 100, "0%").
Can I use a ScrollBar to scroll a multipage or listbox?
No, the ScrollBar control is for numeric input. To scroll a ListBox or MultiPage, use the built-in scroll properties of those controls (e.g., ListBox1.TopIndex) or a separate vertical ScrollBar that you code to move items.
Why is my ScrollBar value stuck at 0?
Check that you haven’t set Min to 0 and Max to 0, which makes the control non-functional. Also, ensure you are reading the Value property, not the ListIndex (which doesn’t exist for ScrollBar).
How do I make the ScrollBar horizontal?
Set the Orientation property to 0 (horizontal) or 1 (vertical) in the Properties window, or in code: ScrollBar1.Orientation = 0.
Can I use a ScrollBar with negative values?
Yes, set Min to a negative number (e.g., -100) and Max to a positive number. The control will work normally.
Safety and Best Practices
When using ScrollBars in a UserForm, always validate the value before using it in calculations, especially if the user can leave the ScrollBar at an extreme. For example, if you use the value as a divisor, check for zero. Also, remember that macros cannot be undone with Ctrl+Z, so always save a copy of your workbook before running any code that modifies data. If you are distributing the form, test it thoroughly on the intended Excel version, as some properties may behave differently in older versions.
Related
- Shortcut to Switch Off Computer: Fastest Ways
- Generate Random Number in Unity: C# Script Guide
- Free Data Entry Software for Windows 10: 7 Real Options
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Find and Replace in a Range: Code and TipsTo find and replace text only within a chosen Excel range, use VBA’s Range.Replace method and call it…
- Excel VBA UserForm Parameters: Pass and Return ValuesTo pass parameters to an Excel VBA UserForm, create an instance of the form, call a public setup…
- MS Access Split String: VBA Syntax and ExamplesYes. In Microsoft Access, VBA’s Split function breaks a string into a zero-based array of pieces using a…
- VBA Get Character From String: Mid, Loops & TipsTo get one character from a VBA string by position, use Mid$(text, position, 1). For example, Mid$("Excel VBA",…
- VBA InputBox Default Value: Set and Use ItIn VBA, set an InputBox’s default value with its third argument: InputBox(prompt, title, default). For example, InputBox("Enter a…
- Excel VBA Sort Custom Order: How to Set It UpTo sort rows in a custom sequence with Excel VBA, add a sort field with the CustomOrder argument,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.