Testrada
Testrada

Reputation: 13

How to fill in a textbox based on user selection in a Combobox (VBA)

I am having troubles filling in a textbox in my userform based on selections of a combobox. Also i Have tried used .Caption, .Value, and .Text. i have attached an example below of what i am trying to do. Also i am not entirely understanding why the combobox has to be Me.combobox when the name is of the userform is "userform". if more clarifications is needed for anything just ask and ill try and help you out.

Private Sub UserForm_Initialize()

    'combobox for column thickness
    Me.ComboBox1.AddItem "W6 x 15"
    Me.ComboBox1.AddItem "W8 x 2"
    Me.ComboBox1.AddItem "W10 x 30 "

'trying to fill in textbox based on combobox
    If Me.ComboBox1.Text = "W6 x 15" Then
        UserForm1.TextBox17.Text = "W6 x 15"
    ElseIf Me.Combobox1.Text = "W8 x 24" Then
        Userform1.textbox17.Text = "W8 x 24"
    End If

Upvotes: 1

Views: 9970

Answers (1)

Andres Felipe Martinez
Andres Felipe Martinez

Reputation: 323

The problem here is that you are just initializing the combobox. The combobox has an event, when its value changes. That's when you have to update the text of the textbox, so try this:

Private Sub UserForm_Initialize()
    Me.ComboBox1.AddItem "W6 x 15"
    Me.ComboBox1.AddItem "W8 x 2"
    Me.ComboBox1.AddItem "W10 x 30"

End Sub

Oh, also, the word "Me" references the "parent" object, which in this case is the userform. If you were to use "UserForm1" instead of "Me", it would also work.

Private Sub ComboBox1_Change()
    Me.TextBox1.Text = Me.ComboBox1.Value
End Sub

Upvotes: 2

Related Questions