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

后端 未结 13 1853
[愿得一人]
[愿得一人] 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.

    0 讨论(0)
  • 2020-11-30 09:30

    Just use two Listboxes, one for header and other for data

    1. for headers - set RowSource property to top row e.g. Incidents!Q4:S4

    2. for data - set Row Source Property to Incidents!Q5:S10

    SpecialEffects to "3-frmSpecialEffectsEtched"

    0 讨论(0)
  • 2020-11-30 09:40

    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.

    0 讨论(0)
  • 2020-11-30 09:43

    Another variant on Lunatik's response is to use a local boolean and the change event so that the row can be highlighted upon initializing, but deselected and blocked after a selection change is made by the user:

    Private Sub lbx_Change()
    
        If Not bHighlight Then
    
            If Me.lbx.Selected(0) Then Me.lbx.Selected(0) = False
    
        End If
    
        bHighlight = False
    
    End Sub
    

    When the listbox is initialized you then set bHighlight and lbx.Selected(0) = True, which will allow the header-row to initialize selected; afterwards, the first change will deselect and prevent the row from being selected again...

    0 讨论(0)
  • 2020-11-30 09:44

    I like to use the following approach for headers on a ComboBox where the CboBx is not loaded from a worksheet (data from sql for example). The reason I specify not from a worksheet is that I think the only way to get RowSource to work is if you load from a worksheet.

    This works for me:

    1. Create your ComboBox and create a ListBox with an identical layout but just one row.
    2. Place the ListBox directly on top of the ComboBox.
    3. In your VBA, load ListBox row1 with the desired headers.
    4. In your VBA for the action yourListBoxName_Click, enter the following code:

      yourComboBoxName.Activate`
      yourComboBoxName.DropDown`
      
    5. When you click on the listbox, the combobox will drop down and function normally while the headings (in the listbox) remain above the list.

    0 讨论(0)
  • 2020-11-30 09:45

    Here's my solution.

    I noticed that when I specify the listbox's rowsource via the properties window in the VBE, the headers pop up no problem. Its only when we try define the rowsource through VBA code that the headers get lost.

    So I first went a defined the listboxes rowsource as a named range in the VBE for via the properties window, then I can reset the rowsource in VBA code after that. The headers still show up every time.

    I am using this in combination with an advanced filter macro from a listobject, which then creates another (filtered) listobject on which the rowsource is based.

    This worked for me

    0 讨论(0)
提交回复
热议问题