macro merge data based on column and sum values based on two columns and insert column balance

Ali M

Active Member
Joined
Oct 10, 2021
Messages
290
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
hi
I have repeated data for each name . so what I want merge duplicate names based on column B and split column C into two columns and if threre repeate item also should merge with comma and summing values in columns D,E and insert column BALANCE to subtract column D from E
first sheet
deb.xlsm
ABCDE
1DATENAMECONDITIONDEBITCREDIT
220/09/2021ALI1INVOICE110,000.000
321/09/2021ALI1INVOICE25,000.000
422/09/2021ALI3INVOICE320,000.000
523/09/2021ALI4INVOICE415,000.000
624/09/2021ALI5INVOICE525,000.000
725/09/2021ALI6INVOICE630,000.000
826/09/2021ALI7INVOICE71,500.000
927/09/2021ALI7INVOICE810,000.000
1028/09/2021ALI9INVOICE92,000.000
1129/09/2021ALI10INVOICE103,500.000
1230/09/2021ALI11INVOICE111,500.000
1301/10/2021ALI12INVOICE1210,000.000
1402/10/2021ALI13INVOICE1320,000.000
1503/10/2021ALI14INVOICE1430,000.000
1604/10/2021ALI11INVOICE1540,000.000
1705/10/2021ALI15INVOICE163,000.000
1806/10/2021ALI14VOUCHER12,000.000
1907/10/2021ALI15VOUCHER21,500.000
2008/10/2021ALI16INVOICE1730,000.000
2109/10/2021ALI16VOUCHER52,000.000
2210/10/2021ALI16VOUCHER61,000.00
FIRST



the result should in second sheet . it should create the whole data with headers
deb.xlsm
ABCDEFG
1ITEMNAMEINVOICE NOVOUCHER NODEBITCREDITBALANCE
21ALI1INVOICE1,2-15,000.000-15,000.000
32ALI3INVOICE3-20,000.000-20,000.000
43ALI4INVOICE4-15,000.000-15,000.000
54ALI5INVOICE5-25,000.000-25,000.000
65ALI6INVOICE6-30,000.000-30,000.000
76ALI7INVOICE7-1,500.000-1,500.000
87ALI7INVOICE8-10,000.000-10,000.000
98ALI9INVOICE9-2,000.000-2,000.000
109ALI10INVOICE10-3,500.000-3,500.000
1110ALI11INVOICE11,15-41,500.000-41,500.000
1211ALI12INVOICE12-10,000.000-10,000.000
1312ALI13INVOICE13-20,000.000-20,000.000
1413ALI14INVOICE14-30,000.0002,000.00028,000.000
1514ALI15INVOICE16VOUCHER23,000.0001,500.0001,500.000
1615ALI16INVOICE17VOUCHER5,630,000.0003,000.00027,000.000
SECOND
Cell Formulas
RangeFormula
G2:G16G2=E2-F2
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Since you have Excel 2016, you have Power Query and Power Pivot as part of your release. In Power Pivot
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"DEBIT", Int64.Type}, {"CREDIT", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Invoices", each if Text.StartsWith([CONDITION],"INV") then [CONDITION] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Voucher", each if Text.StartsWith([CONDITION],"VOU") then [CONDITION] else null),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"CONDITION"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"CREDIT", Int64.Type}})
in
    #"Changed Type1"

This gets your data into a format that will allow you to create a pivot table. You will need to close and load this to a connection and add it to the Data Model when prompted. That will allow you to create some measures in Power Pivot.

Book1
GHIJK
1NAMEInvoiceNrVoucherNrSum of DEBITSum of CREDIT
2ALI1INVOICE1 INVOICE215000
3ALI10INVOICE103500
4ALI11INVOICE11 INVOICE1541500
5ALI12INVOICE1210000
6ALI13INVOICE1320000
7ALI14INVOICE14 VOUCHER1300002000
8ALI15INVOICE16 VOUCHER230001500
9ALI16INVOICE17 VOUCHER5 VOUCHER6300003000
10ALI3INVOICE320000
11ALI4INVOICE415000
12ALI5INVOICE525000
13ALI6INVOICE630000
14ALI7INVOICE7 INVOICE811500
15ALI9INVOICE92000
Sheet1
 

