how make this code faster(is too slow)

tubrak

Board Regular
Joined
May 30, 2021
Messages
216
Office Version
  1. 2019
Platform
  1. Windows
hi
can any body help to make this code faster ,please ?
this code match data across multiple sheets and copy and merge duplicated data based on column B and calculate the quantity among the sheet
VBA Code:
Sub calqt()
Dim Chk, Data, WsArr, Temp, i As Long, ii As Long, x As Long, rw As Long, Tm As Double
ReDim Temp(1 To 50000, 1 To 15): Tm = Timer
WsArr = [{"First", "Import", "Export", "Sales Returns", "Purchase Returns"}]
For i = 1 To UBound(WsArr)
    Data = Sheets(WsArr(i)).Cells(1).CurrentRegion.Value
    For ii = 2 To UBound(Data)
        Chk = Application.Match(Data(ii, 2), Application.Index(Temp, , 2), 0)
        If Not IsNumeric(Chk) Then
            x = x + 1
            Temp(x, 1) = x
            Temp(x, 2) = Data(ii, 2)
            Temp(x, 3) = Data(ii, 3)
            Temp(x, 4) = Data(ii, 4)
            Temp(x, 5) = Data(ii, 5)
            rw = x
        Else
            rw = Chk
        End If
        Temp(rw, i + 5) = Temp(rw, i + 5) + Data(ii, 6)
        If i = 1 Or i = 2 Then
            Temp(rw, 12) = Temp(rw, 12) + Data(ii, 7)
            Temp(rw, 14) = Temp(rw, 14) + 1
        End If
        If i = 1 Or i = 3 Then
            Temp(rw, 13) = Temp(rw, 13) + Data(ii, IIf(i = 1, 8, 7))
            Temp(rw, 15) = Temp(rw, 15) + 1
        End If
    Next ii
Next i
With Sheets("STOCK")
    .Range("A2").Resize(x, 15) = Temp
    .Range("K2:K" & x + 1).Formula = "=F2+G2-H2+I2-J2"
    With .Range("L2:L" & x + 1): .Value = Evaluate("=" & .Address & "/" & .Offset(, 2).Address & ""): End With
    With .Range("M2:M" & x + 1): .Value = Evaluate("=" & .Address & "/" & .Offset(, 2).Address & ""): End With
    .Columns(14).Resize(, 2).Delete: .UsedRange.Borders.Weight = 2
End With
MsgBox Format(Timer - Tm, "0.00")

End Sub
if any body interest I will attach the file if this is not enough to understand the code
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How slow is too slow?

Simple things like setting screen updating to false and calculation to manual while the code runs can make a massive difference.
At the start to turn them off
VBA Code:
With Application
    .ScreenUpdating = False
    .Calculation = xlManual
End With
At the end to turn them back on
VBA Code:
With Application
    .ScreenUpdating = True
    .Calculation = xlAutomatic
End With
 
Upvote 0
How slow is too slow?
Compared to my simple data, its speed should at least be 0.03 .
Although using ARRAY but still gives code speed 2.61
Although I tested simple data about 20 rows in all sheets
About your suggestion made no difference
 
Upvote 0
Based on what evidence?
not exactly 0.03 but I gave you for example 0.04 or 0.05 but not reach 1.00 or 2.61 as in my case .

the code design for at least 50000 rows . this is not big data . as I said I just tested for 20 rows . is it reasonable to gives speed 2.61? :unsure:

I see many codes in this forum just use loop without array . despite of this speed can't reach 1.00
 
Upvote 0
There are many factors to take into consideration, not just the code. There is nothing in it that would cause slow running.

Other applications running on your device (including background processes) could be taking system resources away from excel. Your device might not be as powerful as the ones that the other code ran on.

Do you have any other code that could be executed when this finishes? For example, a worksheet change event in the 'Stock' sheet would run at least 4 times between the start and end points of your timer.

You could try using Debug.Print Format(Timer - Tm, "0.00") at various points in your code in order to establish if there is a specific section that is taking a lot of time compared to the rest. I would add it between Next ii and Next i in order to see how long each sheet is taking in the loop. Note that this would be the time from the start of the procedure, not the time of each individual loop.
 
Upvote 0
I’m afraid I am a bit tied up for a couple of days but this line suggests you might need to be using a dictionary.
VBA Code:
Chk = Application.Match(Data(ii, 2), Application.Index(Temp, , 2), 0)
This looks like a 1-50,000 item search on each iteration.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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