collection columns and delete other for 8000 rows

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
174
Office Version
  1. 2019
Platform
  1. Windows
Hi

I search for macro to merge duplicates items for 8000 rows . so I want merge duplicates items based on column B for each sheet individually .

in sheet purchase ,sell just I want merging column B,E .so in sheet output should merge duplicates items , values for sheet PURCHSE and put in column C. as to sheet SELL should merge duplicates items , values and put in column D and show the BALANCE in column E as I put the formula . every time when run the macro should clear data from row2 before brings data .
items.xlsx
ABCDE
1DATEITEMCUSTOMERINV NOQTY
211/11/19ITTT-100/AS-1CSS-100INV-A123200
312/11/19ITTT-100/AS-2CSS-101INV-A124300
413/11/19ITTT-100/AS-3CSS-102INV-A125400
514/11/19ITTT-100/AS-4CSS-103INV-A126500
615/11/19ITTT-100/AS-5CSS-104INV-A127600
716/11/19ITTT-100/AS-6CSS-105INV-A128700
817/11/19ITTT-100/AS-7CSS-100INV-A129800
918/11/19ITTT-100/AS-4CSS-107INV-A130900
1019/11/19ITTT-100/AS-5CSS-108INV-A1311000
1120/11/19ITTT-100/AS-6CSS-109INV-A1321100
1221/11/19ITTT-100/AS-7CSS-110INV-A1331200
1322/11/19ITTT-100/AS-1CSS-111INV-A1341300
1423/11/19ITTT-100/AS-2CSS-112INV-A1351400
1524/11/19ITTT-100/AS-3CSS-102INV-A1361500
1625/11/19ITTT-100/AS-8CSS-107INV-A1371600
1726/11/19ITTT-100/AS-10CSS-108INV-A1381601
PURCHSE



items.xlsx
ABCDE
1DATEITEMCLIENTINV NOQTY
230/11/19ITTT-100/AS-8CLS-100INV-AT10100
301/12/19ITTT-100/AS-5CLS-101INV-AT11120
402/12/19ITTT-100/AS-6CLS-102INV-AT12140
503/12/19ITTT-100/AS-4CLS-103INV-AT13160
604/12/19ITTT-100/AS-5CLS-104INV-AT14180
705/12/19ITTT-100/AS-5CLS-105INV-AT15200
806/12/19ITTT-100/AS-7CLS-106INV-AT16220
907/12/19ITTT-100/AS-4CLS-107INV-AT17240
1008/12/19ITTT-100/AS-5CLS-108INV-AT18260
1109/12/19ITTT-100/AS-6CLS-109INV-AT19280
1210/12/19ITTT-100/AS-7CLS-110INV-AT20300
1312/12/19ITTT-100/AS-2CLS-112INV-AT22340
1414/12/19ITTT-100/AS-8CLS-114INV-AT24380
1515/12/19ITTT-100/AS-9CLS-115INV-AT25381
SELL



items.xlsx
ABCDE
1ITEMBRANDPURCHASESELLBALANCE
21ITTT-100/AS-11500-1500
32ITTT-100/AS-217003401360
43ITTT-100/AS-31900-1900
54ITTT-100/AS-414004001000
65ITTT-100/AS-516005001100
76ITTT-100/AS-618004201380
87ITTT-100/AS-720003001700
98ITTT-100/AS-8-480-480
109ITTT-100/AS-9-381-381
1110ITTT-100/AS-1010611061
OUTPUT
Cell Formulas
RangeFormula
E2:E11E2=C2-D2





thanks
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi
Perhaps
VBA Code:
Sub test()
    Dim a, itm
    Dim i&
    a = Sheets("PURCHSE").Cells(1).CurrentRegion
    With CreateObject("scripting.dictionary")
        For i = 2 To UBound(a)
            If a(i, 2) <> 0 Then
                If Not .exists(a(i, 2)) Then
                    .Add a(i, 2), Array(a(i, 2), a(i, 5), 0)
                Else
                      w = .Item(a(i, 2))
                      w(1) = w(1) + a(i, 5)
                      .Item(a(i, 2)) = w
                End If
            End If
        Next
         a = Sheets("SEll").Cells(1).CurrentRegion
         For i = 2 To UBound(a)
            If a(i, 2) <> 0 Then
                If .exists(a(i, 2)) Then
                     w = .Item(a(i, 2))
                      w(2) = w(2) + a(i, 5)
                      .Item(a(i, 2)) = w
                Else
                  .Add a(i, 2), Array(a(i, 2), 0, a(i, 5))
                End If
            End If
        Next
        k = .Count:  itm = .items
      End With
        With Sheets("OUTPUT")
        .Cells(1).CurrentRegion.ClearContents
        .Cells(1, 1).Resize(, 5) = Array("Item", "BRAND", "PURCHASE", "SELL", "BALANCE")
        .Cells(2, 1).Resize(k) = Application.Evaluate("row(1:" & k & ")")
        .Cells(2, 2).Resize(k, 3) = Application.Index(itm, 0, 0)
        Cells(2, 5).Resize(k).FormulaR1C1 = "=RC[-2]-RC[-1]"
    End With