Attachments

  • Measure.jpg
    Measure.jpg
    31.8 KB · Views: 6
Upvote 0
Hi
What about
VBA Code:
Sub test()
a = Sheets("sheet1").Cells(1).CurrentRegion
With CreateObject("scripting.dictionary")
  For i = 2 To UBound(a)
    If Not .Exists(a(i, 2)) Then
   .Add a(i, 2), Array(.Count + 1, a(i, 2), a(i, 3), 0, a(i, 4), 0, 0)
    w = .Item(a(i, 2))
   w(6) = w(4) - w(5)
  .Item(a(i, 2)) = w
  Else
  w = .Item(a(i, 2))
  w(5) = IIf(a(i, 5) <> 0, w(5) + a(i, 5), w(5))
  If a(i, 3) Like ("INVOICE*") Then
    w(2) = w(2) & "," & Right(a(i, 3), Len(a(i, 3)) - 7)
    w(4) = w(4) + a(i, 4)
    w(6) = w(4) - w(5)
   .Item(a(i, 2)) = w
  Else
   w(3) = IIf(w(3) = 0, a(i, 3), w(3) & "," & Right(a(i, 3), Len(a(i, 3)) - 7))
   w(5) = w(5) + a(i, 4)
   w(6) = w(4) - w(5)
  .Item(a(i, 2)) = w
 End If
End If
Next
ITM = .items
Sheets("sheet2").Range("a1").Resize(, 7) = Array("ITEM", "NAME", "INVOICE NO", "VOUCHER NO", "DEBIT", "CREDIT", "BALANCE")
Sheets("sheet2").Range("a2").Resize(.Count, 7) = Application.Transpose(Application.Transpose(.items))
Sheets("sheet2").Activate
End With
End Sub
 
Upvote 0
@mohadin that's great ! but I need fixing somthings the value 0 should be" - "and the numbers should show numberforamt as in my OP.
 
Upvote 0
Hi Ali M
VBA Code:
Sub test()
a = Sheets("sheet1").Cells(1).CurrentRegion
With CreateObject("scripting.dictionary")
  For i = 2 To UBound(a)
    If Not .Exists(a(i, 2)) Then
   .Add a(i, 2), Array(.Count + 1, a(i, 2), a(i, 3), "-", a(i, 4), "-")
    w = .Item(a(i, 2))
  .Item(a(i, 2)) = w
  Else
  w = .Item(a(i, 2))
  w(5) = IIf(a(i, 5) <> "-", a(i, 5), w(5))
  If a(i, 3) Like ("INVOICE*") Then
    w(2) = w(2) & "," & Right(a(i, 3), Len(a(i, 3)) - 7)
    w(4) = w(4) + a(i, 4)
    w(5) = IIf(a(i, 5) <> "-", "-", w(4))
   .Item(a(i, 2)) = w
  Else
   w(3) = IIf(w(3) = "-", a(i, 3), w(3) & "," & Right(a(i, 3), Len(a(i, 3)) - 7))
   w(5) = IIf(w(5) = "-", "-", w(5) + a(i, 4))
  .Item(a(i, 2)) = w
 End If
End If
Next
ITM = .items
Sheets("sheet2").Range("a1").Resize(, 7) = Array("ITEM", "NAME", "INVOICE NO", "VOUCHER NO", "DEBIT", "CREDIT", "BALANCE")
Sheets("sheet2").Range("a2").Resize(.Count, 6) = Application.Transpose(Application.Transpose(.items))
Sheets("sheet2").Range("a2").Offset(, 6).Resize(.Count).FormulaR1C1 = "=IFERROR(RC[-2]-RC[-1],RC[-2])"
Sheets("sheet2").Activate
End With
End Sub
 
