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 previous balance when search dates on form

0

Hello,

I would show previous balance from last column in list box when search dates in textboxes  for name is selected from combobox  , if the balance is plus then will put word OPENING BALANCE under CONDITION column in listbox and put in amount in DEBIT column ,if the balance is minus then will put word OPENING BALANCE under CONDITION column in listbox and put amount in CREDIT column.

I put expected result in SS sheet and how calculate and suppose showing in listbox.

thanks for your advanced help .

Answer
Discuss

Discussion

Hi again Tubrak.

Sorry but this makes little sense to me.

You seem to have two UserForm text boxes for "from date" and "to date" but you want to display a balance before the "from date". 

Please explain why you want this and I may be able to help.
John_Ru (rep: 6872) Oct 13, '26 at 6:01 pm
Hi John again.
I would show balance before frome date because of I wnat to know balance how was  before show me movements from date to date and I consider opening balance .
if I use as code does it then will show me balance within dates and ignore previous balance and this is wrong ,but in reality there is balance before from date and should take that balance in consideration  when calculate balance within dates.
this case should show when search for dates.
tubrak (rep: 28) Oct 14, '26 at 4:14 am
Tubrak

Understood but transactions in a date range need the last balance on that account (or order) - that isn't always the (original) opening balance.

For example if the order opened on O1 June and a payment was made on 12 June, a search between 15 June and 30 June should include the balance at 12 June (not 01 June), right?
John_Ru (rep: 6872) Oct 14, '26 at 9:01 am
For example if the order opened on O1 June and a payment was made on 12 June, a search between 15 June and 30 June should include the balance at 12 June (not 01 June), right?
yes that's right.
tubrak (rep: 28) Oct 14, '26 at 9:08 am
Thanks Tubrak.

I think I've fixed your problem. Hope to reply tomorrow.
John_Ru (rep: 6872) Oct 15, '26 at 4:41 pm
Add to Discussion

Answers

0
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.

Discuss

Discussion

thank John.
I thnnk we're agreed about this
For example if the order opened on O1 June and a payment was made on 12 June, a search between 15 June and 30 June should include the balance at 12 June (not 01 June), right?
But everytime search for dates will brings the first row as Previous Balance and this is wrong . the right brings before from date as Previous BalancePrevious Balance
tubrak (rep: 28) Oct 17, '26 at 3:44 am
Tubrak. Thanks for replying. If I get time I'll try to fix that but please note that I volunteer to help fix problems. I'm not obliged to provided code working to your specification (though I normally try to).
John_Ru (rep: 6872) Oct 17, '26 at 6:52 am
no problem John if you can fix it I grateful for you even if you can't fix also I grateful for you.
thanks for your time.
tubrak (rep: 28) Oct 17, '26 at 9:36 am


thank John again 
this what I got  for CUS-OI-1001 customer when search 07/03/2021        to   11/03/2021
DATE           INVOICE NO CONDITION      DEBIT    CREDIT     BALANCE
07/03/2021     INV-OI-1001          PAID                         3000           1000  
-                     Previous Balance                                                          0
11/03/2021    INV-OI-1001           PAID                         1000              0
tubrak (rep: 28) Oct 18, '26 at 3:42 am
Hi Tubrak. I provided another version today (with a new file).
John_Ru (rep: 6872) Oct 20, '26 at 10:46 am
Well Done John!
I'm grateful for your assistance.
tubrak (rep: 28) Oct 21, '26 at 5:24 am
Great! Sorry it took so long; I'm out of practice now! Thanks for selecting my Answer, Tubrak.
John_Ru (rep: 6872) Oct 21, '26 at 8:56 am
Add to Discussion


Answer the Question

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