Create accounts by user form to show balance by calculation for two columns

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
555
Office Version
  1. 2019
Hello
I need populate data for the customer name when select combobox1 and matched with CUSTOMERS column name across sheets,also populate based on two dates in textbox1,2 when select customer combobox1

DECREASE.xlsm
BCDEF
1DATECUSTOMERSDEBITCREDITBALANCE
230/08/2024CCF-10001,500.002,000.00-500.00
330/08/2024CCF-10012,000.002,000.00
430/08/2024CCF-10023,000.003,000.000.00
530/08/2024CCF-10032,000.001,000.001,000.00
630/08/2024CCF-10042,000.002,000.00
730/08/2024CCF-10051,000.00-1,000.00
830/08/2024CCF-10061,000.00-1,000.00
930/08/2024CCF-10074,000.004,000.00
1030/08/2024CCF-10088,000.008,000.00
1130/08/2024CCF-10095,000.005,000.00
BALANCES
Cell Formulas
RangeFormula
F2:F11F2=D2-E2




DECREASE.xlsm
ABCDEFGHIJK
1ITEMDATECUSTOMERSINV.NOCASEBRANDTYPEORIGINQTY PRICEBALANCE
2115/06/2023CCF-1000BSTR_23448OUTSANDINGBS 750R16R230JAP3.00500.001,500.00
3215/06/2023CCF-1000BSTR_23448OUTSANDINGBS 700R16R230JAP2.00400.00800.00
4SUM2,300.00
5115/09/2023CCF-1000BSTR_23449OUTSANDINGGO 1200R20AZ0026CHI1.00920.00920.00
6215/09/2023CCF-1000BSTR_23449OUTSANDINGGO 1200R20AZ0083CHI1.001,000.001,000.00
7SUM1,920.00
8115/09/2023CCF-1000BSTR_23450PAIDBS 1200R20G580JAP9.001,800.0016,200.00
9215/09/2023CCF-1000BSTR_23450PAIDBS 1200R20G580THI9.001,800.0016,200.00
10315/09/2023CCF-1000BSTR_23450PAIDBS 1200R20R187THI10.001,800.0018,000.00
11SUM50,400.00
12116/09/2023CCF-1001BSTR_23452PAIDBS 1200R20G580JAP4.001,800.007,200.00
13SUM7,200.00
14116/09/2023BSTR_23453OUTSANDINGBS 1200R20G580JAP3.001,880.005,640.00
15SUMCCF-10015,640.00
SV



DECREASE.xlsm
ABCDEFGHIJK
1ITEMDATECUSTOMERSINV.NOCASEBRANDTYPEORIGINQTY PRICEBALANCE
2115/06/2023CCF-1000BSJ_23444OUTSANDINGBS 215/60R16ER30JAP4.00430.001,720.00
3SUMCCF-1000OUTSANDING1,720.00
4115/06/2023BSJ_23445GO 1200R20AZ0026CHI2.00955.001,910.00
5SUM1,910.00
6115/09/2023CCF-1000BSJ_23446PAIDGO 1200R20AZ0026CHI2.00950.001,900.00
7215/09/2023CCF-1000BSJ_23446PAIDGO 1200R21AZ0027CHI3.001,000.003,000.00
8SUM4,900.00
9115/09/2023CCF-1002BSJ_23447PAIDBS 1200R20G580JAP1.002,000.002,000.00
10215/09/2023CCF-1002BSJ_23447PAIDBS 1200R20G580THI1.002,000.002,000.00
11315/09/2023CCF-1002BSJ_23447PAIDBS 1200R20R187THI1.002,000.002,000.00
12SUM6,000.00
SR



