match duplicates names to sum & subtract for each other

Omran Y

Board Regular
Joined
Jul 17, 2023
Messages
60
Office Version
  1. 2013
Platform
  1. Windows
Hello
I want creating report in OUTPUT sheet .
it should populate names and sum amounts in DEBIT column and CREDIT column for each duplicates names
and insert TOTAL row to sum whole columns DEBIT,CREDIT . every time I will add new data in ALL sheet so should clear data before brings in OUPUT sheet
the names will be in last location in item .
the data could be 5000 rows

OMRAN.xlsm
ABCD
1DATEDESCRIBEDEBITCREDIT
201/01/2023SALES INVOICE AT300 - OMRAN OMAR3,000.00
302/01/2023SALES INVOICE AT301 - OMAR ALI6,000.00
403/01/2023SALES INVOICE AT302 - ALI OMARN10,000.00
504/01/2023PAID OMRAN OMAR1,000.00
605/01/2023PAID ALI OMARN2,000.00
706/01/2023SALES INVOICE AT303 - OMRAN OMAR3,000.00
807/01/2023SALES INVOICE AT304 - ALI OMARN50,000.00
908/01/2023PAID OMRAN OMAR1,200.00
1009/01/2023PAID OMRAN OMAR200.00
all



before
OMRAN.xlsm
ABCDE
1ITEMNAMEDEBITCREDITBALANCE
2
3
4
5
OUTPUT




result
OMRAN.xlsm
ABCDE
1ITEMNAMEDEBITCREDITBALANCE
21OMRAN OMAR6,000.002,400.003,600.00
32OMAR ALI6,000.000.006,000.00
43ALI OMARN60,000.00200058,000.00
5TOTAL72,000.004,400.0067,600.00
OUTPUT
Cell Formulas
RangeFormula
E2:E5E2=C2-D2
C5:D5C5=SUM(C2:C4)
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You could achieve this with a simple formula, rather than messing about with VBA etc. The totals would update if new data is added, you would however, need to add a new name on the OUTPUT sheet if one was added.
Book1
ABCDE
1ITEMNAMEDEBITCREDITBALANCE
21OMRAN OMAR6,000.002,400.003,600.00
32OMAR ALI6,000.000.006,000.00
43ALI OMARN60,000.002,000.0058,000.00
5TOTAL72,000.004,400.0067,600.00
OUTPUT
Cell Formulas
RangeFormula
C2:C4C2=SUMPRODUCT(--ISNUMBER(FIND(B2,all!B:B)),all!C:C)
D2:D4D2=SUMPRODUCT(--ISNUMBER(FIND(B2,all!B:B)),all!D:D)
E2:E5E2=C2-D2
C5:D5C5=SUM(C2:C4)
 
