Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

ADVANCED CONTROLS IN USERFORMS WITH VBA MACROS

Part seven 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)
  7. Check Boxes (this article)
  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

Check Boxes

A check box can have two possible values: True or False.

Check box

You either do want a biscuit, or you don't!

 

Adding a CheckBox

To add a checkbox to a form, use the CheckBox tool:

The checkbox tool

Click on the CheckBox tool to add it to a form.

 

Formatting a CheckBox

A checkbox comes with associated text, which can appear either to the right or left of the box itself:

The Alignment property of a checkbox

The Alignment property doesn't align the text within its container, but instead specified whether it should appear to the right or left of the tick box.

 

To show how this works, consider a check box with the following properties:

Property Value
Alignment 0 - frmAlignmentLeft
TextAlign 1 - frmTextAlignRight

Here's what this would look like:

A checkbox with right alignment

The text appears to the right of the tick box, but is left aligned.

 

Picking Up on a Checkbox's Value

The Value property of a checkbox tells you whether it has been ticked or not:

'put biscuit choice in column 6 of current block

ActiveCell.Offset(0, 5).Value = IIf(Me.chkBiscuit, "Yes", "No")

This is short-hand for:

'put biscuit choice in column 6 of current block

If Me.chkBiscuit = True Then

ActiveCell.Offset(0, 5).Value = "Yes"

Else

ActiveCell.Offset(0, 5).Value = "No"

End If

If you set a checkbox's Tristate property to True (not the default) it can - as the name suggests - have 3 possible values, according to whether it is ticked, unticked or left null (in which case it would appear shaded).  I can't help feeling you'd be better off with a combobox instead, however!

ADVANCED CONTROLS IN USERFORMS WITH VBA MACROS

Part seven 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)
  7. Check Boxes (this article)
  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.