DECREASE.xlsm
ABCDEFGHIJK
1ITEMDATECUSTOMERSINV.NOCASEBRANDTYPEORIGINQTY PRICEBALANCE
2115/06/2023CCF-1000VSTR_23444PAIDBS 750R16R230JAP1.00500.00500.00
3SUM500.00
4115/09/2023CCF-1001VSTR_23445OUTSANDINGGO 1200R20AZ0083CHI1.001,000.001,000.00
5SUM1,000.00
6115/09/2023CCF-1000VSTR_23446OUTSANDINGBS 1200R20G580JAP1.001,800.001,800.00
7215/09/2023CCF-1000VSTR_23446OUTSANDINGBS 1200R20G580THI1.001,800.001,800.00
8SUM3,600.00
9116/09/2023CCF-1003VSTR_23447PAIDBS 215/60R16ER30JAP4.00400.001,600.00
10SUM1,600.00
11116/09/2023CCF-1002VSTR_23448PAIDBS 1200R20G580JAP4.001,800.007,200.00
12SUM7,200.00
13116/09/2023CCF-1000VSTR_23449PAIDBS 1200R20G580JAP2.001,880.003,760.00
14SUM3,760.00
VS



DECREASE.xlsm
ABCDEFGHIJK
1ITEMDATECUSTOMERSINV.NOBRANDTYPEORIGINQTY PRICEBALANCE
2110/06/2023CCF-1000RSS_23222OUTSANDINGBS 215/60R16ER30JAP2.00430.00860.00
3SUM860.00
4110/06/2023CCF-1004BSJ_23445OUTSANDINGGO 1200R20AZ0026CHI1.00955.00955.00
5SUM955.00
6115/09/2023CCF-1000BSJ_23446PAIDGO 1200R20AZ0026CHI1.00950.00950.00
7215/09/2023CCF-1000BSJ_23446PAIDGO 1200R21AZ0027CHI2.001,000.002,000.00
8SUM2,950.00
RS



DECREASE.xlsm
ABCD
1DATECUSTOMERSCASEBALANCE
215/06/2023CCF-1000RVCH2000030,000.00
316/06/2023CCF-1001RVCH2000125,000.00
417/06/2023CCF-1000RVCH2000210,000.00
518/06/2023CCF-1003RVCH200031,200.00
615/09/2023CCF-1004VCH200044,000.00
715/09/2023CCF-1000VCH2000515,000.00
815/09/2023CCF-1006VCH2000617,000.00
916/09/2023CCF-1007VCH2000718,000.00
1017/09/2023CCF-1008VCH200081,200.00
1118/09/2023CCF-1009VCH200091,300.00
1219/09/2023CCF-1010RVCH 200101,400.00
1320/09/2023RVCH 200111,500.00
RECEIPT


when select CCF-1000 from combobox1 then should some steps:

1- brings balance in column F from sheet BALANCES as show in row2 under headers in listbox 1 if the amount is minus then should put in CREDIT column and the same amount put in BALANCE column as show in listbox1(if the he amount is positive then should put in DEBIT column and the same amount put in BALANCE column)
2- for RS sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the OUTSTANDING word is existed in CASE column then will put amount in column CREDIT and the calculation in BALANCE will be the first balance in row2 + next row in DEBIT column - CREDIT column like this -500+0-860=1360
3- for SV sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the OUTSTANDING word is existed in CASE column then will put amount in column CREDIT and the calculation in BALANCE will be the first balance in row3 + next row in DEBIT column - CREDIT column like this -1360+0-2300=-3660
4- for SR sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the OUTSTANDING word is existed in CASE column then will put amount in column DEBIT and the calculation in BALANCE will be the first balance in row4+ next row in DEBIT column - CREDIT column like this -3660+1720-0=-1940
5-for VS sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the PAID word is existed in CASE column then will put amount in column CREDIT and the calculation in BALANCE will be the first balance in row5 + next row in DEBIT column - CREDIT column like this -1940+0-500=-2440
6- for RECEIPT brings amount from BALANCE column and match PART of item "RVCH" in CASE column then will put in DEBIT column like this
-2440+30000-0=27560 another process for the same sheet will be= 27560+10000-0= 37560

7- for SV sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the PAID word is existed in CASE column then will put amount in column DEBIT and the calculation in BALANCE will be the first balance in row8 + next row in DEBIT column - CREDIT column like this 37560+50400-0=86040

8- for SR sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the PAID word is existed in CASE column then will put amount in column CREDIT and the calculation in BALANCE will be the first balance in row9 + next row in DEBIT column - CREDIT column like this 86040+0-4900=-81140
9-for VS sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the OUTSTANDING word is existed in CASE column then will put amount in column DEBIT and the calculation in BALANCE will be the first balance in row10 + next row in DEBIT column - CREDIT column like this 81140+3600-0=84740

