filter duplicates 7000 rows for 1000 names and insert new column to calculation

Alaa mg

Active Member
Joined
May 29, 2021
Messages
378
Office Version
  1. 2019
Hello
I have about 1000 names ,could repeat for more than 7000 rows
so what I want put the data for the same name under each other of duplicates names and insert new column to calculation based on column B.
and should brings the balances from OPEN BALANCES for each name and put in first row for each name before filter data. and if there is no name in OPEN BALANCES sheet and it's existed in CUSTOMERS sheet ,then should show zero in column BALANCE.
er
ABCDEF
1DATENAMEORDER NOCONDITIONDEBITCREDIT
206/02/2023ALAA-1ORD-001N/H20000
306/02/2023ALAA-1ORD-002M/H200001000
406/02/2023ALAA-2ORD-003MM/N10000
506/02/2023ALAA-1ORD-004B/N2000100
606/02/2023ALAA-1ORD-004B/NN100
706/02/2023ALAA-2ORD-005T/R1000
806/02/2023ALAA-3ORD-006MNG1200
906/02/2023ALAA-2ORD-007ITSR1200150
1006/02/2023ALAA-2ORD-007ITSRI200100
1106/02/2023ALAA-3ORD-008ITRE200
1206/02/2023ALAA-3ORD-009ITRE100
1306/02/2023ALAA-3ORD-010ITRE100
1406/02/2023ALAA-3ORD-011ITRE1000
1506/02/2023ALAA-3ORD-012ITRE1200
1606/02/2023ALAA-3ORD-013ITRE200
1706/02/2023ALAA-3ORD-014ITRE10050
CUSTOMERS


er
ABC
1ITEMNAMEBALANCE
21ALAA-11200
32ALAA-2-200
4
OPEN BALNCES

so when filter in RESULT sheet brings balance for each name from OPEN BALANCES and insert column BALANCE and calculation as I put the formula .


result
er
ABCDEFG
1DATENAMEORDER NOCONDITIONDEBITCREDITBALANCES
206/02/2023ALAA-1OPEN BALANCE12001200
306/02/2023ALAA-1ORD-001N/H2000021200
406/02/2023ALAA-1ORD-002M/H20000100040200
506/02/2023ALAA-1ORD-004B/N200010042100
606/02/2023ALAA-1ORD-004B/NN10042200
706/02/2023ALAA-2OPEN BALANCE-200-200
806/02/2023ALAA-2ORD-003MM/N100009800
906/02/2023ALAA-2ORD-005T/R100010800
1006/02/2023ALAA-2ORD-007ITSR120015011850
1109/02/2023ALAA-2ORD-007ITSRI20010011950
1206/02/2023ALAA-3OPEN BALANCE0
1306/02/2023ALAA-3ORD-006MNG12001200
1410/02/2023ALAA-3ORD-008ITRE2001400
1511/02/2023ALAA-3ORD-009ITRE1001500
1612/02/2023ALAA-3ORD-010ITRE1001400
1713/02/2023ALAA-3ORD-011ITRE10002400
1814/02/2023ALAA-3ORD-012ITRE12003600
1915/02/2023ALAA-3ORD-013ITRE2003400
2016/02/2023ALAA-3ORD-014ITRE100503450
RESULT
Cell Formulas
RangeFormula
G13:G20,G8:G11,G3:G6G3=G2+E3-F3

if the balance for each name in OPEN BALANCES sheet is minus should put in CREDIT column and the same value put in BALANCE column ,if the balance for each name in OPEN BALANCES sheet is positive should put in DEBIT column and the same value put in BALANCE column and should write OPEN BALANCE in column D for the same row brings value from OPEN BALANCES sheet .
every time when run the macro should clear data in RESULT sheet.
thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try the following macro:

