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

后端 未结 13 2047
[愿得一人]
[愿得一人] 2020-11-30 09:01

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 assigne

13条回答
  •  一整个雨季
    2020-11-30 09:30

    I was searching for quite a while for a solution to add a header without using a separate sheet and copy everything into the userform.

    My solution is to use the first row as header and run it through an if condition and add additional items underneath.

    Like that:

    If lborowcount = 0 Then
     With lboorder
     .ColumnCount = 5
     .AddItem
     .Column(0, lborowcount) = "Item"
     .Column(1, lborowcount) = "Description"
     .Column(2, lborowcount) = "Ordered"
     .Column(3, lborowcount) = "Rate"
     .Column(4, lborowcount) = "Amount"
     End With
     lborowcount = lborowcount + 1
    End If
            
            
    With lboorder
     .ColumnCount = 5
     .AddItem
     .Column(0, lborowcount) = itemselected
     .Column(1, lborowcount) = descriptionselected
     .Column(2, lborowcount) = orderedselected
     .Column(3, lborowcount) = rateselected
     .Column(4, lborowcount) = amountselected
     
     
     End With
    
    lborowcount = lborowcount + 1

    in that example lboorder is the listbox, lborowcount counts at which row to add the next listbox item. It's a 5 column listbox. Not ideal but it works and when you have to scroll horizontally the "header" stays above the row.

提交回复
热议问题