How merging by sum DEBIT,CREDIT columns for each name

Abdo

Board Regular
Joined
May 16, 2022
Messages
216
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi experts,
I hope finding macro to deal with client no to merge amounts in DEBIT , CREDIT but the merging will be for duplicates CLIENT NO within two dates when add data for every time .
so the report will be in columns I:N
in columns I,J will brings from column A and column K will brings from column C and columns LM will merge from column E ,F based on duplicates client no in column C within dates in column A .
the column N will subtract column L from column F and insert TOTAL row to sum column L,M .
example:
المصنف1
ABCDEFGHIJKLMNO
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00
DEB




result

المصنف1
ABCDEFGHIJKLMN
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.00TOTAL40,000.001,000.0039,000.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00
DEB
Cell Formulas
RangeFormula
N2:N3N2=L2-M2
L3:M3L3=SUM(L2)


example:
المصنف1
ABCDEFGHIJKLMN
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.00TOTAL40,000.001,000.0039,000.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00
506/01/2022-ABDEND10OPENNING10,000.000.0010,000.00
606/01/2022PA-B4ABDEND10PA2,000.008,000.00
707/01/2022SA-B36ABDEND10SA0.00500.007,500.00
DEB
Cell Formulas
RangeFormula
N2:N3N2=L2-M2
L3:M3L3=SUM(L2:L2)



result
المصنف1
ABCDEFGHIJKLMN
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.0006/01/202207/01/2022ABDEND1010,000.002,500.007,500.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00TOTAL50,000.003,500.0046,500.00
506/01/2022-ABDEND10OPENNING10,000.000.0010,000.00
606/01/2022PA-B4ABDEND10PA2,000.008,000.00
707/01/2022SA-B36ABDEND10SA0.00500.007,500.00
DEB
Cell Formulas
RangeFormula
N2:N4N2=L2-M2
L4:M4L4=SUM(L2:L3)



another example and important
المصنف1
ABCDEFGHIJKLMN
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.0006/01/202207/01/2022ABDEND1010,000.002,500.007,500.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00TOTAL50,000.003,500.0046,500.00
506/01/2022-ABDEND10OPENNING10,000.000.0010,000.00
606/01/2022PA-B4ABDEND10PA2,000.008,000.00
707/01/2022SA-B36ABDEND10SA0.00500.007,500.00
809/01/2022PA-B5ABDEND1PA1,200.000.001,200.00
910/01/2022PA-B6ABDEND1SA1,000.002,200.00
1011/01/2022SA-B38ABDEND1SA0.002,000.00200.00
DEB
Cell Formulas
RangeFormula
N2:N4N2=L2-M2
L4:M4L4=SUM(L2:L3)


result
المصنف1
ABCDEFGHIJKLMN
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.0006/01/202207/01/2022ABDEND1010,000.002,500.007,500.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.0009/01/202211/01/2022ABDEND12,200.002,000.00200.00
506/01/2022-ABDEND10OPENNING10,000.000.0010,000.00TOTAL52,200.005,500.0046,700.00
606/01/2022PA-B4ABDEND10PA2,000.008,000.00
707/01/2022SA-B36ABDEND10SA0.00500.007,500.00
809/01/2022PA-B5ABDEND1PA1,200.000.001,200.00
910/01/2022PA-B6ABDEND1SA1,000.002,200.00
1011/01/2022SA-B38ABDEND1SA0.002,000.00200.00
DEB
Cell Formulas
RangeFormula
N5,N2:N3N2=L2-M2
L5:M5L5=SUM(L2:L4)

as you see there is duplicates client no then should not merge for all of duplicates name, just merge within two dates alone for new process.
the project depends on add new data for every time so when add previous data don't repeat again.

I hope to get chance who can write code for me.
thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Abdo,
what code are you looking for, VBA? If so, what start did you write yourself? For the elements in columns L & M you can use the SUMIFS formula - once you've set up the data in I,J an K.
Cheers,
Koen
 
Upvote 0
If so, what start did you write yourself?
if I have then I posted .
this project seem to be difficult to achieve that, that's why I said.
I hope finding macro


For the elements in columns L & M you can use the SUMIFS formula - once you've set up the data in I,J an K
obviously you can't give me vba , if it's so what's the formulas should be as you suggest?
 