Upvote 0
thanks .
what about before picture , and populate names in column B automatically and insert TOTAL row automatically ?:(
I no know if all of theses the formula deals with them !
did you read my thread well or not clear ? !:rolleyes:
 
Upvote 0
How do you expect to be able to add new names to the Output sheet when they only form part of the text string in column B of the all sheet?
 
Upvote 0
You can give the below a try:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant, x As Long
    Dim col As New Collection, tmp As String, c As Variant
    Dim oVar As Variant, z As Long
    Dim wsAll As Worksheet, wsOut As Worksheet
  
    Set wsAll = Sheets("all")
    Set wsOut = Sheets("OUTPUT")
  
    Set rng = wsAll.Range("A1:D" & wsAll.Range("A" & Rows.Count).End(xlUp).Row)
    var = rng.Value
  
    For x = 2 To UBound(var)
        If InStr(var(x, 2), " - ") Then
            var(x, 2) = Right(var(x, 2), Len(var(x, 2)) - InStr(var(x, 2), " - ") - 2)
            tmp = var(x, 2)
        Else
            var(x, 2) = Replace(var(x, 2), "PAID ", "")
            tmp = var(x, 2)
        End If
        On Error Resume Next
            col.Add tmp, CStr(tmp)
        On Error GoTo 0
    Next x

    ReDim oVar(col.Count, 4)
  
    With Application
        For Each c In col
            oVar(z, 0) = z + 1
            oVar(z, 1) = c
            oVar(z, 2) = .SumIf(.Index(rng, , 2), "*" & c & "*", .Index(rng, , 3))
            oVar(z, 3) = .SumIf(.Index(rng, , 2), "*" & c & "*", .Index(rng, , 4))
            oVar(z, 4) = oVar(z, 2) - oVar(z, 3)
            z = z + 1
        Next c
        oVar(z, 0) = "TOTAL"
        oVar(z, 2) = .Sum(Application.Index(rng, , 3))
        oVar(z, 3) = .Sum(Application.Index(rng, , 4))
        oVar(z, 4) = oVar(z, 2) - oVar(z, 3)
    End With
  
    wsOut.UsedRange.Offset(1).Font.Bold = False
    wsOut.UsedRange.Offset(1).ClearContents
    wsOut.Range("A2").Resize(UBound(oVar) + 1, 5) = oVar
    wsOut.Range("A:A").Find("TOTAL").EntireRow.Font.Bold = True
End Sub


It creates the below from your 'all' sheet:
TABLE.xlsm
ABCDE
1ITEMNAMEDEBITCREDITBALANCE
21OMRAN OMAR600024003600
32OMAR ALI600006000
43ALI OMARN60000200058000
5TOTAL72000440067600
OUTPUT
 
Upvote 0
You could also replace the below two lines:
VBA Code:
        oVar(z, 2) = .Sum(Application.Index(rng, , 3))
        oVar(z, 3) = .Sum(Application.Index(rng, , 4))
With:
VBA Code:
        oVar(z, 2) = .Sum(.Index(rng, , 3))
        oVar(z, 3) = .Sum(.Index(rng, , 4))
 
Upvote 0
How do you expect to be able to add new names to the Output sheet when they only form part of the text string in column B of the all sheet?
big question , difficult question , sorry buddy ! 🙏🙏
I know you ask me this question , but I wanted advice how do that if the experts guide me the right way , that's why I specify in last location ,Thinking of me could be help ,but doesn't seem help at all . :eek:
any option to avoid this problem I will be all my ears . :love:
thanks in advance
 
Upvote 0
@Georgiboy
thanks works 99%
I need some fixing
when add new names in ALL sheet and when update in OUTPUT sheet will delete borders from the new rows for new added names and also delete borders from TOTAL row . is there any way to add borders and formatting automatically when add new data in OUPUT sheet
also could I add another words in this line
VBA Code:
            var(x, 2) = Replace(var(x, 2), "PAID ", "")
for instance : PAID, RECIEVED ,SALARY
 
Upvote 0
Maybe the below:
VBA Code:
Sub test()
    Dim rng As Range, var As Variant, x As Long
    Dim col As New Collection, tmp As String, c As Variant
    Dim oVar As Variant, z As Long
    Dim wsAll As Worksheet, wsOut As Worksheet
    
    Set wsAll = Sheets("all")
    Set wsOut = Sheets("OUTPUT")
    
    wsOut.Rows("2:" & Rows.Count).Delete
    
    Set rng = wsAll.Range("A1:D" & wsAll.Range("A" & Rows.Count).End(xlUp).Row)
    var = rng.Value
    
    For x = 2 To UBound(var)
        If InStr(var(x, 2), " - ") Then
            var(x, 2) = Right(var(x, 2), Len(var(x, 2)) - InStr(var(x, 2), " - ") - 2)
            tmp = var(x, 2)
        Else
            var(x, 2) = Replace(Replace(Replace(var(x, 2), "PAID ", ""), "RECIEVED ", ""), "SALARY ", "")
            tmp = var(x, 2)
        End If
        On Error Resume Next
            col.Add tmp, CStr(tmp)
        On Error GoTo 0
    Next x

    ReDim oVar(col.Count, 4)
    
    With Application
        For Each c In col
            oVar(z, 0) = z + 1
            oVar(z, 1) = c
            oVar(z, 2) = .SumIf(.Index(rng, , 2), "*" & c & "*", .Index(rng, , 3))
            oVar(z, 3) = .SumIf(.Index(rng, , 2), "*" & c & "*", .Index(rng, , 4))
            oVar(z, 4) = oVar(z, 2) - oVar(z, 3)
            z = z + 1
        Next c
        oVar(z, 0) = "TOTAL"
        oVar(z, 2) = .Sum(.Index(rng, , 3))
        oVar(z, 3) = .Sum(.Index(rng, , 4))
        oVar(z, 4) = oVar(z, 2) - oVar(z, 3)
    End With
    
    With wsOut
        .UsedRange.Offset(1).Font.Bold = False
        .UsedRange.Offset(1).ClearContents
        .Range("A2").Resize(UBound(oVar) + 1, 5) = oVar
        With .Range("A:A").Find("TOTAL")
            .EntireRow.Font.Bold = True
            .Interior.Color = 11573124
        End With
        .UsedRange.Borders.LineStyle = xlContinuous
    End With
End Sub
 
Upvote 0
Solution
@Georgiboy
I appreciate your dedication to writing the code for me . everything is perfect .(y)
thank you so much .:)
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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