Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

populate headers in listbox on form

0

Hello,

I'm not sure if use code to show headers in listbox on form based on row1 inside sheet.

I know way by lable tool to do that , but if it's possible by code will be great and no add more tools on form.

thanks

Answer
Discuss

Answers

0
Selected Answer

Hi Malkal

In the attached  revised file, the Listbox will include the header row. (Note in the "safe" sheet, I changed cell A1 to "DATE", a more meaningful header title!)

The changes in FilterData code are shown in bold in the code extract below (as are new comments):

    ' changed from "A2:", to include header row
    a = Sheets("safe").Range("A1:E" & Sheets("safe").Range("D" & Rows.Count).End(3).Row).Value

    If IsDate(TextBox4.Value) Then dateFrom = DateValue(TextBox4.Value) Else dateFrom = 0
    If IsDate(TextBox5.Value) Then dateTo = DateValue(TextBox5.Value) Else dateTo = DateSerial(9999, 1, 1)

    For i = 1 To UBound(a, 1)

        If (ComboBox2.Value = "" Or LCase(a(i, 2)) Like "*" & LCase(ComboBox2.Text) & "*") And _
           (IsDate(a(i, 1)) And a(i, 1) >= dateFrom And a(i, 1) <= dateTo) Then
            k = k + 1

            ReDim Preserve b(1 To UBound(a, 2), 1 To k)
            For j = 1 To UBound(a, 2)
                b(j, k) = Format(a(i, j), "DD/MM/YYYY")
            Next
            ' Add individual totals...
            If k <= 1 Then
                If IsNumeric(a(i, 3)) Then FTotal = a(i, 3)
                If IsNumeric(a(i, 4)) Then F2Total = a(i, 4)
                Else
                If IsNumeric(a(i, 3)) Then FTotal = FTotal + a(i, 3)
                If IsNumeric(a(i, 4)) Then F2Total = F2Total + a(i, 4)
            End If

            ' added this
            Else
            If k = 0 Then
            ' add header row without adding to Totals
                k = 1
                ReDim Preserve b(1 To UBound(a, 2), 1 To k)
                For j = 1 To UBound(a, 2)
                    b(j, k) = a(i, j)
                Next
            End If

        End If

    Next i
    ' changed from k=0
    If k = 1 Then
        Me.ListBox1.Clear

    Else
        Me.ListBox1.Column = b

    End If

    ' ...add the total row
    ListBox1.AddItem
    'If k = 1 Then
    ' pick title, dependent on any values
    If (ComboBox2 <> "" Or TextBox4 <> "" Or TextBox5 <> "") And k > 0 Then
        ListBox1.List(k, 0) = "Filtered totals"
        Else
        ListBox1.List(k, 0) = "Totals"
    End If

    ' populate columns with totals
    ListBox1.List(k, 1) = ">>>>>>>>>>>>>>>>>>>>>>>"
    ListBox1.List(k, 2) = FTotal
    ListBox1.List(k, 3) = F2Total
    ListBox1.List(k, 4) = FTotal - F2Total

 '   End If

With ListBox1
For s = 0 To .ListCount - 1
.List(s, 2) = Format(.List(s, 2), "#,##0.00")
.List(s, 3) = Format(.List(s, 3), "#,##0.00")
.List(s, 4) = Format(.List(s, 4), "#,##0.00")
Next s
End With

End Sub

Also I modified the textbox code so the Filter is only applied if there's a full date (or no date) to prevent errors with partial dates:

Private Sub TextBox4_Change()
    'Only filter if date delete or full date added
    If Len(TextBox4.Value) = 0 Or Len(TextBox4.Value) = 10 Then FilterData
End Sub

Private Sub TextBox5_Change()
    'Only filter if date delete or full date added
    If Len(TextBox5.Value) = 0 Or Len(TextBox5.Value) = 10 Then FilterData
End Sub

Hope this fixes things for you. If so, please mark my Answer as Selected.

Discuss

Discussion

Awesome !
thanks John.
Malkal (rep: 30) Jul 30, '26 at 7:25 am
Glad you liked it and thanks for selecting my Answer, Malkal.
John_Ru (rep: 6832) Jul 30, '26 at 8:32 am
Add to Discussion
0

Hello Malkal,

This worked for me:

In the FilterData macro, after the block

    If k = 0 Then
        Me.ListBox1.Clear

    Else
        Me.ListBox1.Column = b

    End If

Add in this block:

With ListBox1
    .ColumnHeads = True
    .RowSource = "A2:E11"
End With

To make this dynamic replace "E11" with "E" & LR where LR is a variable that stores the number of the last row with data.

Hope this helps.

Cheers   :-)

Discuss

Discussion

Hi Willi,
I  don't know how works for you!
Malkal (rep: 30) Jul 28, '26 at 11:46 am
@Malkal

I rechecked your original file and found what is probably causing the problem. When I checked the properties of the ListBox I saw that ColumnHeads was set to "False". While testing I had changed that to "True". So you have a choice - change the setting to "True" or add the following line: .ColumnHeads = True above the RowSource line (as I have done in my answer above).

Cheers   :-)
WillieD24 (rep: 738) Jul 28, '26 at 2:10 pm
also doesn't work and shows error!
Malkal (rep: 30) Jul 29, '26 at 5:38 am
I also get a crtical error with that change (or rather when replacing the Else outcome with Willie's suggestion).

Setting the RowRange causes an error when subsequent code which tries to append an addtional row to the ListBox. This prevents the Totals row Malkal got from my previous Answer. In any case, setting the RowSource bypasses any filtering done from TextBox and/or ComboBow selections in this UserForm.

I'll see if I have time to look at a solutiion in the next day or two.
John_Ru (rep: 6832) Jul 29, '26 at 6:15 am
In my answer above I have added the file I used so you can see that it works when you click on "CommandButton1". However, when making a selection under "DESCRIBE" an error is generated. I haven't worked on that at all, I will leave that up to you.

Cheers   :-)
WillieD24 (rep: 738) Jul 29, '26 at 8:08 am
@Willie - thanks for adding the file. It gives the headings on initialization but produces an error if the ComboBox or TextBoxes are changed. Unfortunately this means the essential data filtering (or filtered subtotalling) can't work. That's the downside of that approach. Might work if a filtered (hidden) sheet was created but I don't want to try that.
John_Ru (rep: 6832) Jul 29, '26 at 10:16 am
@John

Yes, I knew about the error as I mentioned in my discussion point of 8:08am. I also said I hadn't worked on it and was leaving that for Malkal to sort out.
WillieD24 (rep: 738) Jul 29, '26 at 5:24 pm
@Willie - okay. I may be wrong but I suspect that Malkal doesn't have sufficient knowledge to sort out such matters  Hopefully my Answer compensates for that.
John_Ru (rep: 6832) Jul 29, '26 at 6:25 pm
I'm not sure guys what are your talking about erro!
when I  fill textboxes and combobox doesn't show any error . this file has answerd by john in earlier !
Malkal (rep: 30) Jul 30, '26 at 8:46 am
Malkal, We were talking (yesterday) about Willie's file and errors from it, not the one I sent today.
John_Ru (rep: 6832) Jul 30, '26 at 12:53 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login