WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 482 reviews for our classroom and online training
Advanced controls in userforms with VBA macros
Part six of a nine-part series of blogs

The two previous parts of this mini-blog have shown how to draw forms and how to write code to handle form events. This final part shows how to add some of the more exotic controls to user forms, like combo boxes, list boxes, multipage controls, spinners and option buttons.

  1. Advanced Controls - Our Example
  2. Multipage Controls
  3. Combo boxes (and list boxes)
  4. Multiple Column Combo Boxes and Listboxes
  5. Multi-select ListBoxes
  6. SpinButtons (Spinners) (this blog)
  7. Check Boxes
  8. Option Buttons (Radio Buttons)
  9. The Calendar Control

This blog is part of our Excel VBA tutorial.  Wise Owl's main business is running Excel, VBA and other courses for businesses.

Posted by Andy Brown on 28 February 2012

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

SpinButtons (Spinners)

SpinButtons at first glance look clunky and unnecessary:

A spinner control

The SpinButton allows the user to change the number of sugars ordered.


However, the reason that they are so useful is that they allow you to prevent users typing in numbers directly:

Enabled property of textbox

The textbox for the number of sugars has the Enabled property set to False - this means that a user can't change the value directly.


If a user can't type in a number directly, you don't have to validate its data type or values.  This is a big advantage!

Creating a SpinButton

To create a SpinButton, just click on the tool and then click where you want it to go:

The SpinButton tool

Don't confuse the SpinButton tool with the scroll bar tool ...


A SpinButton usually sits next to the textbox whose value it will affect:

A SpinButton next to a textbox

Here the SpinButton will control the value in the txtSugar textbox.


That's the easy part.  When you double-click on the SpinButton to write code, you get an odd default event:

Private Sub spnSugar_Change()

End Sub

It's more likely that you would attach code to the SpinDown and SpinUp events.

Writing a Generic Procedure to Handle Spinning Up and Down

It's good programming practice to write a single procedure to handle both possible spin directions.  For our example, this could be:

Sub AddSugar(NumberAdd As Integer)

Dim CurrentSugars As Integer

'find out how many sugars currently

CurrentSugars = CInt(Me.txtSugar.Text)

CurrentSugars = CurrentSugars + NumberAdd

'don't allow negative sugars

txtSugar.Text = IIf(CurrentSugars > 0, CurrentSugars, 0)

End Sub

Here we'll pass in the number of sugars to add (this can be negative).  The routine makes sure that you don't set the number of sugars to be less than 0.

Attaching Code to the SpinButton Events

If you've written a generic procedure as above, you just need to call it twice - once when you spin up, and once when you spin down:

Private Sub spnSugar_SpinDown()

'add a sugar

AddSugar -1

End Sub

Private Sub spnSugar_SpinUp()

'remove a sugar, if any left to remove

AddSugar 1

End Sub

When you click on the down arrow of the SpinButton, for example, it will call the AddSugar routine to add -1 sugars.


This blog has 0 threads Add post