REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

OptionButton is one of the UserForm control. You can select and drag OptionButton on the UserForm. This Control is used to select only one selection from multiple selections within a group. When we select an OptionButton_Control on the user form in a group immediately it will de select all other OptionButtons in the same group. It has value property. It indicates whether the OptionButton_Control is selected or not. When we select Option Button the value is ‘True’. And when it de selects the value is ‘False’. Option Button is used on the UserForm. You can see how it works and more details about Option Button Control on the UserForm in the following chapter.

VBA OptionButton_Control on the UserForm

Please find more details about VBA ActiveX Option Button Control on the UserForm.

    1. Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
    2. Go To Insert Menu, Click UserForm. Please find the screenshot for the same.

Excel VBA UserForm CheckBox

    1. Drag the Frame and two Option Button controls on the Userform from the Toolbox. Please find the below screenshot for the same.

OptionButton Excel VBA ActiveX Control55

    1. Select Frame and change its properties as follows.

Caption: Gender

    1. Select first OptionButton and change its properties as mentioned below.

Caption: Male

    1. Double click on first OptionButton control, now you will see the following code in the VBA Editor window.
Private Sub OptionButton1_Click()

End Sub
    1. Now add the following statement to the above procedure.
Private Sub OptionButton1_Click()
    MsgBox "Selected Gender is Male"
End Sub
    1. Now, Select second OptionButton and change its properties as follows.

Caption: FeMale

    1. Double click on second OptionButton control, now you will see the following code in the VBA Editor window.
Private Sub OptionButton2_Click()

End Sub
    1. Now add the following statement to the above procedure.
Private Sub OptionButton2_Click()
    MsgBox "Selected Gender is FeMale"
End Sub
    1. You can see the following outputs. It is as shown in the following screen shot.

Output1: When we select ‘Male’ Option Button

OptionButton Excel VBA ActiveX Control6

Output2: When we select ‘FeMale’ Option Button

OptionButton Excel VBA ActiveX Control5

Add dynamic OptionButton_Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic Option Button control on the UserForm.

    1. Add Option Button and CommandButton on the userform from the toolbox.
    2. Right click on the CommandButton, click properties
    3. Change the CommandButton caption to ‘Create_Option Button ’
    4. Double click on the CommandButton
    5. Now, it shows the following code.
Private Sub CommandButton1_Click()
 
End Sub
    1. Call the below procedure named ‘Add_Dynamic_OptionButton ’ and find the below procedure to run.
Private Sub CommandButton1_Click()
    Call Add_Dynamic_OptionButton 
End Sub

Procedure to call in the CommandButton:

Sub Add_Dynamic_OptionButton()
    'Add Dynamic OptionButton and assign it to object 'OpBtn'
    Set OpBtn = UserForm2.Controls.Add("Forms.OptionButton.1")
           
    'Assign OptionButton Name
    OpBtn.Caption = "Dynamic OptionButton"
           
    'OptionButton Position
    OpBtn.Left = 15
    OpBtn.Top = 10
End Sub
    1. Now, click F5 to run the macro, click ‘Create_Option Button ’ button to see the result.
    2. You can see the created dynamic Option Button which is shown in the following screen shot.

output:

OptionButton Excel VBA ActiveX Control1

Delete OptionButton_Control on the UserForm using VBA

Please find the below code, it will show you how to delete or remove the control on the UserForm. In the below example, its deleting the Option Button named ‘New Option Button’ which is on the UserForm named ‘UserForm4’. We can use Remove method to delete the controls which are created during run time. Controls which are created during design time cannot be deleted using this method. Please find the below example and screen shots for better understand.
Code 1: Adding control During Run Time

Private Sub CommandButton1_Click()
    'We can use Add method to add the new controls on run time
    Set lblBtn = Me.Controls.Add("Forms.Option Button.1")
    With lblBtn
        .Top = 20
        .Left = 20
        .Caption = "New Option Button"
        .Name = "lblNew1"
    End With
    MsgBox "New Option Button Added"
End Sub

Please find the below screen shot for your reference for the above macro and its output.
When we click on Add Command Button:

OptionButton Excel VBA ActiveX Control3

Code 1: Deleting or Removing Option Button_control which is created during run time.

Private Sub CommandButton2_Click()
    'We can use Remove method to delete the controls which are created during run time
    'Note: Controls which are created on design time cannot be deleted using this method
    Me.Controls.Remove ("lblNew1")
    MsgBox "New Option Button Deleted"
End Sub

Please find the below screen shot for your reference for the above macro and its output.
When we click on Delete Command Button:

OptionButton Excel VBA ActiveX Control4

Check if a OptionButton is Selected or Not Using VBA

Here is the below example. It will show you how to Check if a OptionButton is Selected or Not Using VBA.

Sub Chk_OptBtn_Selection()
    If OptionButton1.Value = True Then
        MsgBox "OptionButton has Selected"
    Else
        MsgBox "OptionButton has Not Selected"
    End If
End Sub

Difference Between OptionButton and CheckBox Control

Option Button and Checkbox both displays an option. Both are used to turn On or Off options. Difference between is, Option Button is used to select only one selection from multiple selections. Where as CheckBox is used to select multiple selections from the available list.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Last Updated: March 2, 2023