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

missed column in listbox when try calculation

0

Hi,

I need to know why doesn't show last column in listbox when calculate balance .

the correct as show in the picture.

I hope fixing my problem.

Answer
Discuss

Answers

0
Selected Answer

Hi again Tubrak

To match your "picture" results, I corrected one cell in the worksheet to "UNPAID" (see the yellow cell) and modified the procedure LoadCustomerData() (behind Userform1), added just one new variable - see changes in bold below and comments:

Private Sub LoadCustomerData()
    On Error GoTo ErrHandler

    Dim ws As Worksheet
    Dim lastRow As Long, i As Long
    Dim custNo As String
    Dim debitTotal As Double, creditTotal As Double
    Dim rowData As Variant
    Dim itemNo As Long ' new variable for listbox items

    Set ws = ThisWorkbook.Sheets("data")
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    custNo = ComboBox1.Value

    If custNo = "" Then Exit Sub

    ListBox1.Clear
    debitTotal = 0
    creditTotal = 0

    ' Set ListBox1 column count & widths
    ListBox1.ColumnCount = 7 ' was 5
    ListBox1.ColumnWidths = "30;80;80;80;90;60;60;60" 'added first and columns and adjusted

    ' add header row from array
    rowData = Array("ITEM", "DATE", "INVOICE NO", "CONDITION", "DEBIT", "CREDIT", "BALANCE")

    ListBox1.AddItem

    For i = 0 To ListBox1.ColumnCount - 1
        ListBox1.List(ListBox1.ListCount - 1, i) = rowData(i)
    Next i

    For i = 2 To lastRow
        If Trim(ws.Cells(i, 2).Value) = custNo Then
            rowData = Array( _
                ws.Cells(i, 1).Text, _
                ws.Cells(i, 2).Text, _
                ws.Cells(i, 3).Text, _
                ws.Cells(i, 4).Text, _
                ws.Cells(i, 5).Value, _
                ws.Cells(i, 6).Value)
            'add to count
            itemNo = itemNo + 1
            ListBox1.AddItem
            ListBox1.List(ListBox1.ListCount - 1, 0) = itemNo ' item number, was rowData(0)
            ListBox1.List(ListBox1.ListCount - 1, 1) = rowData(0)
            ListBox1.List(ListBox1.ListCount - 1, 2) = rowData(2) ' skip rowData(1), Customer number
            ListBox1.List(ListBox1.ListCount - 1, 3) = rowData(3)
            ListBox1.List(ListBox1.ListCount - 1, 4) = rowData(4)
            ListBox1.List(ListBox1.ListCount - 1, 5) = rowData(5)
            'ListBox1.List(ListBox1.ListCount - 1, 6) = "Balance"

            debitTotal = debitTotal + Val(ws.Cells(i, 5).Value)
            creditTotal = creditTotal + Val(ws.Cells(i, 6).Value)

            ' use the above to add Balance column
            ListBox1.List(ListBox1.ListCount - 1, 6) = debitTotal - creditTotal
        End If
    Next i

    ' Set totals
    TextBox1.Value = Format(debitTotal, "#,##0.00")
    TextBox2.Value = Format(creditTotal, "#,##0.00")
    TextBox3.Value = Format(debitTotal - creditTotal, "#,##0.00")

    ' Conditional formatting for balance
    With TextBox3
        If (debitTotal - creditTotal) < 0 Then
            .BackColor = vbRed
            .ForeColor = vbWhite
        Else
            .BackColor = vbWhite
            .ForeColor = vbBlack
        End If
    End With
    Exit Sub

ErrHandler:
    MsgBox "Error loading customer data: " & Err.Description, vbCritical
End Sub

The revised code is in the attached file and should work.

Hope this makes sense and solves your issue- if so, please remember to mark this Answer as Selected.

Discuss

Discussion

thanks for your help.
tubrak (rep: 26) Sep 24, '26 at 3:56 am
Thanks for selecting my Answer, Tubrak.
John_Ru (rep: 6842) Sep 24, '26 at 4:32 am
Add to Discussion


Answer the Question

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