Macro Run when OptionButton enabled

nidhipatel

New Member
Joined
Feb 11, 2017
Messages
42
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
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,638
Office Version
365
Platform
Windows
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:

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,251
Office Version
2013
Platform
Windows
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
 

Forum statistics

Threads
1,081,574
Messages
5,359,704
Members
400,545
Latest member
Damntheman30

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top