I would like to optimize many loops which compare 2 tables in my code.
Indeed, the execution time is very long because the 2 tables have about 1500 rows to compare. So, a double loop at the end of the code is just doing 1500*1500 actions. So 2 250 000 actions is really too many.
Hope you will be able to help me, I didn't find the trick to do something else...
Here is the code :
'********************This code compare if some values are in the first table and not in the second one and then in the second one and not in the first one with 2 loops********************
x = DL_COMPARATIF + 4
For t = 2 To DL_COMPARATIF
If Application.WorksheetFunction.CountIf(Sheets("AFFRETEMENTS EN COURS").Range("T:T"), Sheets("COMPARATIF").Range("C" & t)) = 0 Then
x = x + 1
For k = 1 To 17
Sheets("COMPARATIF").Cells(x, k) = Sheets("COMPARATIF").Cells(t, k)
Next k
Sheets("COMPARATIF").Range("R" & x) = "L'OT ne figure pas dans Excel."
Sheets("COMPARATIF").Range("A" & x & ":S" & x).Interior.Color = RGB(221, 235, 247)
Erreur_mois = True
End If
Next t
For t = 3 To DL_AFFRETEMENT
If Application.WorksheetFunction.CountIf(Sheets("COMPARATIF").Range("C:C"), Sheets("AFFRETEMENTS EN COURS").Range("T" & t)) = 0 Then
If Sheets("AFFRETEMENTS EN COURS").Range("V" & t) = "Affrété & faxé" Then
x = x + 1
Sheets("COMPARATIF").Range("A" & x) = Sheets("AFFRETEMENTS EN COURS").Range("B" & t) 'client
Sheets("COMPARATIF").Range("C" & x) = Sheets("AFFRETEMENTS EN COURS").Range("T" & t) 'Numéro d'OT
Sheets("COMPARATIF").Range("E" & x) = Sheets("AFFRETEMENTS EN COURS").Range("S" & t) 'Référence client
Sheets("COMPARATIF").Range("F" & x) = Sheets("AFFRETEMENTS EN COURS").Range("I" & t) 'Date de chargement
Sheets("COMPARATIF").Range("G" & x) = Sheets("AFFRETEMENTS EN COURS").Range("D" & t) 'Ville de chargement
Sheets("COMPARATIF").Range("K" & x) = Sheets("AFFRETEMENTS EN COURS").Range("F" & t) 'Ville d'arrivée
Sheets("COMPARATIF").Range("M" & x) = Sheets("AFFRETEMENTS EN COURS").Range("J" & t) 'Date de livraison
Sheets("COMPARATIF").Range("N" & x) = Sheets("AFFRETEMENTS EN COURS").Range("K" & t) 'Prix client
Sheets("COMPARATIF").Range("O" & x) = Sheets("AFFRETEMENTS EN COURS").Range("L" & t) 'Prix affrété
Sheets("COMPARATIF").Range("P" & x) = Sheets("AFFRETEMENTS EN COURS").Range("M" & t) 'Marge
Sheets("COMPARATIF").Range("Q" & x) = Sheets("AFFRETEMENTS EN COURS").Range("P" & t) 'Affrété
Sheets("COMPARATIF").Range("R" & x) = "L'OT ne figure pas dans AKANEA"
Sheets("COMPARATIF").Range("A" & x & ":Z" & x).Interior.Color = RGB(255, 192, 0)
Erreur_mois = True
End If
End If
Next t
'**********************If rows columns T and C are the same, then we will compare 2 other columns********************************************
For n = 3 To DL_AFFRETEMENT
For t = 2 To DL_COMPARATIF
' Si les OT sont les mêmes
If CStr(Sheets("AFFRETEMENTS EN COURS").Range("T" & n).Value) = CStr(Sheets("COMPARATIF").Range("C" & t).Value) Then
' Alors on verifie que les prix correspondent et si pas correspondance on relève les colonnes + message et calcul de différence
If CStr(Sheets("AFFRETEMENTS EN COURS").Range("K" & n).Value) <> CStr(Sheets("COMPARATIF").Range("N" & t).Value) Then
x = x + 1
For k = 1 To 17
Sheets("COMPARATIF").Cells(x, k) = Sheets("COMPARATIF").Cells(t, k)
Next
Sheets("COMPARATIF").Range("R" & x) = "Ecart de prix client"
Sheets("COMPARATIF").Range("S" & x) = Sheets("AFFRETEMENTS EN COURS").Range("K" & n) - Sheets("COMPARATIF").Range("N" & t)
Sheets("COMPARATIF").Range("A" & x & ":S" & x).Interior.Color = RGB(169, 208, 142)
Erreur_mois = True
End If
If CStr(Sheets("AFFRETEMENTS EN COURS").Range("L" & n).Value) <> CStr(Sheets("COMPARATIF").Range("O" & t).Value) Then
x = x + 1
For k = 1 To 17
Sheets("COMPARATIF").Cells(x, k) = Sheets("COMPARATIF").Cells(t, k)
Next
Sheets("COMPARATIF").Range("R" & x) = "Ecart de prix affrété"
Sheets("COMPARATIF").Range("S" & x) = Sheets("AFFRETEMENTS EN COURS").Range("L" & n) - Sheets("COMPARATIF").Range("O" & t)
Sheets("COMPARATIF").Range("A" & x & ":S" & x).Interior.Color = RGB(47, 117, 181)
Erreur_mois = True
End If
End If
Next t
Next n
Thank you in advance for your precious help.
Sheets("COMPARATIF")the computer is doing a lookup operation which is slow. Do it once and store the results in aWorksheetvariable. Also doing string math with string math likeRange("A" & x & ":S" & x)is slower than direct accessRange("A2").Cells(x,1).Resize(1,19). Even better, store the first cell in each table likeRange("A2")into aRangevariable instead of doing the cell lookup every time. Finally, using.Cells(),.Offset()and.Resize()if much cleaner and easier to maintain as the intent is obvious.