+ Reply to Thread
Results 1 to 8 of 8

User Form with option buttons

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    User Form with option buttons

    On my user form, I have a few option buttons within different frames. I use [If Then] to allocate the selection to a cell range but the cell is only updated when I click the OK button on the user form:

    If OptionLocal Then Range("Import") = "Local"
        If OptionImport Then Range("Import") = "Import"
    If OptionBMW Then Range("Category") = "BMW"
    If OptionMercedes Then Range("Category") = "Mercedes"

    What code can I use for option buttons so the cell address changes as I change my selection on the user form?

    Thank you.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: User Form with option buttons

    You could put the code in the Click events of the buttons.

    Or have a separate sub that runs the existing code and call that from the Click event of each option button.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: User Form with option buttons

    I am not sure what I put after the = sign for the option buttons as what I have tried does not work?

    Private Sub OptionBMW_Click()
       Range("CategoryChosen").Value = 
    End Sub
    Thanks in advance

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Can you attach a workbook with an example form with option buttons?

  5. #5
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: User Form with option buttons

    Thank you.
    Attached Files Attached Files

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: User Form with option buttons

    Try this.
    Option Explicit
    
    Private Sub OptionBMW_Click()
        GetVehicle
    End Sub
    
    Private Sub OptionMercedes_Click()
        GetVehicle
    End Sub
    
    Private Sub OptionVolvo_Click()
        GetVehicle
    End Sub
    
    Private Sub OptionCost_Click()
        GetMetric
    End Sub
    
    Private Sub OptionUnits_Click()
        GetMetric
    End Sub
    
    Private Sub UserForm_Initialize()
        Me.ActivityList.ListIndex = 1
        Me.MonthList.ListIndex = 2
    End Sub
    Private Sub ActivityList_Click()
        Range("ActivityChosen").Value = Me.ActivityList.Value
    End Sub
    Private Sub MonthList_Click()
        Range("MonthChosen").Value = Me.MonthList.Value
    End Sub
    Private Sub CancelButton_Click()
        Unload UserForm1
    End Sub
    
    Sub GetVehicle()
    Dim strVehicle As String
    
        Select Case True
            Case OptionBMW
                strVehicle = "BMW"
            Case OptionVolvo
                strVehicle = "Volvo"
            Case OptionMercedes
                strVehicle = "Mercedes"
        End Select
    
        Range("VehicleChosen").Value = strVehicle
    End Sub
    
    Sub GetMetric()
    Dim strMetric As String
    
        Select Case True
            Case OptionUnits
                strMetric = "Unit"
            Case OptionCost
                strMetric = "Cost"
        End Select
    
        Range("Metric").Value = strMetric
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: User Form with option buttons

    Thank you very much for your time and effort. I have tested your code on my test file and it works perfectly. I will now work through the code to understand it and then apply it to my real model.

  8. #8
    Forum Contributor
    Join Date
    06-11-2009
    Location
    Cape Town
    MS-Off Ver
    Microsoft 365
    Posts
    854

    Re: User Form with option buttons

    I have used your code and added some extra options buttons and everything works perfectly. Thank you very much.

+ 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