Userform Radio Buttons- how code this ?

Pat_The_Bat

Board Regular
Joined
Jul 12, 2018
Messages
83
I've designed a simple userform with 3 radio dial buttons, an OK and a Cancel button.

How do I write the code so that when a user chooses OptionButton 1, then hits OK, the operation I want to perform is performed.

I feel like I should be getting this more easily! Appreciate help and guidance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
All you need is something like this:

Code:
   If OptionButton1 Then
' do some code
End If
 
Upvote 0
Thanks Rory,

I guess the part I am struggling with is that the user clicks the radio button, then when user clicks "ok", nothing happens.

So I guess what I'm trying to do is let user choose which option, then click "OK", and once OK gets clicked, that's when I need that code to execute. Thoughts?
 
Last edited:
Upvote 0
If you double click the OK button you should see something like this code stub, where CommandButton1 will be replaced with the name of the OK button control.
Code:
Private Sub CommandButton1_Click()

End Sub

This is where the code you want to be executed when the button is clicked should go.

For example, here's Rory's code added to the sub.
Code:
Private Sub CommandButton1_Click()
    If OptionButton1.Value Then
        ' do some code
    End If
End Sub
 
Upvote 0
I've made some progress here. I went to the code associated with the OK button, and there I started with Me.hide, then put

If OptionButton1.Value Then
' do some code
End If
 
Upvote 0
Try this:
Code:
Private Sub CommandButton3_Click()
'Modified  5/16/2019  9:10:52 PM  EDT
If OptionButton1.Value = True Then
MsgBox "Yep Option Button One is True"
End If
End Sub
 
Upvote 0
I've made some progress here. I went to the code associated with the OK button, and there I started with Me.hide, then put

If OptionButton1.Value Then
' do some code
End

Why Me.Hide?


Just:

Code:
Private Sub CommandButton1_Click()

If OptionButton1.Value Then
        Msgbox "operation 1"
    End 

If OptionButton2.Value Then
        Msgbox "operation 2"
    End If

If OptionButton3.Value Then
        Msgbox "operation 3"
    End If
End sub

Just select one option and press ok button.
 
Upvote 0
I always like using True and False with Option Buttons

So if your going to use:
If OptionButton1.Value Then


What are you going to use if it's false
 
Upvote 0
I always like using True and False with Option Buttons

So if your going to use:
If OptionButton1.Value Then


What are you going to use if it's false

Optionbutton.value
or
optionbutton.value = true
is the same

Not Optionbutton.value
Or
Optionbutton.value = false
Is the same
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,144
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top