userform - combobox & texbox-VBForums
Results 1 to 5 of 5

Thread: userform - combobox & texbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    4

    userform - combobox & texbox

    hi

    I have a problem and I don't know how to solve it.
    I have a userform with 15 comboboxes and 15 textboxes.

    combobox1 refer to textbox1, the combobox2 refer to textbox2 etc.

    i.e
    sub load_data_to_combobox()
    .
    .
    .
    With UserForm1.ComboBox1
    .ColumnCount = 4
    .ColumnWidths = "0;1in;1in;;;"
    .TextColumn = 4 'the first column is the ID, the second is the name the third is the price
    .AddItem Sheets("sh1").Range("a2:d" & y).Cells(i - 1, 1).Value
    End With
    .
    .
    .
    end sub

    Private Sub ComboBox1_Change()
    .
    .
    .
    .
    userform1.textbox1.value=???
    .
    .
    .
    .
    end sub

    question1: Is it possible to take the value from the third column of the combobox directly to textbox? I'm using the vlookup from the sheet that contains the data.

    question2: The same function load_data_to_combobox I have to repeat for all 15 comboboxes. Is any possibility to use the function "for .. to... next" or "loop" or anything else
    something like this

    for i =1 to 15

    With UserForm1.ComboBox & i ' I tried this but is not working
    .ColumnCount = 4
    .ColumnWidths = "0;1in;1in;;;"
    .TextColumn = 4 'the first column is the ID, the second is the name the third is the price
    .AddItem Sheets("sh1").Range("a2:d" & y).Cells(i - 1, 1).Value
    End With
    next i

    thanks for your help

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    22,890

    Re: userform - combobox & texbox

    something like this
    Code:
    for i = 1 to 15
    with userform.controls("Combobox" & i)

    Code:
    Private Sub ComboBox1_Change()
    .
    .
    .
    .
    userform1.textbox1.value=ComboBox1.List(ComboBox1.ListIndex, 3)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    4

    Re: userform - combobox & texbox

    thanks for the reply. It works!!!!!!!!!

    I have another question.

    Now my combobox sort the items by the first column, the ID.
    Is it possible to sort by second column, the name?

    again thanks

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    22,890

    Re: userform - combobox & texbox

    i think you would need to sort the items prior to adding ie. sort the input range, else put all the items into an array then sort the array, then add to combobox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2015
    Posts
    4

    Re: userform - combobox & texbox

    thanks

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.