VBA Code:
Sub Balances()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim dic1 As Object, dic2 As Object
  Dim kys As String
  Dim i&, j&, k&, m&, nRow&, nCol&, y&
  Dim a, b, c, ky
  Dim openB As Double
  Dim rng As Range
  
  Set sh1 = Sheets("CUSTOMERS")
  Set sh2 = Sheets("OPEN BALANCES")
  Set sh3 = Sheets("RESULT")
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  Set rng = sh3.Range("D1")
  
  a = sh1.Range("A2:F" & sh1.Range("B" & Rows.Count).End(3).Row).Value  'customers
  ReDim b(1 To UBound(a) * 2, 1 To 2000)                                'rows
  c = sh2.Range("A2:C" & sh2.Range("B" & Rows.Count).End(3).Row).Value  'open balances
  ReDim d(1 To UBound(a) * 2, 1 To 7)                                   'result
  
  For i = 1 To UBound(c)            'open balances
    dic1(c(i, 2)) = c(i, 3)
  Next

  For i = 1 To UBound(a, 1)         'customers
    kys = a(i, 2)
    If Not dic2.exists(kys) Then
      y = y + 1
      b(y, 1) = i
      dic2(kys) = y & "|" & 1
    Else
      nRow = Split(dic2(kys), "|")(0)
      nCol = Split(dic2(kys), "|")(1)
      nCol = nCol + 1
      b(nRow, nCol) = i
      dic2(kys) = nRow & "|" & nCol
    End If
  Next
  
  For Each ky In dic2.keys
    nRow = Split(dic2(ky), "|")(0)
    nCol = Split(dic2(ky), "|")(1)
    For j = 1 To nCol
      If j = 1 Then   'insert open balance
        k = k + 1
        d(k, 1) = a(b(nRow, j), 1)
        d(k, 2) = a(b(nRow, j), 2)
        d(k, 4) = "OPEN BALANCE"
        d(k, 7) = 0
        Set rng = Union(rng, sh3.Range("D" & k + 1))
        If dic1.exists(ky) Then
          openB = dic1(ky)
          If openB < 0 Then d(k, 6) = openB Else d(k, 5) = openB
          d(k, 7) = openB
        End If
      End If
      
      k = k + 1
      For m = 1 To 6
        d(k, m) = a(b(nRow, j), m)
      Next m
      d(k, 7) = d(k - 1, 7) + d(k, 5) - d(k, 6)
    Next j
  Next ky
  
  sh3.Range("2:" & Rows.Count).Clear
  sh1.Range("A1:F1").Copy sh3.Range("A1")
  sh1.Range("F1").Copy sh3.Range("G1")
  sh3.Range("G1").Value = "BALANCES"
  sh3.Range("A2").Resize(k, UBound(d, 2)).Value = d
  rng.Font.Bold = True
  sh3.Range("A:G").EntireColumn.AutoFit
  sh3.Range("A:G").HorizontalAlignment = xlCenter
End Sub

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------​
 
Upvote 0
Solution
Hi @Alaa mg :

Note:
1686315411173.png


If the name is correct, then wrap the name in this line of the macro:
VBA Code:
Set sh2 = Sheets("OPEN BALANCES")

Comment the result of the macro and the time it takes to process.
Cordially
Dante Amor
--------------​
 
Upvote 0
Hi,
sorry about error in sheet name🙏
your code works perfectly with small data ,but gives problem will delete the borders have already created manually and delete numbers formatting E: G
as to big data it gives subscript out of range in this line
VBA Code:
 b(nRow, nCol) = i
I no know what's the problem . I will check it again
if you have any idea this could be useful .;)
 
Upvote 0
but gives problem will delete the borders
Change this line
VBA Code:
sh3.Range("2:" & Rows.Count).Clear

To this:
VBA Code:
sh3.Range("2:" & Rows.Count).ClearContents


----------------

as to big data it gives subscript out of range in this line
Change this line:
VBA Code:
 ReDim b(1 To UBound(a) * 2, 1 To 2000)                                'rows

To this line:
VBA Code:
 ReDim b(1 To UBound(a) * 2, 1 To UBound(a))                                'rows

----------------
I did a test with 7 thousand records in the "CUSTOMER" sheet and the result is in 2 seconds.

Comment how many records you have on each of your sheets.
:cool:
 
Last edited:
Upvote 0
thanks for adjusting .
I did a test with 7 thousand records in the "CUSTOMER" sheet and the result is in 2 seconds.
OMG!!!
so my laptop is too much bad? !!:eek:
I'm shocked !o_O
gives 81.30 sec for 7000 rows.:rolleyes:

Comment how many records you have on each of your sheets.
the big data just be in CUSTOMER sheet more than 7000 rows and of course will increase ,others sheets don't increase at 2000 rows as maximum .
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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