collection columns and delete other for 8000 rows

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
149
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
 
help me sorting data based on ID in column B
VBA Code:
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
      a = Application.Index(itm, 0, 0)
        ReDim Preserve a(1 To UBound(a), 1 To UBound(a, 2) + 1)
        For i = 1 To UBound(a)
        a(i, UBound(a, 2)) = Format$(Split(a(i, 1), "-")(2), String(12, "0"))
        Next
        a = BubleSort(a, 5)
        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) = a
    End With
End Sub

Function BubleSort(a As Variant, s_col As Integer)
Dim lb&, ub&, ub2&, lb2&, i&, ii&
Dim flag As Boolean
Dim temp
    lb = LBound(a, 1): ub = UBound(a, 1): ub2 = UBound(a, 2): lb2 = LBound(a, 2)
    flag = True
    Do While flag
        flag = False
        For ii = lb To ub - 1
            If a(ii, s_col) = 0 Then a(ii, s_col) = 100000 Else
            If a(1 + ii, s_col) < a(ii, s_col) Then
                flag = True
                For i = lb2 To ub2: temp = a(1 + ii, i):  a(1 + ii, i) = a(ii, i):  a(ii, i) = temp
                Next i
            End If
        Next ii
    Loop
    BubleSort = a
End Function
 
Upvote 0
Solution

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You are very welcome
And thank you for the feedback
Be happy and safe
 
Upvote 0
Been travelling and no internet. Do you have a table1 identified. What are your table names.
 
Upvote 0

Forum statistics

Threads
1,215,422
Messages
6,124,808
Members
449,191
Latest member
rscraig11

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