this code become slow for 18000 rows despite of using dictionary and array

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
Hi experts
I got help from mr.Peter_Ss with some modification .
the code becomes more slowly when test for big data, despite of using dictionary & array
so the big data can be 18000 rows for each sheet . the code will calculation items based on column B and if there is duplicates items should merge for each sheet . the formula will be (stock-sales+pur-return) in sheet summary for last column ,also if there is new item in one of sheets but is not in another , then should also show and calculation . the code create whole data with format & borders and collect the data across sheet into sheet summary
VBA Code:
Sub CollateData_v4()
  Dim d As Object
  Dim ShList As Variant, a As Variant, vals As Variant
  Dim i As Long, j As Long
  Dim s As String
 
  Set d = CreateObject("Scripting.Dictionary")
  ShList = Split("stock|sales|pur|returns", "|")
  For j = 0 To UBound(ShList)
    With Sheets(ShList(j))
      a = .UsedRange.Value2
      For i = 2 To UBound(a)
        s = .Cells(i, 2)
        If Len(s) > 2 Then
         
        If Not d.exists(s) Then d(s) = Join(Application.Index(a, i, Array(3, 4, 5)), ";") & ";;;;"
          vals = Split(d(s), ";")
          If IsNumeric(vals(j + 3)) Then
           vals(j + 3) = vals(j + 3) + a(i, 6)
           Else
           vals(j + 3) = a(i, 6)
          End If
         
            d(s) = Join(vals, ";")
         
        End If
      Next i
    End With
  Next j
  Application.ScreenUpdating = False
  With Sheets("summary")
    .UsedRange.EntireRow.Delete
    With .Range("B2:C2").Resize(d.Count)
      .Value = Application.Transpose(Array(d.Keys, d.Items))
      With .Columns(2)
        .TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
        With .Offset(, 7) ' ### was .Offset(, 5)
          .FormulaR1C1 = "=RC[-4]-RC[-3]+RC[-2]+RC[-1]"
          .Value = .Value
        End With
        '.Resize(, 3).EntireColumn.Insert '### not needed
      End With
      .Columns(1).TextToColumns DataType:=xlDelimited, ConsecutiveDelimiter:=False, Semicolon:=True, Comma:=False, Space:=False, Other:=False
      With .Columns(0)
        .Cells(1).Value = 1
        .DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
      End With
    End With
    With .Range("A1:J1")
      .Value = Array("item", "CODE", "BRAND", "TYPE", "MANUFACTURE", "STOCK", "SALES", "PUR", "RETURNS", "BALANCE")
       .Font.Bold = True
  .Interior.Color = RGB(166, 166, 166)
      .EntireColumn.AutoFit
    End With
    With .UsedRange
      .BorderAround xlContinuous
      .Borders(xlInsideVertical).LineStyle = xlContinuous
      .Borders(xlInsideHorizontal).LineStyle = xlContinuous
    End With
  End With
  Application.ScreenUpdating = True
End Sub
for more detailes
INVEN with single search v0 c.xlsm
ABCDEF
1itemCODEBRANDTYPEMANUFACTUREQTY
21AA-110W40 208LQ8EU2222
32AA-215W40 208LCASSU400
43AA-35W30 208LQ8EU800
54AA-45W30 12x1LQ8EU600
65AA-510W40 208LENIIT300
76AA-65W30 4x4LQ8EU200
87AA-710W40 12x1LQ8EU120
98AA-815W40 12x1LCASSU450
109AA-910W40 12x1LENIIT890
1110AA-1010W40 4x4LQ8EU345
1211AA-1110W40 4x4LCASSU78
1312AA-1210W40 4x4LENIIT123
1413AA-135W40 4x4LQ8EU456
1514AA-145W40 4x4LCASSU678
1615AA-155W40 4x4LENIIT1234
1716AA-1620W50 4x4LQ8EU456
stock



INVEN with single search v0 c.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTURESALES
21/1/2021AA-110W40 208LQ8EU100
31/2/2021AA-215W40 208LCASSU50
41/3/2021AA-35W30 208LQ8EU280
51/4/2021AA-45W30 12x1LQ8EU300
61/5/2021AA-510W40 208LENIIT80
71/6/2021AA-65W30 4x4LQ8EU20
81/7/2021AA-710W40 12x1LQ8EU20
91/8/2021AA-815W40 12x1LCASSU20
101/9/2021AA-910W40 12x1LENIIT876
111/10/2021AA-1010W40 4x4LQ8EU345
121/11/2021AA-1110W40 4x4LCASSU123
131/12/2021AA-1210W40 4x4LENIIT78
141/13/2021AA-135W40 4x4LQ8EU300
151/14/2021AA-145W40 4x4LCASSU34
161/15/2021AA-155W40 4x4LENIIT23
171/16/2021AA-1620W50 4x4LQ8EU56
181/17/2021AA-110W40 208LQ8EU100
sales