10- for RS sheet brings amount from SUM row in column K based on OUTSTANDING,PAID . if the PAID word is existed in CASE column then will put amount in column DEBIT and the calculation in BALANCE will be the first balance in row11 + next row in DEBIT column - CREDIT column like this 84740+2950-0=87690
11- for RECEIPT brings amount from BALANCE column and match PART of item "VCH" in CASE column then will put in CREDIT column like this
87690+0-15000=72690
moreover should insert SUM roe to sum column DEBIT,CREDIT and subtract column DEBIT from CREDIT and show BALANCE column .
without forgetting the data could be 9000 rows for each sheet.
here is picture to show and calculation for selected customer from combobox2
1.JPG

last thing I would show formatting number like"#,##0.00"
also should show - for empty digits in column DEBIT,CREDIT and if the BALANCE column is zero for any row should also show hyphen instead of zero.
thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Well, it is a very very long request, with several details.

So I have several considerations that you should follow:

1. If you are going to capture dates, then I ask you to put a commandbutton1 in your userform, then after capturing the Customer and/or the dates you must press the button.​
2. In your example you have some errors, which are corrected with a macro, you will have the opportunity to verify them.​
3. You have dates 2023 and 2024 in your data. Just so you consider it when you do your tests.​
4. With the date filter, it is possible that no records exist in the "BALANCES" sheet.​
5. To get to your example, I had to sort the data by dates, so I need to put the data in columns P to V of the "BALANCES" sheet.​
6. The first time you load the data into the userform it will take a couple of seconds, as it will read all the sheets and perform the sort, after that the filters you make will be fast.​
7. I adjusted the size of the listbox columns and the font of the listbox, so that the amounts were aligned to the right.​
8. I fill the combobox with the Customers from the "BALANCES" sheet. If you already have it filled then you can delete this line from the macro.​
VBA Code:
  ComboBox1.List = Sheets("BALANCES").Range("C2", Sheets("BALANCES").Range("C" & Rows.Count).End(3)).Value

Final Example:
1727814671759.png

Try:
VBA Code:
Dim c As Variant                  '<-- At the beginning of all the code

Sub Populate_Listbox()
  Dim i As Long, j As Long, k As Long, n As Long
  Dim bal As Double, sumD As Double, sumC As Double
  Dim d As Variant, e As Variant
  Dim una As Boolean
  Dim sVal As String
  Dim ini As Double, fin As Double
  
  ListBox1.Clear
  
  'add HEADERS
  ReDim d(1 To UBound(c, 1) + 2, 1 To UBound(c, 2))
  For j = 1 To 7
    d(1, j) = c(1, j)
  Next
  d(2, 1) = 1

  'add ITEMS
  k = 1
  n = 0
  For i = 2 To UBound(c, 1)
    If c(i, 1) = ComboBox1.Value Then
    
      If TextBox1.Value = "" Then ini = c(i, 2) Else ini = CDate(TextBox1.Value)
      If TextBox2.Value = "" Then fin = c(i, 2) Else fin = CDate(TextBox2.Value)
      
      If c(i, 2) >= ini And c(i, 2) <= fin Then
        n = n + 1
        k = k + 1
        d(k, 1) = n
        For j = 2 To 7
          Select Case j
            Case 2
              d(k, j) = Format(c(i, j), "dd/mm/yyyy")
            Case 5, 6, 7
              sVal = Format(c(i, j), "#,##0.00;-#,##0.00;-")
              d(k, j) = String(15 - Len(sVal), " ") & sVal
            Case Else
              d(k, j) = c(i, j)
          End Select
        Next
        sumD = sumD + c(i, 5)
        sumC = sumC + c(i, 6)
        If una = False Then
          bal = bal + c(i, 5) - Abs(c(i, 6))
          una = True
        Else
          bal = bal + c(i, 5) - c(i, 6)
        End If
        sVal = Format(bal, "#,##0.00;-#,##0.00;-")
        d(k, 7) = String(15 - Len(sVal), " ") & sVal
        
      End If
    End If
  Next
  
  'add SUM
  k = k + 1
  d(k, 1) = "SUM"
  sVal = Format(sumD, "#,##0.00;-#,##0.00;-")
  d(k, 5) = String(15 - Len(sVal), " ") & sVal
  
  sVal = Format(sumC, "#,##0.00;-#,##0.00;-")
  d(k, 6) = String(15 - Len(sVal), " ") & sVal
  
  sVal = Format(sumD - sumC, "#,##0.00;-#,##0.00;-")
  d(k, 7) = String(15 - Len(sVal), " ") & sVal
  
  '
  ReDim e(1 To k, 1 To UBound(d, 2))
  For i = 1 To k
    For j = 1 To UBound(d, 2)
      e(i, j) = d(i, j)
    Next
  Next
  ListBox1.List = e
