How do I link commandbuttons to combobox selection?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ERG1982
    New Member
    • Dec 2018
    • 1

    How do I link commandbuttons to combobox selection?

    Hi all,

    I need to combine the action of a command button to be dependent on the selection in the Combo Box in order to fill the cell with the selection that the combo box refers to.

    For instance, if I have three A columns,

    A2: Shoes
    A3: Socks
    A4: Gloves

    And two rows in B and C:

    B1: Video
    C1: Article

    I have a userform where my command buttons are "Started" and "Published" .

    In my userform, the ComboBox lists Shoes, Socks, Gloves

    Clicking Started or Published would place an "S" or a "P" in a cell. I want the cell that is filled with an S or P to be dependent on my ComboBox selection.

    So if I choose Socks and clicked Started, an S would appear in B3. If I click Shoes in the ComboBox, then click the same Started command button, an "S" would appear in B2.

    Do you know what coding I should use to make this happen, and if there's a shortcut or trick if I have a long list of columns?



    Thanks!!
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Code:
    Private Sub CommandButton1_Click()
        a = "You clicked on " + UserForm1.ComboBox1.Value + "which is option number: " + Str(UserForm1.ComboBox1.ListIndex)
        MsgBox a
    End Sub
    the ListIndex starts counting from 0.

    Comment

    • SioSio
      Contributor
      • Dec 2019
      • 272

      #3
      Luuk writes tips.
      In your writing, I don't know what would go in column C, but I assumed that when "Published" was pressed, "P" would go in
      Code:
      Private Sub CommandButton1_Click()
      With ComboBox1
      Cells(.ListIndex + 2, 2).Value = "S"
      End With
      End Sub
      
      Private Sub CommandButton2_Click()
      With ComboBox1
      Cells(.ListIndex + 2, 3).Value = "P"
      End With
      End Sub

      Comment

      Working...