557 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Advanced controls in userforms with VBA macros Part eight 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.
This blog is part of our Excel VBA tutorial. Wise Owl's main business is running Excel, VBA and other courses for businesses.
|
An option button (sometimes called a radio button) allows you to choose between mutually exclusive possibilities:
You can have no milk, skimmed or normal milk, but you must choose one and one only possibility.
I'd avoid option buttons like the plague: a combo box is easier to create, and makes it much easier to add additional options that you've forgotten (semi-skimmed milk, anyone?). I've only included this page because some clients or managers will ask for option buttons!
In a group of option buttons, each one can be True or False, but at any one time only one button can be True:
Here the OptNone option button has a value of False initially. As soon as you select None as your milk choice - and thereby set the value of OptNone to True - the value of the other option buttons in the frame will automatically be set to False.
There are two ways to add option buttons: either as part of a frame, or as part of an option group. To add option buttons to a frame, first add the frame:
![]() |
![]() |
Click on the Frame tool ... | ... to add a frame |
You can then click on the option button tool to add buttons to the frame one by one:
![]() |
![]() |
Click on the option button ... | ... then click inside the frame |
It's a good idea to rename your option buttons once you've added them: I've called the ones above OptNone, OptSkimmed and OptNormal.
Option buttons don't have to belong to a group. Instead, you can add the option buttons to a form, then set their GroupName property:
Select the option buttons to change their GroupName property (or you can set it for each button individually).
Type in a value for the GroupName property (here we've set it to grpMilkChoices). Because no other option buttons use the same group name, the buttons will automatically now cancel each other when clicked.
There's no great advantage that I can think of to using a frame or using grouped buttons - maybe it's down to personal preference!
The code to manage option buttons can be a bit messy. Here's our example code:
'transfer milk option into worksheet
'variable to hold name of milk choice
Dim MilkChoice As String
'determine which button was chosen
If Me.optNone Then
MilkChoice = "None"
ElseIf Me.optSkimmed Then
MilkChoice = "Skimmed"
ElseIf Me.optNormal Then
MilkChoice = "Normal"
Else
MilkChoice = "N/A2"
End If
'transfer this into worksheet
ActiveCell.Offset(0, 4).Value = MilkChoice
Adding another option button is not only going to mess up the form, but also require more lines of code. Like I say - combo boxes are easier!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.