INVEN with single search v0 c.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTUREPURCHASE
22/4/2021AA-110W40 208LQ8EU55
32/5/2021AA-215W40 208LCASSU20
42/6/2021AA-35W30 208LQ8EU10
52/7/2021AA-45W30 12x1LQ8EU10
62/8/2021AA-510W40 208LENIIT3
72/9/2021AA-65W30 4x4LQ8EU4
82/10/2021AA-710W40 12x1LQ8EU45
92/11/2021AA-815W40 12x1LCASSU8
102/12/2021AA-910W40 12x1LENIIT1
112/13/2021AA-1010W40 4x4LQ8EU100
122/14/2021AA-1110W40 4x4LCASSU20
132/15/2021AA-1210W40 4x4LENIIT100
142/16/2021AA-135W40 4x4LQ8EU44
152/17/2021AA-145W40 4x4LCASSU20
162/18/2021AA-155W40 4x4LENIIT50
172/19/2021AA-1620W50 4x4LQ8EU12
182/20/2021AA-1720W50 4x4LCASSU9
192/21/2021AA-1820W50 4x4LENIIT4
202/22/2021AA-110W40 208LQ8EU55
pur


INVEN with single search v0 c.xlsm
ABCDEF
1itemCODEBRANDTYPEMANUFACTUREreturns
24/5/2021AA-910W40 12x1LENIIT20
34/6/2021AA-1010W40 4x4LQ8EU30
44/7/2021AA-1110W40 4x4LCASSU40
54/8/2021AA-45W30 12x1LQ8EU10
64/9/2021AA-45W30 12x1LQ8EU11
returns



result
INVEN with single search v0 c.xlsm
ABCDEFGHIJ
1itemCODEBRANDTYPEMANUFACTURESTOCKSALESPURRETURNSBALANCE
21AA-110W40 208LQ8EU22222001102132
32AA-215W40 208LCASSU4005020370
43AA-35W30 208LQ8EU80028010530
54AA-45W30 12x1LQ8EU6003001021331
65AA-510W40 208LENIIT300803223
76AA-65W30 4x4LQ8EU200204184
87AA-710W40 12x1LQ8EU1202045145
98AA-815W40 12x1LCASSU450208438
109AA-910W40 12x1LENIIT89087612035
1110AA-1010W40 4x4LQ8EU34534510030130
1211AA-1110W40 4x4LCASSU78123204015
1312AA-1210W40 4x4LENIIT12378100145
1413AA-135W40 4x4LQ8EU45630044200
1514AA-145W40 4x4LCASSU6783420664
1615AA-155W40 4x4LENIIT123423501261
1716AA-1620W50 4x4LQ8EU4565612412
1817AA-1720W50 4x4LCASSU99
1918AA-1820W50 4x4LENIIT44
summary

can anybody make it fast,please?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
@johnnyL thanks for your code ,file and forgive me I don't used to calss module I thought just add in standard module without see you comment inside the code .
sorry I said that your code gives 16.5 sec then I see Jec's code is better gives 1.5sec for 18000 rows but not completely becuase doesn't show minus values .as to bebo021999's code gives 1,6 sec for 18000 rows
so I select bebo021999's code
thanks guys for all of theses assistance
 
Upvote 0
Oki doki, You can lead 'em to water, but you can't make 'em drink. ;(
 
Upvote 0
Ok how about:

VBA Code:
Sub jec()
 Dim sp, ar, k, a, j As Long, jj As Long
 sp = Split("stock|sales|pur|returns", "|")
  
 With CreateObject("scripting.dictionary")
    For j = 0 To UBound(sp)
       ar = Sheets(sp(j)).UsedRange
       For jj = 2 To UBound(ar)
         k = ar(jj, 3) & "|" & ar(jj, 4) & "|" & ar(jj, 5)
         If Not .exists(k) Then
           .Item(k) = Array(.Count + 1, ar(jj, 2), ar(jj, 3), ar(jj, 4), ar(jj, 5), 0, 0, 0, 0, 0)
           a = .Item(k)
           a(j + 5) = ar(jj, 6)
           a(9) = IIf(j = 1, -ar(jj, 6), ar(jj, 6))
         Else
           a = .Item(k)
           a(j + 5) = a(j + 5) + ar(jj, 6)
           a(9) = a(9) + IIf(j = 1, -ar(jj, 6), ar(jj, 6))
         End If
         .Item(k) = a
       Next
    Next
    Sheets("summary").Range("A2").Resize(.Count, 10) = Application.Index(.items, 0, 0)
 End With
End Sub
 
Upvote 0
@JEC thanks for new version .
the column sales works and show the minus values, but the column RETURNS still doesn't show minus values
 
Upvote 0
I don't see how. I have exactly the same output as Bebo's code
 
Upvote 0
@JEC yes you're right about column returns . I don't note it about Bebo's code. how can I modify your code to show the minus values as columns sales to complete the project,please?
 
