VBA option button control in Excel
Option button, as the name indicates, is a kind of button that enables the user to select any one option only.
This button, when used more than one in a form, allows the user to select only one.
This control is normally found in many web sites and other VB applications like,
Gender selection, yes or no and so on.
This button is often used in variety of excel application.
The optionbutton values are predefined and cannot be modified by the end user.
Option Button Groups
If in an application, if the user need to enter more than one option inputs, say male or female and yes or no,
then, optionbuttons are grouped together to act as two separate set of controls.
The Frame control is used to group option buttons, as shown below:
As seen from the above, there are now two groups of option buttons which are placed inside a frame.
The option button also has the same properties as that of other controls discussed earlier such as:
1.Caption
2.Value
3.Name
4.Font
5.Autosize
and many more.
The important event associated with that of option button is "Change" event.
This event is triggered whenver an option button options is changed.
The following example illustrates this
- Private Sub OptionButton1_Change()
- MsgBox "Change event triggered by selecting 'Yes' "
- End Sub
- Private Sub OptionButton2_Change()
- MsgBox "Change event triggered by selecting 'No' "
- End Sub
- Private Sub OptionButton3_Change()
- MsgBox "Change event triggered by selecting 'Male' "
- End Sub