Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add headers to a multicolumn listbox in an Excel userform using VBA

Tags:

excel

vba

Is it possible to set up the headers in a multicolumn listbox without using a worksheet range as the source?

The following uses an array of variants which is assigned to the list property of the listbox, the headers appear blank.

Sub testMultiColumnLb()
    ReDim arr(1 To 3, 1 To 2)

    arr(1, 1) = "1"
    arr(1, 2) = "One"
    arr(2, 1) = "2"
    arr(2, 2) = "Two"
    arr(3, 1) = "3"
    arr(3, 2) = "Three"


    With ufTestUserForm.lbTest
        .Clear
        .ColumnCount = 2
        .List = arr
    End With

    ufTestUserForm.Show 1
End Sub
like image 400
vzczc Avatar asked Mar 18 '09 09:03

vzczc


People also ask

How do I add a header to a ListBox in VBA?

Column headers are automatically added to the ListBox when you use the RowSource property. The ColumnHeads property must be set to True or the headers will not appear. You can set this property in the code or in the properties window of the ListBox.

How do I add a column to a ListBox in VBA?

Multiple Columns If you want to add items to a multi column listbox, you need to use "AddItem" to add a new row and then either "List" or "Column" to add the specific items past the first column. Both column and row numbers in a listbox start at 0 by default and not 1.


1 Answers

No. I create labels above the listbox to serve as headers. You might think that it's a royal pain to change labels every time your lisbox changes. You'd be right - it is a pain. It's a pain to set up the first time, much less changes. But I haven't found a better way.

like image 129
Dick Kusleika Avatar answered Oct 02 '22 00:10

Dick Kusleika