Selected Answer
Hi Tubrak
In the attached file, this code has been changed (with changes and comments in bold):
Private Sub LoadCustomerData()
On Error GoTo ErrHandler
Dim i As Long
Dim rowCustNo As String
Dim rowDate As Date
Dim debit As Double
Dim credit As Double
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("data")
Dim custNo As String
custNo = Trim$(CStr(ComboBox1.Value))
ListBox1.Clear
If Len(custNo) = 0 Then Exit Sub
' add a new variable
Dim useDateFilter As Boolean, FirstDate As Boolean
useDateFilter = False
FirstDate = False
If Len(Trim$(TextBox4.Value)) > 0 And _
Len(Trim$(TextBox5.Value)) > 0 Then
If Not IsDate(TextBox4.Value) Then
Exit Sub
End If
If Not IsDate(TextBox5.Value) Then
Exit Sub
End If
Dim dateFrom As Date
dateFrom = CDate(TextBox4.Value)
Dim dateTo As Date
dateTo = CDate(TextBox5.Value)
If dateFrom > dateTo Then
Exit Sub
End If
useDateFilter = True
End If
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
With ListBox1
.ColumnCount = 7
.ColumnWidths = "30;80;80;80;90;60;60"
.AddItem
.List(0, 0) = "ITEM"
.List(0, 1) = "DATE"
.List(0, 2) = "INVOICE NO"
.List(0, 3) = "CONDITION"
.List(0, 4) = "DEBIT"
.List(0, 5) = "CREDIT"
.List(0, 6) = "BALANCE"
End With
Dim itemNo As Long
itemNo = 0
Dim debitTotal As Double
debitTotal = 0
Dim creditTotal As Double
creditTotal = 0
Dim balance As Double
balance = 0
For i = 2 To lastRow
rowCustNo = Trim$(CStr(ws.Cells(i, "B").Value))
If StrComp(rowCustNo, custNo, vbTextCompare) = 0 Then
' order matches
If IsDate(ws.Cells(i, "A").Value) Then
rowDate = CDate(ws.Cells(i, "A").Value)
' if no dates or in range
' If Not useDateFilter Or _
' (rowDate >= dateFrom And rowDate <= dateTo) Then
If IsNumeric(ws.Cells(i, "E").Value) Then
debit = CDbl(ws.Cells(i, "E").Value)
Else
debit = 0
End If
If IsNumeric(ws.Cells(i, "F").Value) Then
credit = CDbl(ws.Cells(i, "F").Value)
Else
credit = 0
End If
debitTotal = debitTotal + debit
creditTotal = creditTotal + credit
balance = balance + debit - credit
' moved this
'itemNo = itemNo + 1
' check if this is first date in range
If FirstDate = False And useDateFilter = True Then
' flip flag
FirstDate = True
' add opening item if needed
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "-"
.List(.ListCount - 1, 2) = _
"Previous Balance"
.List(.ListCount - 1, 6) = balance
End With
End If
If Not useDateFilter Or (rowDate >= dateFrom And rowDate <= dateTo) Then
itemNo = itemNo + 1
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = itemNo
.List(.ListCount - 1, 1) = _
Format$(rowDate, "dd/mm/yyyy")
.List(.ListCount - 1, 2) = _
ws.Cells(i, "C").Text
.List(.ListCount - 1, 3) = _
ws.Cells(i, "D").Text
If debit <> 0 Then
.List(.ListCount - 1, 4) = debit
Else
.List(.ListCount - 1, 4) = vbNullString
End If
If credit <> 0 Then
.List(.ListCount - 1, 5) = credit
Else
.List(.ListCount - 1, 5) = vbNullString
End If
.List(.ListCount - 1, 6) = balance
End With
End If
End If
End If
Next i
TextBox1.Value = Format$(debitTotal, "#,##0.00")
TextBox2.Value = Format$(creditTotal, "#,##0.00")
TextBox3.Value = Format$(balance, "#,##0.00")
With TextBox3
If balance < 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
I don't have time to explain (sorry) but please try it.
Revision #1, 16 October 2026:
Basically, the new variable FirstDate is used to flag when a row (matching the ComboBox1 choice) first falls within the date range. Prior to that, the Balance is accumulated using your code. When that happens, a ListBox item is created to show "Previous Balance"and the amount. Item numbers are counted from that point.
Revision #2, 18 October 2026:
In the new file attached, I replaced that variable with another (as below) and set it when a matching record date is found before any start date:
' add variable for first matches start of date range
Dim BfrDtRng As Long
For i = 2 To lastRow
rowCustNo = Trim$(CStr(ws.Cells(i, "B").Value))
If StrComp(rowCustNo, custNo, vbTextCompare) = 0 Then
' order matches
If IsDate(ws.Cells(i, "A").Value) Then
rowDate = CDate(ws.Cells(i, "A").Value)
' if no dates or in range
' If Not useDateFilter Or _
' (rowDate >= dateFrom And rowDate <= dateTo) Then
' if any date before from date, set flag
If useDateFilter And rowDate < dateFrom Then
BfrDtRng = True
End If
Then the new record is only set as follows:
' add "Previous Balance" if needed
If BfrDtRng And rowDate > dateFrom Then
' add opening record
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "-"
.List(.ListCount - 1, 2) = _
"Previous Balance"
.List(.ListCount - 1, 6) = balance
End With
' reset flag
BfrDtRng = False
End If
This does assume that your records are in ascending date order.
Revision #3, 20 October 2026:
In this revision, I changed the variable and moved the code to add a "Previous value" item (if needed) BEFORE the test to add other list items. Changes in bold.:
' add variable for first match before start of date range
Dim BfrDtRng As Boolean
BftDt = False
For i = 2 To lastRow
rowCustNo = Trim$(CStr(ws.Cells(i, "B").Value))
If StrComp(rowCustNo, custNo, vbTextCompare) = 0 Then
' order matches
If IsDate(ws.Cells(i, "A").Value) Then
rowDate = CDate(ws.Cells(i, "A").Value)
' if no dates or in range
' If Not useDateFilter Or _
' (rowDate >= dateFrom And rowDate <= dateTo) Then
' if any date after from date, set flag
If useDateFilter And rowDate >= dateFrom And BfrDtRng = False And itemNo = 0 Then
BfrDtRng = True
' add "Previous Balance" if needed
With ListBox1
.AddItem
.List(.ListCount - 1, 0) = "-"
.List(.ListCount - 1, 2) = _
"Previous Balance"
.List(.ListCount - 1, 6) = balance
End With
End If
End If
If this fixes your problem, please mark my Answer as Selected.