Upvote 0
Code:
Sub test()
    Dim s$(1), i&, r As Range
    Set r = Sheets("deb").[i1]
    r.CurrentRegion.Clear
    s(0) = "Select Min(`Date`) As `FROM DATE`, Max(`Date`) As `TO DATE`, `CLIENT NO`, " & _
           "SUM(`Debit`) As `DEBIT`, SUM(`Credit`) As `CREDIT`, SUM(`CREDIT`) - SUM(`DEBIT`) " & _
           "As `BALANCE`From `DEB$` Group By `CLIENT NO`;"
    s(1) = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & _
            ";Extended Properties='Excel 12.0;HDR=Yes';"
    With CreateObject("ADODB.Recordset")
        .Open s(0), s(1), 3, 3, 1
        r.Parent.Range("a1:f1").Copy r
        For i = 0 To .Fields.Count - 1
            r.Cells(1, i + 1) = .Fields(i).Name
        Next
        r.Cells(2, 1).CopyFromRecordset .DataSource
    End With
    With r.CurrentRegion
        With .Cells(.Rows.Count + 1, 1)
            .Value = "TOTAL": .Interior.Color = vbYellow: .Font.Bold = True
        End With
        .Rows(.Rows.Count + 1).Range("d1:f1") = "=sum(r2c:r[-1]c)"
        .Resize(.Rows.Count + 1).Borders.Weight = 2
        .Resize(.Rows.Count + 1).HorizontalAlignment = xlCenter
    End With
End Sub
 
Upvote 0
Thanks Fuji .
I'm not sure what my bad !
it shows error in this line
VBA Code:
        .Open s(0), s(1), 3, 3, 1<br>
it informs me about DEB if this object is existed.
I made sure the sheet name and the headers don't contain any spaces ,but still shows the error!
 
Upvote 0
ok seem to be work.;)
but as I said
as you see there is duplicates client no then should not merge for all of duplicates name, just merge within two dates alone for new process.
see the picture 5,6 for last example will show duplicate name ABDEND1 doesn't merge when repeat the same name for new entering data . but the code will merge, I don't this way.
every new entering data should merge alone regardless there are the same name in previous data.
 
Last edited:
Upvote 0
Missed last sample data.
Code:
Sub test()
    Dim a, i&, ii&, iii&, n&, temp
    With Sheets("deb")
        a = .[a1].CurrentRegion.Value2
        ReDim b(1 To UBound(a, 1), 1 To 6)
        .Columns("i:n").Clear
        For i = 2 To UBound(a, 1)
            If temp <> a(i, 3) Then
                temp = a(i, 3): n = n + 1: ii = 0
                b(n, 1) = a(i, 1): b(n, 2) = a(i, 1): b(n, 3) = a(i, 3)
                Do While temp = a(i + ii, 3)
                    If b(n, 1) > a(i + ii, 1) Then b(n, 1) = a(i + ii, 1)
                    If b(n, 2) < a(i + ii, 1) Then b(n, 2) = a(i + ii, 1)
                    For iii = 1 To 2
                        b(n, iii + 3) = b(n, iii + 3) + a(i + ii, iii + 4)
                    Next
                    b(n, 6) = b(n, 4) - b(n, 5)
                    ii = ii + 1
                    If i + ii > UBound(a, 1) Then Exit Do
                Loop
                i = i + ii - 1
            End If
        Next
        .[a1].Copy .[i1:n1]
        .[i1:n1] = [{"FROM DATE","TO DATE","CLIENT NO","DEBIT","CREDIT","BALANCE"}]
        .[i2].Resize(n, 6) = b
        With [i1].CurrentRegion.Offset(1)
            .HorizontalAlignment = xlCenter
            .Columns("a:b").NumberFormatLocal = "dd/mm/yyyy"
            .Columns("d:f").NumberFormatLocal = "#,#.00"
            With .Rows(.Rows.Count).Cells(1)
                .Value = "TOTAL"
                .Font.Bold = True
                .Interior.Color = vbYellow
                .Range("d1:f1").FormulaR1C1 = "=sum(r2c:r[-1]c)"
            End With
        End With
        [i1].CurrentRegion.Borders.Weight = 2
    End With
End Sub
 
Upvote 0
Great !
I have case , I'm not sure if solve that or impossible.
DEB.xlsm
ABCDEFGHIJKLMN
1DateINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.00TOTAL40,000.001,000.0039,000.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00
DEB
Cell Formulas
RangeFormula
L3:N3L3=SUM(L$2:L2)


and if In enter new data for same name and same date result should be
DEB.xlsm
ABCDEFGHIJKLMN
1DateINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCEFROM DATETO DATECLIENT NODEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.0001/01/202205/01/2022ABDEND140,000.001,000.0039,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.0005/01/202207/01/2022ABDEND12,000.00-2,000.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00TOTAL40,000.003,000.0037,000.00
505/01/2022SA-B36ABDEND1SA0.001,000.0038,000.00
607/01/2022SA-B37ABDEND1SA0.001,000.0037,000.00
DEB
Cell Formulas
RangeFormula
L4:N4L4=SUM(L$2:L3)
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,457
Members
452,515
Latest member
nguyenkim

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