Upvote 0
@mohadin I would create the whole data with formatting whether values or borders in cells . see the pic2 . it should the same thing
 
Upvote 0
VBA Code:
Option Explicit
Sub test()
Dim a, w, itm As Variant
Dim i As Long
a = Sheets("sheet1").Cells(1).CurrentRegion
With CreateObject("scripting.dictionary")
  For i = 2 To UBound(a)
    If Not .Exists(a(i, 2)) Then
   .Add a(i, 2), Array(.Count + 1, a(i, 2), a(i, 3), "-", a(i, 4), "-")
    w = .Item(a(i, 2)): .Item(a(i, 2)) = w
  Else
  w = .Item(a(i, 2)): w(5) = IIf(a(i, 5) <> "-", a(i, 5), w(5))
  If a(i, 3) Like ("INVOICE*") Then
    w(2) = w(2) & "," & Right(a(i, 3), Len(a(i, 3)) - 7)
    w(4) = w(4) + a(i, 4): w(5) = IIf(a(i, 5) <> "-", "-", w(4))
   .Item(a(i, 2)) = w
  Else
   w(3) = IIf(w(3) = "-", a(i, 3), w(3) & "," & Right(a(i, 3), Len(a(i, 3)) - 7))
   w(5) = IIf(w(5) = "-", "-", w(5) + a(i, 4))
  .Item(a(i, 2)) = w
 End If: End If
Next
i = .Count: itm = .items
End With
With Sheets("sheet2").Range("a2").Resize(i, 7)
.Offset(-1).Resize(1) = Array("ITEM", "NAME", "INVOICE NO", "VOUCHER NO", "DEBIT", "CREDIT", "BALANCE")
.Resize(, 6).Value = Application.Transpose(Application.Transpose(itm))
.Offset(, 6).Resize(i, 1).FormulaR1C1 = "=IFERROR(RC[-2]-RC[-1],RC[-2])"
      With .Offset(-1).Resize(i + 1)
       .Resize(1).Interior.Color = vbGreen '5287936
       .Resize(1).Font.Bold = True
       .HorizontalAlignment = xlCenter
       .Borders(xlEdgeLeft).LineStyle = xlContinuous
       .Borders(xlEdgeTop).LineStyle = xlContinuous
       .Borders.Weight = xlThin
       .EntireColumn.AutoFit
       End With: End With
Sheets("sheet2").Activate
End Sub
 
Upvote 0
it doesn't merge duplicate value in column F (CREDIT). see th row 14 it gives 1000. it should be 3000. also I have ever said repeatedly should show numberformat as in pic2 like this 3,000.00
may you fix it please?
Monthly Summery.xlsm
ABCDEFG
1ITEMNAMEINVOICE NOVOUCHER NODEBITCREDITBALANCE
21ALI1INVOICE1,2-15000-15000
32ALI3INVOICE3-20000-20000
43ALI4INVOICE4-15000-15000
54ALI5INVOICE5-25000-25000
65ALI6INVOICE6-30000-30000
76ALI7INVOICE7,8-11500-11500
87ALI9INVOICE9-2000-2000
98ALI10INVOICE10-3500-3500
109ALI11INVOICE11,15-41500-41500
1110ALI12INVOICE12-10000-10000
1211ALI13INVOICE13-20000-20000
1312ALI14INVOICE14VOUCHER130000200028000
1413ALI15INVOICE16VOUCHER2300015001500
1514ALI16INVOICE17VOUCHER5,630000100029000
sheet2
Cell Formulas
RangeFormula
G2:G15G2=IFERROR(E2-F2,E2)
 
Upvote 0

Forum statistics

Threads
1,215,128
Messages
6,123,204
Members
449,090
Latest member
bes000

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