+ Reply to Thread
Results 1 to 5 of 5

How to execute a macro when user selects radiobutton

Hybrid View

  1. #1
    Registered User
    Join Date
    07-31-2013
    Location
    Chennai, India
    MS-Off Ver
    Excel 2010
    Posts
    64

    How to execute a macro when user selects radiobutton

    Hi,

    I have a button named "Convert" that performs some formatting. I have few radio buttons below in which the user can select his option.

    When user selects radiobutton1 and click the Convert button radiobutton1 function should execute. similarly if radiobutton 2 is selected i want that function to excute on clicking the convert button.

    I tried assigning macro to radiobutton it excutes on radio button click.

    But i want it to execute only after clicking on the convert button.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: How to execute a macro when user selects radiobutton

    Try something like the following code (tested using Excel 2003):

    In the UserForm1 code module:
    Private Sub CommandButtonConvert_Click()
      Call ProcessConvert
    End Sub
    In an ordinary code module:
    Sub ProcessConvert()
    
      'NOTE: The '= True' is NOT NEEDED, but I like to use it for readability.
    
      If UserForm1.OptionButton1 = True Then
        Call RadioButton1ConvertRoutine
      ElseIf UserForm1.OptionButton2 = True Then
        Call RadioButton2ConvertRoutine
      End If
    
    End Sub
    
    Sub RadioButton1ConvertRoutine()
      MsgBox "RadioButton1ConvertRoutine() stub"
    End Sub
    
    Sub RadioButton2ConvertRoutine()
      MsgBox "RadioButton2ConvertRoutine() stub"
    End Sub

  3. #3
    Registered User
    Join Date
    07-31-2013
    Location
    Chennai, India
    MS-Off Ver
    Excel 2010
    Posts
    64

    Re: How to execute a macro when user selects radiobutton

    Thanks. Is it possible to user the optionbutton on cells without the userform

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: How to execute a macro when user selects radiobutton

    No, you can't use 'Option Buttons' on cells without the Userform to the best of my knowledge.

    However, You can SIMULATE 'Option Buttons' by using Shapes, and assign macros to the shapes.
    See the attached workbook written and tested using Excel 2003, which I expanded to 3 Simulated Option Buttons, and uses code very
    similar to what existed with the Userform.


    In the 'ThisWorkbook' module an optional initialization can be used:
    Private Sub Workbook_Open()
      Call SimulatedOptionButtonInitialization
    End Sub
    I arbitrarily divided the rest of the code into two modules:
    ModSimulatedOptionButtons - which simulates the Option Buttons and interfaces with the Worksheet.
    ModConvert - which contains code similar the original.

    Code for ModSimulatedOptionButtons follows:
    Option Explicit
    
    Private SimulatedOptionButton1 As Boolean
    Private SimulatedOptionButton2 As Boolean
    Private SimulatedOptionButton3 As Boolean
    
    Private Const SimulatedOptionButton1_CELL = "E9"
    Private Const SimulatedOptionButton2_CELL = "E10"
    Private Const SimulatedOptionButton3_CELL = "E11"
    
    '''''''''''''''''''''''''''''''''''''''''''''''
    'Get Routines - available for use by outside routines
    '''''''''''''''''''''''''''''''''''''''''''''''
    Function GetSimulatedOptionButton1Value() As Boolean
      GetSimulatedOptionButton1Value = SimulatedOptionButton1
    End Function
    
    Function GetSimulatedOptionButton2Value() As Boolean
      GetSimulatedOptionButton2Value = SimulatedOptionButton2
    End Function
    
    Function GetSimulatedOptionButton3Value() As Boolean
      GetSimulatedOptionButton3Value = SimulatedOptionButton3
    End Function
    
    
    '''''''''''''''''''''''''''''''''''''''''''''''
    'Set Routines
    '''''''''''''''''''''''''''''''''''''''''''''''
    Private Sub SetAllSimulatedOptionButtonsFalse()
      SimulatedOptionButton1 = False
      SimulatedOptionButton2 = False
      SimulatedOptionButton3 = False
      Range(SimulatedOptionButton1_CELL) = False
      Range(SimulatedOptionButton2_CELL) = False
      Range(SimulatedOptionButton3_CELL) = False
    End Sub
    
    
    Sub SetSimulatedOptionButton1()
      If SimulatedOptionButton1 = True Then
        'do nothing - if true - already selected
      Else
        Call SetAllSimulatedOptionButtonsFalse
        SimulatedOptionButton1 = True
        Range(SimulatedOptionButton1_CELL) = True
      End If
      
    End Sub
    Sub SetSimulatedOptionButton2()
      If SimulatedOptionButton2 = True Then
        'do nothing - if true - already selected
      Else
        Call SetAllSimulatedOptionButtonsFalse
        SimulatedOptionButton2 = True
        Range(SimulatedOptionButton2_CELL) = True
      End If
      
    End Sub
    Sub SetSimulatedOptionButton3()
      If SimulatedOptionButton3 = True Then
        'do nothing - if true - already selected
      Else
        Call SetAllSimulatedOptionButtonsFalse
        SimulatedOptionButton3 = True
        Range(SimulatedOptionButton3_CELL) = True
      End If
      
    End Sub
    
    
    '''''''''''''''''''''''''''''''''''''''''''''''
    'Other Routines
    '''''''''''''''''''''''''''''''''''''''''''''''
    Sub SimulatedOptionButtonInitialization()
      'Excel VBA initializes all Booleans to 'False'
      'If you want to initialize a value to 'True' this
      'routine should contain the 'True' value for the one
      'Simulated Option button in the GROUP you want true.
      'This Function is called from the Workbook_Open()
      'code area in 'ThisWorkbook'.
    
      Range(SimulatedOptionButton1_CELL) = False
      Range(SimulatedOptionButton2_CELL) = False
      Range(SimulatedOptionButton3_CELL) = False
      Call SetSimulatedOptionButton2
    End Sub
    Code for ModConvert follows:
    Sub ProcessConvert()
    
      'NOTE: The '= True' is NOT NEEDED, but I like to use it for readability.
      'With 3 choices - 'Select Case' is probably better than an 'If'
    
      If GetSimulatedOptionButton1Value() = True Then
        Call RadioButton1ConvertRoutine
      ElseIf GetSimulatedOptionButton2Value() = True Then
        Call RadioButton2ConvertRoutine
      ElseIf GetSimulatedOptionButton3Value() = True Then
        Call RadioButton3ConvertRoutine
      End If
    
    End Sub
    
    Sub RadioButton1ConvertRoutine()
      MsgBox "RadioButton1ConvertRoutine() stub"
    End Sub
    
    Sub RadioButton2ConvertRoutine()
      MsgBox "RadioButton2ConvertRoutine() stub"
    End Sub
    
    Sub RadioButton3ConvertRoutine()
      MsgBox "RadioButton3ConvertRoutine() stub"
    End Sub

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: How to execute a macro when user selects radiobutton

    I added another sheet to the Workbook, which has 'Shapes' that have the
    look and feel of UserForm Option Buttons. See the attached workbook.

    I did this by adding two Oval 'Shapes' that look like circles for each
    simulated Option Button required. The name of each 'outer circle' is irrelevant.
    The name of each 'inner (filled in) circle' is IMPORTANT because it is used by
    the 'Event Handler' (Macro) to make the 'inner circle' visible or NOT visible.
    I assigned the same Macro to each inner/outer circle pair.

    I had to rename a couple of the 'Shapes' from their original names which looked
    like "Oval 8" to "SimulatedOptionButton1B". The following code is included in
    'ModZTest' in the workbook.

    First I had to identify all the 'Shape' names:
    Sub LookAtShapeCharacteristics()
      'This was used to look through the 'Shapes' to find
      'the 'Shapes' I was interested in (3 small filled in circles).
      '
      'The Original Names were 'Oval 7', 'Oval 8', and 'Oval 9'.
    
      Dim Sh As Object
    
      For Each Sh In ActiveSheet.Shapes
        Debug.Print
        Debug.Print "Shape Name:    " & Sh.Name
        Debug.Print "Shape Type:    " & Sh.Type
        Debug.Print "Shape Id:      " & Sh.ID
        Debug.Print "Shape Locked:  " & Sh.Locked
        Debug.Print "Shape Visible: " & Sh.Visible
        Debug.Print "Shape Top:     " & Sh.Top
        Debug.Print "Shape Left:    " & Sh.Left
        Debug.Print "Shape Height:  " & Sh.Height
        Debug.Print "Shape Width:   " & Sh.Width
        Debug.Print "Shape ZOrderPosition:  " & Sh.ZOrderPosition
        Debug.Print "Shape AlternativeText: " & Sh.AlternativeText
        
      Next Sh
      
    End Sub

    Then I had to rename the 'inner circle' 'Shapes'.
    Sub ModifyShapes()
      'This was used to modify the Names of the 3 small filled in circles
      'to 'SimulatedOptionButton1B', 'SimulatedOptionButton2B' and 'SimulatedOptionButton3B'.
      '
      'The names of the LARGE CIRCLES are irrelevant.
      '
      'The changes can be verified by running LookAtShapeCharacteristics() again.
      
      ActiveSheet.Shapes("Oval 7").Name = "SimulatedOptionButton1B"
      ActiveSheet.Shapes("Oval 8").Name = "SimulatedOptionButton2B"
      ActiveSheet.Shapes("Oval 9").Name = "SimulatedOptionButton3B"
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Quick and Dirty : User selects from dropdown (sht1)...macro populates data from (sht2)
    By Joecruz749 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-26-2013, 05:06 PM
  2. Macro that Selects Specific Excel Worksheets to a PDF file & Saves to the User's Desktop
    By Excel Wet Behind Ear in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-31-2013, 01:15 PM
  3. [SOLVED] Start Macro after user selects a choice from a pick list
    By Mathew in forum Excel General
    Replies: 2
    Last Post: 08-17-2006, 10:30 AM
  4. Record the time a user selects a macro
    By marcusmccown in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-22-2006, 10:31 PM
  5. pause macro until user selects correct range
    By JCIrish in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-18-2006, 05:30 PM

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