+ Reply to Thread
Results 1 to 3 of 3

UserForm Option Button selection to determine which macro to run.

Hybrid View

strud UserForm Option Button... 05-09-2013, 07:23 AM
Tinbendr Re: UserForm Option Button... 05-09-2013, 08:28 AM
strud Re: UserForm Option Button... 05-09-2013, 08:53 AM
  1. #1
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    UserForm Option Button selection to determine which macro to run.

    Hi excel experts,

    I am new to VBA and have a really limited knowledge so please bare with me.

    I am trying to have option buttons on my user form determine which macro to run. There are three options (call them option1, option2, option3) and three corresponding macros (lets call them macro1,

    I have found online this template code:

    Userform.hide
    '------------------------------
    if (Userform.Opt_Macro1.value) then 
    '(run Macro associated with Macro 1) 
    elseif (Userform.Opt_Macro2.value) then 
    '(run Macro associated with Macro 2) 
    elseif (Userform.Opt_Macro3.value) then 
    '(run Macro associated with Macro 3)
    end if 
    '------------------------------
    Unload Userform
    I am not sure how to make it work for my example.

    I have assigned the options separate names (option1, option2, option3) and a group name for all (OptionAll)

    My Userform is named AddDataForm.

    So I have:

    Private Sub CommandButton2_Click()
    UserForm.Hide
    '------------------------------
    If (UserForm.Opt_Option1.Value) Then
    Run (Macro1)
    ElseIf (UserForm.Opt_Option2.Value) Then
    Run (Macro2)
    ElseIf (UserForm.Opt_Option3.Value) Then
    Run (Macro3)
    End If
    '------------------------------
    Unload UserForm
    End Sub

    Where am I going wrong here??


    Thanks in advance...

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: UserForm Option Button selection to determine which macro to run.

    The same thing only different.
    Private Sub CommandButton2_Click()
    Select Case True
    
    Case Me.OptionButton1
    MyMacro1
    
    Case Me.OptionButton2
    MyMacro2
    
    Case Me.OptionButton3
    MyMacro3
    
    End Select
    End Sub
    Sub MyMacro1()
    MsgBox "Now running Macro1"
    End Sub
    
    Sub MyMacro2()
    MsgBox "Now running Macro2"
    End Sub
    
    Sub MyMacro3()
    MsgBox "Now running Macro3"
    End Sub
    David
    (*) Reputation points appreciated.

  3. #3
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: UserForm Option Button selection to determine which macro to run.

    Thanks for the swift response.

    That was exactly what I was after and works perfectly.

    Massive thanks again!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1