Fill combobox with multiple columns

  • Hello,


    I'd like to fill a combobox (in a userform) with 2 columns. With a listbox i know how to do that. When i try that way for a combobox an error occurs.


    Is it possible to fill a combobox with multiple columns? Does anyone know how to do that?


    Thanks in advance


    Werner

  • Re: Fill combobox with multiple columns


    do you not just need to adjust the bound columns property of the combo box to be = to no. of columns you're filling it with?


    if you're filling with VBA can you post your code.

  • Re: Fill combobox with multiple columns


    This uses a recordset to populate but you could use data from a worksheet

  • Re: Fill combobox with multiple columns


    You guys make this look so difficult!


    Code
    Private Sub UserForm_Initialize()
        With ComboBox1
            .ColumnCount = 2
            .List = Range("ArrayList").Value
        End With
    End Sub



    Then just set the bound column to the one you want returned.


    You don't even have to set the ColumnCount in code like this it's a property you can change in the properties window.

    [SIZE="1"]Sub All_Macros(Optional control As Variant)[/SIZE]

  • Re: Fill combobox with multiple columns


    Thank you for your quick reply!


    I guess Hogmaghub has the solution to my problem. The combo box is not filled with values of a range. I'll go and try it.


    Werner

  • Re: Fill combobox with multiple columns


    So where are you populating your combobox from. Aaron's solution is neat & quick, although I would use a Listbox.

  • Re: Fill combobox with multiple columns


    Quote from Winnij

    Thank you for your quick reply!


    I guess Hogmaghub has the solution to my problem. The combo box is not filled with values of a range. I'll go and try it.


    Werner


    Range? Array? doesn't matter...


    OK then here's the array version:

    Code
    Private Sub UserForm_Initialize() 
        With ComboBox1 
            .ColumnCount = 2 
            .List = MyArray
        End With 
    End Sub

    [SIZE="1"]Sub All_Macros(Optional control As Variant)[/SIZE]

  • Re: Fill combobox with multiple columns


    Thanx for your answer Aaron.


    I would post some code but it's a kind of a mess at the moment.
    I quess i didn't explain my problem good enough.


    The first problem i have is a recordset, it loses its values when a adodb connection is closed. Therefore i need to put the recordset in an array.


    That's where your code comes, with an array you can fill a combobox. I tried your code with a recordset, but it didn't work out.


    I'll go and try your code, but first i need to "convert" a recordset to an array

  • Re: Fill combobox with multiple columns


    This is the solution:


    In one combobox with 2 columns, you need to put this code:


    Do While Not recordset.EOF



    combobox.AddItem (recordset.Fields("field1") & ";" & recordset.Fields("field2"))
    recordset.MoveNext

    Loop


    The point is in the ";", used by vba for separing the different columns.


    I hope this help you.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!