End Sub

Private Sub CommandButton1_Click()
  Dim bCont As Boolean
  
  If ComboBox1.ListIndex = -1 Then
    MsgBox "Selecct customer"
    Exit Sub
  End If
  
  If TextBox1.Value = "" And TextBox2.Value = "" Then
    Call Populate_Listbox
  Else
    If TextBox1.Value <> "" And TextBox2.Value = "" Then
      MsgBox "Populate To Date"
      Exit Sub
    End If
    If TextBox1.Value = "" And TextBox2.Value <> "" Then
      MsgBox "Populate From Date"
      Exit Sub
    End If
    If Not IsDate(TextBox1.Value) Or Len(TextBox1.Value) <> 10 Then
      MsgBox "From Date Invalid"
      Exit Sub
    End If
    If Not IsDate(TextBox2.Value) Or CDate(TextBox2.Value) < CDate(TextBox1.Value) Or Len(TextBox2.Value) <> 10 Then
      MsgBox "To Date Invalid"
      Exit Sub
    End If
    Call Populate_Listbox
  End If
End Sub

Private Sub UserForm_Activate()
  Dim shB As Worksheet
  Dim ba, sv, sr, vs, rs, re
  Dim i&, k&, lr&
  
  Set shB = Sheets("BALANCES")
  
  ba = shB.Range("B2:F" & shB.Range("B" & Rows.Count).End(3).Row).Value2
  sv = Sheets("SV").Range("A2:K" & Sheets("SV").Range("A" & Rows.Count).End(3).Row).Value2
  sr = Sheets("SR").Range("A2:K" & Sheets("SR").Range("A" & Rows.Count).End(3).Row).Value2
  vs = Sheets("VS").Range("A2:K" & Sheets("VS").Range("A" & Rows.Count).End(3).Row).Value2
  rs = Sheets("RS").Range("A2:K" & Sheets("RS").Range("A" & Rows.Count).End(3).Row).Value2
  re = Sheets("RECEIPT").Range("A2:D" & Sheets("RECEIPT").Range("A" & Rows.Count).End(3).Row).Value2
  
  ReDim b(1 To UBound(ba) + UBound(sv) + UBound(sr) + UBound(vs) + UBound(rs) + UBound(re), 1 To 7)
  
  Call fill_a(b, ba, k)                         'BALANCES
  Call fill_b(b, sv, k, "PAID", "OUTSANDING")   'SV
  Call fill_b(b, sr, k, "OUTSANDING", "PAID")   'SR
  Call fill_b(b, vs, k, "OUTSANDING", "PAID")   'VS
  Call fill_b(b, rs, k, "PAID", "OUTSANDING")   'RS
  Call fill_c(b, re, k)                         'RECEIPT

  Application.ScreenUpdating = False
  shB.Range("P1").Resize(1, 7).Value = Array("ITEM", "DATE", "INV.NO", "CASE", "DEBIT", "CREDIT", "BALANCE")
  shB.Range("P2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  lr = shB.Range("V" & Rows.Count).End(3).Row
  For i = 2 To lr
    If shB.Range("R" & i).Value <> "" Then
      shB.Range("P" & i & ":V" & lr).Sort shB.Range("Q" & i), xlAscending, Header:=xlNo
      Exit For
    End If
  Next
  Application.ScreenUpdating = True
  
  c = shB.Range("P1:V" & lr).Value
End Sub

Sub fill_a(b, ba, k)
  Dim i As Long
  
  For i = 1 To UBound(ba)
    k = k + 1
    b(k, 1) = ba(i, 2)          'customer
    b(k, 2) = ba(i, 1)          'date
    If ba(i, 5) > 0 Then
      b(k, 5) = ba(i, 5)        'dbt
      b(k, 6) = 0               'cdt
    Else
      b(k, 6) = ba(i, 5)        'cdt
      b(k, 5) = 0               'dbt
    End If
    b(k, 7) = ba(i, 5)          'balance
  Next
End Sub

Sub fill_c(b, re, k)
  Dim i As Long
  
  For i = 1 To UBound(re)
    k = k + 1
    b(k, 1) = re(i, 2)          'customer
    b(k, 2) = re(i, 1)          'date
    b(k, 4) = re(i, 3)          'case
    If Left(re(i, 3), 4) = "RVCH" Then
      b(k, 5) = re(i, 4)        'dbt
      b(k, 6) = 0               'cdt
    ElseIf Left(re(i, 3), 3) = "VCH" Then
      b(k, 5) = 0               'dbt
      b(k, 6) = re(i, 4)        'cdt
    End If
    b(k, 7) = re(i, 4)          'balance
  Next
End Sub

Sub fill_b(b, ary, k, c1, c2)
  Dim i As Long
  
  For i = 1 To UBound(ary)
    If ary(i, 1) = "SUM" Then
      k = k + 1
      b(k, 1) = ary(i - 1, 3)   'customer
      b(k, 2) = ary(i - 1, 2)   'date
      b(k, 3) = ary(i - 1, 4)   'inv
      b(k, 4) = ary(i - 1, 5)   'case
      If ary(i - 1, 5) = c1 Then
        b(k, 5) = ary(i, 11)    'dbt
        b(k, 6) = 0             'cdt
      ElseIf ary(i - 1, 5) = c2 Then
        b(k, 5) = 0             'dbt
        b(k, 6) = ary(i, 11)    'cdt
      End If
      b(k, 7) = ary(i, 11)      'bal
    End If
  Next
End Sub

Private Sub UserForm_Initialize()
  ComboBox1.List = Sheets("BALANCES").Range("C2", Sheets("BALANCES").Range("C" & Rows.Count).End(3)).Value
  With ListBox1
    .ColumnCount = 7
    .Font.Name = "Consolas"
    .Font.Size = 10
    .ColumnWidths = "40;60;100;100;100;100;100"
  End With
End Sub


I tested with 9 thousand records on each sheet and it works well. But you will have to do a lot of testing to verify the results. If any result is not correct, you should put the data sample here, so that I can do the test.

I hope everything is fine though.

😅
 
Upvote 0
Well, it is a very very long request, with several details.
sorry !
I 'm confused how I post this big subject . I thought if I split this project into multiple threads , but chance to getting help is slight little, even I add this subject I don't except help because it's really hard to do that by form, but you did it as your usual , and I'm glad to follow my threads.
In your example you have some errors
sorry about it !
actually by yellow highlighted in picture I've found out after leave 10 minutes can't edited to correct it, and I don't want post new for this as long I clarify the details and I hoped this missed row is not obstacle to understand my requirements.
as to green highlighted item I accept your way.
I tested code with big data and based on my requirements works excellently!:)
but I would fix my mistake, sorry!
in first row for BALANCE column I would subtract amount DEBIT from CREDIT example:
for CCF-1000= 0-(-500)=500 because the last row should show the same balance in BALANCE column as in ITEM 14 for BALANCE column .
thanks again
 
