Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

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 article)
  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 | 3 comments

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.

 

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 article)
  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.

Comments on this blog

This blog has 3 comments:

Comment added by VBAGuy on 02 October 2012 at 02:41 GMT
I was looking at the option boxes and got stuck when you went to the me.option..... I keep getting an invalid use of me error message. I am not sure if you defined it somewhere up above or what. I would use a combo box but I have been trying to figure out option boxes just to know how to use them if I did and I think they look cleaner
Reply from Andy Brown (blog author)
You're probably trying to use Me to refer to the current form, but doing it within a code module (in which case Me would actually refer to the active Excel workbook).
 
Comment added by BernieG on 24 December 2012 at 13:20 GMT
Cheers Andy
Have been struggling with userform multipages & yours is the best article I've seen by miles.
I only program now and again and like to construct my code in the same manner as you.
Putting the bouncers on the door making sure the correct information is in the right place.

Cheers
          Bernie
  
 
Comment added by kevinzimm on 27 December 2012 at 07:42 GMT
I'm not finding the calendar in my list of additional controls?  Thoughts?
Reply from Andy Brown (blog author)
Choose Tools from the menu in VBA, choose Additional Controls...   and tick the Calendar Control 11.0 (or whatever number appears after it) in the list.  This should reference the required calendar control.  While you're there, you could have a look at the hundreds of other add-ins!

All content copyright Wise Owl Business Solutions Ltd 2013. All rights reserved.