Upvote 0
Could you post some sample data again with desired results? I can look tomorrow probably
 
Upvote 0
OK
see the item for last row for sheets sales & returns
somtimes just I have item in sheet SALES but is not existed in others sheets ,also just I have item in sheet RETURNS but is not existed in others sheets . so when calculate the sheet sales for item AA-222= 0-101+0+0 the result is by minus -101 in column BALANCE for sheet SUMMARY .
as to sheet returns for ITEM AA-51=0-0+0-12 the result is by minus -12 in column BALANCE for sheet SUMMARY
INVEN.xlsm
ABCDEF
1DATECODEBRANDTYPEMANUFACTURESALES
21/1/2021AA-110W40 208LQ8EU100
31/2/2021AA-215W40 208LCASSU50
41/3/2021AA-35W30 208LQ8EU280
51/4/2021AA-45W30 12x1LQ8EU300
61/5/2021AA-510W40 208LENIIT80
71/6/2021AA-65W30 4x4LQ8EU20
81/7/2021AA-710W40 12x1LQ8EU20
91/8/2021AA-815W40 12x1LCASSU20
101/9/2021AA-910W40 12x1LENIIT876
111/10/2021AA-1010W40 4x4LQ8EU345
121/11/2021AA-1110W40 4x4LCASSU123
131/12/2021AA-1210W40 4x4LENIIT78
141/13/2021AA-135W40 4x4LQ8EU300
151/14/2021AA-145W40 4x4LCASSU34
161/15/2021AA-155W40 4x4LENIIT23
171/16/2021AA-1620W50 4x4LQ8EU56
181/17/2021AA-110W40 208LQ8EU100
191/18/2021AA-22210W40 208LQ9EU101
sales


INVEN.xlsm
ABCDEF
1itemCODEBRANDTYPEMANUFACTUREreturns
24/5/2021AA-910W40 12x1LENIIT20
34/6/2021AA-1010W40 4x4LQ8EU30
44/7/2021AA-1110W40 4x4LCASSU40
54/8/2021AA-45W30 12x1LQ8EU10
64/9/2021AA-45W30 12x1LQ8EU11
74/10/2021AA-515W30 12x1LQ9EU12
returns


result
INVEN.xlsm
ABCDEFGHIJ
1itemCODEBRANDTYPEMANUFACTURESTOCKSALESPURRETURNSBALANCE
21AA-110W40 208LQ8EU222220011002132
32AA-215W40 208LCASSU40050200370
43AA-35W30 208LQ8EU800280100530
54AA-45W30 12x1LQ8EU6003001021331
65AA-510W40 208LENIIT3008030223
76AA-65W30 4x4LQ8EU2002040184
87AA-710W40 12x1LQ8EU12020450145
98AA-815W40 12x1LCASSU4502080438
109AA-910W40 12x1LENIIT89087612035
1110AA-1010W40 4x4LQ8EU34534510030130
1211AA-1110W40 4x4LCASSU78123204015
1312AA-1210W40 4x4LENIIT123781000145
1413AA-135W40 4x4LQ8EU456300440200
1514AA-145W40 4x4LCASSU67834200664
1615AA-155W40 4x4LENIIT1234235001261
1716AA-1620W50 4x4LQ8EU45656120412
1817AA-22210W40 208LQ9EU010100-101
1918AA-1720W50 4x4LCASSU00909
2019AA-1820W50 4x4LENIIT00404
2120AA-515W30 12x1LQ9EU00012-12
summary



I hope this help you
 
Upvote 0
Ok give this a try:

VBA Code:
Sub jec()
 Dim sp, ar, k, a, j As Long, jj As Long
 sp = Split("stock|sales|pur|returns", "|")
  
 With CreateObject("scripting.dictionary")
    For j = 0 To UBound(sp)
       ar = Sheets(sp(j)).UsedRange
       For jj = 2 To UBound(ar)
         k = ar(jj, 3) & "|" & ar(jj, 4) & "|" & ar(jj, 5)
         If Not .exists(k) Then
           .Item(k) = Array(.Count + 1, ar(jj, 2), ar(jj, 3), ar(jj, 4), ar(jj, 5), 0, 0, 0, 0, 0)
           a = .Item(k)
           a(j + 5) = ar(jj, 6)
           a(9) = IIf(j = 1 Or j = 3, -ar(jj, 6), ar(jj, 6))
         Else
           a = .Item(k)
           a(j + 5) = a(j + 5) + ar(jj, 6)
           a(9) = a(9) + IIf(j = 1, -ar(jj, 6), IIf(j = 3 And a(5) & a(6) & a(7) = "000", -ar(jj, 6), ar(jj, 6)))
         End If
         .Item(k) = a
       Next
    Next
    Sheets("summary").Range("L2").Resize(.Count, 10) = Application.Index(.items, 0, 0)
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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