Macro Run when OptionButton enabled

nidhipatel

Board Regular
Joined
Feb 11, 2017
Messages
52
my userform contians

OptionButton1
OptionButton2
Command Button
macro 1
macro2

i need when OptionButton1 is Enabled and click on commandButton than Macro1 is run Or when OptionBUtton2 is Enabled and click on CommandButton than macro2 is run
how it works
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
This works with active-x contols
Code goes in sheet module (right-click sheet tab\ View Code \ paste code in window on right)

Code:
Private Sub CommandButton1_Click()
    If OptionButton1.Value = True Then
        Call macro1
    ElseIf OptionButton2.Value = True Then
        Call macro2
    End If
End Sub

Private Sub OptionButton1_Click()
    If OptionButton1.Value = True Then OptionButton2.Value = False
End Sub

Private Sub OptionButton2_Click()
    If OptionButton2.Value = True Then OptionButton1.Value = False
End Sub

[COLOR=#008080][I]Place macro1 & 2 codes into either sheet or standard modules[/I][/COLOR]
Sub macro1()
    MsgBox "A"
End Sub

 Sub macro2()
    MsgBox "B"
End Sub
 
Last edited:
Upvote 0
my userform contians

OptionButton1
OptionButton2
Command Button
macro 1
macro2

i need when OptionButton1 is Enabled and click on commandButton than Macro1 is run Or when OptionBUtton2 is Enabled and click on CommandButton than macro2 is run
how it works

By enabled I assume you mean user has selected the optionbutton if this is so then try

Code:
Private Sub CommandButton1_Click()
      If Me.OptionButton1.Value Then Macro1 Else Macro2
End Sub

and see if this does what you want

Dave
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,783
Members
449,049
Latest member
greyangel23

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