End Sub
 
Upvote 0
Hi
thanks , but I need fixing some problems .
1- seem the sheet output should be active . if it's not will implement any active sheet and will occures big choas and error mismatch in this line
VBA Code:
W(1) = W(1) + a(i, 5)
so I want avoiding this problem if I select any sheet will show data in sheet OUTPUT without any problem
2- variables need declaration W,K . it gives error varibale not defined
3- I don't want showing formula in column BALANCE just as value.
 
Upvote 0
All right a typo
I don't want showing formula in column BALANCE just as value.
Then try
VBA Code:
Sub test()
    Dim a, itm, k, w
    Dim i&
    a = Sheets("PURCHSE").Cells(1).CurrentRegion
    With CreateObject("scripting.dictionary")
        For i = 2 To UBound(a)
            If a(i, 2) <> 0 Then
                If Not .exists(a(i, 2)) Then
                    .Add a(i, 2), Array(a(i, 2), a(i, 5), 0, a(i, 5))
                Else
                      w = .Item(a(i, 2))
                      w(1) = w(1) + a(i, 5)
                      w(3) = w(1) - w(2)
                      .Item(a(i, 2)) = w
                End If
            End If
        Next
         a = Sheets("SEll").Cells(1).CurrentRegion
         For i = 2 To UBound(a)
            If a(i, 2) <> 0 Then
                If .exists(a(i, 2)) Then
                     w = .Item(a(i, 2))
                      w(2) = w(2) + a(i, 5)
                       w(3) = w(1) - w(2)
                      .Item(a(i, 2)) = w
                Else
                  .Add a(i, 2), Array(a(i, 2), 0, a(i, 5), -a(i, 5))
                End If
            End If
        Next
        k = .Count:  itm = .items
      End With
        With Sheets("OUTPUT")
        .Cells(1).CurrentRegion.ClearContents
        .Cells(1, 1).Resize(, 5) = Array("Item", "BRAND", "PURCHASE", "SELL", "BALANCE")
        .Cells(2, 1).Resize(k) = Application.Evaluate("row(1:" & k & ")")
        .Cells(2, 2).Resize(k, 4) = Application.Index(itm, 0, 0)
'        .Cells(2, 5).Resize(k).FormulaR1C1 = "=RC[-2]-RC[-1]"
    End With
End Sub
 
Upvote 0
An alternative means is with Power Query

1. Load purchases and Group
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ITEM"}, {{"Purchase", each List.Sum([QTY]), type number}})
in
    #"Grouped Rows"
2. Load Sales and Group
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ITEM"}, {{"Sales", each List.Sum([QTY]), type number}})
in
    #"Grouped Rows"
3. Join the two tables with a full outer join and subtract sales from purchases
Power Query:
let
    Source = Table.NestedJoin(Table1, {"ITEM"}, Table2, {"ITEM"}, "Table2", JoinKind.FullOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"ITEM", "Sales"}, {"ITEM.1", "Sales"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Table2",null,0,Replacer.ReplaceValue,{"Purchase"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value",null,0,Replacer.ReplaceValue,{"Sales"}),
    #"Inserted Subtraction" = Table.AddColumn(#"Replaced Value1", "Subtraction", each [Purchase] - [Sales], type number)
in
    #"Inserted Subtraction"
.4. Any updates to either table will automatically update the results when you select Refresh All on the Data Tab.
 
Upvote 0
@mohadin thanks, but can you help me sorting data based on ID in column B in sheet OUTPUT from small to big like this,please?
ITTT-100/AS-1
ITTT-100/AS-2
ITTT-100/AS-3
ITTT-100/AS-4
ITTT-100/AS-5
ITTT-100/AS-6
ITTT-100/AS-7
ITTT-100/AS-8
ITTT-100/AS-9
ITTT-100/AS-10
ITTT-100/AS-11
ITTT-100/AS-12
ITTT-100/AS-13
ITTT-100/AS-14
ITTT-100/AS-15
ITTT-100/AS-16
ITTT-100/AS-17
ITTT-100/AS-18
 
Upvote 0
@alansidman thanks but step 3 for PQ3 when join two tables gives error name Table1 , how ?
should also shows error for PQ1 but it doesn't , this means to be right in PQ1 :unsure:
 
Upvote 0
What is the error message? Don't know what your issue is. Are all field names in your tables the same as in your example.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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