Upvote 0
I tested code with big data and based on my requirements works excellently!:)
:)

in first row for BALANCE column I would subtract amount DEBIT from CREDIT example:
for CCF-1000= 0-(-500)=500 because the last row should show the same balance in BALANCE column as in ITEM 14 for BALANCE column .
That took me a lot of work to make the first balance record add up differently than the others. 😅

But don't worry, the correction is simpler, I put the corrected code here:
VBA Code:
Dim c As Variant                  '<-- At the beginning of all the code

Sub Populate_Listbox()
  Dim i As Long, j As Long, k As Long, n As Long
  Dim bal As Double, sumD As Double, sumC As Double
  Dim d As Variant, e As Variant
  'Dim una As Boolean
  Dim sVal As String
  Dim ini As Double, fin As Double
 
  ListBox1.Clear
 
  'add HEADERS
  ReDim d(1 To UBound(c, 1) + 2, 1 To UBound(c, 2))
  For j = 1 To 7
    d(1, j) = c(1, j)
  Next
  d(2, 1) = 1

  'add ITEMS
  k = 1
  n = 0
  For i = 2 To UBound(c, 1)
    If c(i, 1) = ComboBox1.Value Then
   
      If TextBox1.Value = "" Then ini = c(i, 2) Else ini = CDate(TextBox1.Value)
      If TextBox2.Value = "" Then fin = c(i, 2) Else fin = CDate(TextBox2.Value)
     
      If c(i, 2) >= ini And c(i, 2) <= fin Then
     
        n = n + 1
        k = k + 1
        d(k, 1) = n
        For j = 2 To 7
          Select Case j
            Case 2
              d(k, j) = Format(c(i, j), "dd/mm/yyyy")
            Case 5, 6, 7
              sVal = Format(c(i, j), "#,##0.00;-#,##0.00;-")
              d(k, j) = String(15 - Len(sVal), " ") & sVal
            Case Else
              d(k, j) = c(i, j)
          End Select
        Next
        sumD = sumD + c(i, 5)
        sumC = sumC + c(i, 6)
        bal = bal + c(i, 5) - c(i, 6)
        sVal = Format(bal, "#,##0.00;-#,##0.00;-")
        d(k, 7) = String(15 - Len(sVal), " ") & sVal
       
      End If
    End If
  Next
 
  'add SUM
  k = k + 1
  d(k, 1) = "SUM"
  sVal = Format(sumD, "#,##0.00;-#,##0.00;-")
  d(k, 5) = String(15 - Len(sVal), " ") & sVal
 
  sVal = Format(sumC, "#,##0.00;-#,##0.00;-")
  d(k, 6) = String(15 - Len(sVal), " ") & sVal
 
  sVal = Format(sumD - sumC, "#,##0.00;-#,##0.00;-")
  d(k, 7) = String(15 - Len(sVal), " ") & sVal
 
  '
  ReDim e(1 To k, 1 To UBound(d, 2))
  For i = 1 To k
    For j = 1 To UBound(d, 2)
      e(i, j) = d(i, j)
    Next
  Next
  ListBox1.List = e
End Sub

Private Sub CommandButton1_Click()
  Dim bCont As Boolean
 
  If ComboBox1.ListIndex = -1 Then
    MsgBox "Selecct customer"
    Exit Sub
  End If
 
  If TextBox1.Value = "" And TextBox2.Value = "" Then
    Call Populate_Listbox
  Else
    If TextBox1.Value <> "" And TextBox2.Value = "" Then
      MsgBox "Populate To Date"
      Exit Sub
    End If
    If TextBox1.Value = "" And TextBox2.Value <> "" Then
      MsgBox "Populate From Date"
      Exit Sub
    End If
    If Not IsDate(TextBox1.Value) Or Len(TextBox1.Value) <> 10 Then
      MsgBox "From Date Invalid"
      Exit Sub
    End If
    If Not IsDate(TextBox2.Value) Or CDate(TextBox2.Value) < CDate(TextBox1.Value) Or Len(TextBox2.Value) <> 10 Then
      MsgBox "To Date Invalid"
      Exit Sub
    End If
    Call Populate_Listbox
  End If
End Sub

Private Sub UserForm_Activate()
  Dim shB As Worksheet
  Dim ba, sv, sr, vs, rs, re
  Dim i&, k&, lr&
 
  Set shB = Sheets("BALANCES")
 
  ba = shB.Range("B2:F" & shB.Range("B" & Rows.Count).End(3).Row).Value2
  sv = Sheets("SV").Range("A2:K" & Sheets("SV").Range("A" & Rows.Count).End(3).Row).Value2
  sr = Sheets("SR").Range("A2:K" & Sheets("SR").Range("A" & Rows.Count).End(3).Row).Value2
  vs = Sheets("VS").Range("A2:K" & Sheets("VS").Range("A" & Rows.Count).End(3).Row).Value2
  rs = Sheets("RS").Range("A2:K" & Sheets("RS").Range("A" & Rows.Count).End(3).Row).Value2
  re = Sheets("RECEIPT").Range("A2:D" & Sheets("RECEIPT").Range("A" & Rows.Count).End(3).Row).Value2
 
  ReDim b(1 To UBound(ba) + UBound(sv) + UBound(sr) + UBound(vs) + UBound(rs) + UBound(re), 1 To 7)
 
  Call fill_a(b, ba, k)                         'BALANCES
  Call fill_b(b, sv, k, "PAID", "OUTSANDING")   'SV
  Call fill_b(b, sr, k, "OUTSANDING", "PAID")   'SR
  Call fill_b(b, vs, k, "OUTSANDING", "PAID")   'VS
  Call fill_b(b, rs, k, "PAID", "OUTSANDING")   'RS
  Call fill_c(b, re, k)                         'RECEIPT

  Application.ScreenUpdating = False
  shB.Range("P1").Resize(1, 7).Value = Array("ITEM", "DATE", "INV.NO", "CASE", "DEBIT", "CREDIT", "BALANCE")
  shB.Range("P2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  lr = shB.Range("V" & Rows.Count).End(3).Row
  For i = 2 To lr
    If shB.Range("R" & i).Value <> "" Then
      shB.Range("P" & i & ":V" & lr).Sort shB.Range("Q" & i), xlAscending, Header:=xlNo
      Exit For
    End If
  Next
  Application.ScreenUpdating = True
 
  c = shB.Range("P1:V" & lr).Value
End Sub

Sub fill_a(b, ba, k)
  Dim i As Long
 
  For i = 1 To UBound(ba)
    k = k + 1
    b(k, 1) = ba(i, 2)          'customer
    b(k, 2) = ba(i, 1)          'date
    If ba(i, 5) > 0 Then
      b(k, 5) = ba(i, 5)        'dbt
      b(k, 6) = 0               'cdt
    Else
      b(k, 6) = ba(i, 5)        'cdt
      b(k, 5) = 0               'dbt
    End If
    b(k, 7) = ba(i, 5)          'balance
  Next
End Sub

Sub fill_c(b, re, k)
  Dim i As Long
 
  For i = 1 To UBound(re)
    k = k + 1
    b(k, 1) = re(i, 2)          'customer
    b(k, 2) = re(i, 1)          'date
    b(k, 4) = re(i, 3)          'case
    If Left(re(i, 3), 4) = "RVCH" Then
      b(k, 5) = re(i, 4)        'dbt
      b(k, 6) = 0               'cdt
    ElseIf Left(re(i, 3), 3) = "VCH" Then
      b(k, 5) = 0               'dbt
      b(k, 6) = re(i, 4)        'cdt
    End If
    b(k, 7) = re(i, 4)          'balance
  Next
End Sub

Sub fill_b(b, ary, k, c1, c2)
  Dim i As Long
 
  For i = 1 To UBound(ary)
    If ary(i, 1) = "SUM" Then
      k = k + 1
      b(k, 1) = ary(i - 1, 3)   'customer
      b(k, 2) = ary(i - 1, 2)   'date
      b(k, 3) = ary(i - 1, 4)   'inv
      b(k, 4) = ary(i - 1, 5)   'case
      If ary(i - 1, 5) = c1 Then
        b(k, 5) = ary(i, 11)    'dbt
        b(k, 6) = 0             'cdt
      ElseIf ary(i - 1, 5) = c2 Then
        b(k, 5) = 0             'dbt
        b(k, 6) = ary(i, 11)    'cdt
      End If
      b(k, 7) = ary(i, 11)      'bal
    End If
  Next
End Sub

Private Sub UserForm_Initialize()
  ComboBox1.List = Sheets("BALANCES").Range("C2", Sheets("BALANCES").Range("C" & Rows.Count).End(3)).Value
  With ListBox1
    .ColumnCount = 7
    .Font.Name = "Consolas"
    .Font.Size = 10
    .ColumnWidths = "40;60;100;100;100;100;100"
  End With
End Sub

And the image with the example:
1727874883549.png


Note: you must capture the dates in the dd/mm/yyyy format, including zeros, for example for May 2: 02/05/2024

😇
 
Upvote 0
Solution

Forum statistics

Threads
1,222,115
Messages
6,164,032
Members
451,868
Latest member
Fifa2020

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top