Bonjour,

Un module contient du code composé de plusieurs boucles For récupérant des données dans un tableau (commandes) d'environ 5000 lignes, rien d'incroyable.

Lorsque je démarre excel pour le première fois, le code s’exécute rapidement (environ 2 secondes) puis lorsque j’exécute le même code une seconde fois, il dure environ 30 secondes pour un résultat identique.

J'ai placé quelques "Doevents à la fin des boucles les plus importantes, un End à la fin du module, et Option Explicit en début de module pour assurer la déclaration des variables, mais rien n'y fait.

Auriez-vous une explication à cela ?

Merci

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
Option Explicit
Sub Details()
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer
 
'PREMIÈRE OPÉRATION : Page Détails N-1
ThisWorkbook.Sheets("Détails N-1").Range("A:C").ClearContents
'Récupération des commandes par clients selon critères
If ThisWorkbook.Sheets("Statistiques client").Range("E2") = "Totaux" Then
For k = 1 To ThisWorkbook.Sheets("Commandes").Range("A" & Rows.Count).End(xlUp).Row
If ThisWorkbook.Sheets("Commandes").Cells(k, 3) >= ThisWorkbook.Sheets("Statistiques").Range("A2") And ThisWorkbook.Sheets("Commandes").Cells(k, 3) <= ThisWorkbook.Sheets("Statistiques").Range("B2") Then
ThisWorkbook.Sheets("Détails N-1").Cells(ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = ThisWorkbook.Sheets("Commandes").Cells(k, 4)
ThisWorkbook.Sheets("Détails N-1").Cells(ThisWorkbook.Sheets("Détails N-1").Range("B" & Rows.Count).End(xlUp).Row + 1, 2) = ThisWorkbook.Sheets("Commandes").Cells(k, 2)
End If
Next k
Else
For k = 1 To ThisWorkbook.Sheets("Commandes").Range("A" & Rows.Count).End(xlUp).Row
If ThisWorkbook.Sheets("Commandes").Cells(k, 5) = ThisWorkbook.Sheets("Statistiques client").Range("E2") And _
ThisWorkbook.Sheets("Commandes").Cells(k, 3) >= ThisWorkbook.Sheets("Statistiques").Range("A2") And ThisWorkbook.Sheets("Commandes").Cells(k, 3) <= ThisWorkbook.Sheets("Statistiques").Range("B2") Then
ThisWorkbook.Sheets("Détails N-1").Cells(ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = ThisWorkbook.Sheets("Commandes").Cells(k, 4)
ThisWorkbook.Sheets("Détails N-1").Cells(ThisWorkbook.Sheets("Détails N-1").Range("B" & Rows.Count).End(xlUp).Row + 1, 2) = ThisWorkbook.Sheets("Commandes").Cells(k, 2)
End If
Next k
End If
'S'il n'y a aucune données alors on arrete
If Application.CountA(ThisWorkbook.Sheets("Détails N-1").Range("A:A")) <> 0 Then
'Addition des commandes par SIRET dans la colonne C
For l = 2 To ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Détails N-1").Cells(l, 3) = Application.WorksheetFunction.SumIfs(ThisWorkbook.Sheets("Détails N-1").Columns(2), ThisWorkbook.Sheets("Détails N-1").Columns(1), ThisWorkbook.Sheets("Détails N-1").Cells(l, 1))
Next l
'Copie de la colonne C vers la colonne B
ThisWorkbook.Sheets("Détails N-1").Columns(2).Value = ThisWorkbook.Sheets("Détails N-1").Columns(3).Value
'Purge de la colonne C
ThisWorkbook.Sheets("Détails N-1").Columns(3).ClearContents
'Suppression des doublons
ThisWorkbook.Sheets("Détails N-1").Range("A1:B" & ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row).RemoveDuplicates Columns:=1
'Tri par quantité
ThisWorkbook.Sheets("Détails N-1").Range("A1:B" & ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=ThisWorkbook.Sheets("Détails N-1").Range("B1" & ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row), order1:=xlDescending
End If
Doevents
 
'DEUXIÈME OPÉRATION : Page Détails N
ThisWorkbook.Sheets("Détails N").Range("A:C").ClearContents
'Récupération des commandes par clients selon critères
If ThisWorkbook.Sheets("Statistiques client").Range("E2") = "Totaux" Then
For i = 1 To ThisWorkbook.Sheets("Commandes").Range("A" & Rows.Count).End(xlUp).Row
If ThisWorkbook.Sheets("Commandes").Cells(i, 3) >= ThisWorkbook.Sheets("Statistiques").Range("C2") And ThisWorkbook.Sheets("Commandes").Cells(i, 3) <= ThisWorkbook.Sheets("Statistiques").Range("D2") Then
ThisWorkbook.Sheets("Détails N").Cells(ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = ThisWorkbook.Sheets("Commandes").Cells(i, 4)
ThisWorkbook.Sheets("Détails N").Cells(ThisWorkbook.Sheets("Détails N").Range("B" & Rows.Count).End(xlUp).Row + 1, 2) = ThisWorkbook.Sheets("Commandes").Cells(i, 2)
End If
Next i
Else
For i = 1 To ThisWorkbook.Sheets("Commandes").Range("A" & Rows.Count).End(xlUp).Row
If ThisWorkbook.Sheets("Commandes").Cells(i, 5) = ThisWorkbook.Sheets("Statistiques client").Range("E2") And _
ThisWorkbook.Sheets("Commandes").Cells(i, 3) >= ThisWorkbook.Sheets("Statistiques").Range("C2") And ThisWorkbook.Sheets("Commandes").Cells(i, 3) <= ThisWorkbook.Sheets("Statistiques").Range("D2") Then
ThisWorkbook.Sheets("Détails N").Cells(ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = ThisWorkbook.Sheets("Commandes").Cells(i, 4)
ThisWorkbook.Sheets("Détails N").Cells(ThisWorkbook.Sheets("Détails N").Range("B" & Rows.Count).End(xlUp).Row + 1, 2) = ThisWorkbook.Sheets("Commandes").Cells(i, 2)
End If
Next i
End If
'S'il n'y a aucune données alors on arrete
If Application.CountA(ThisWorkbook.Sheets("Détails N").Range("A:A")) <> 0 Then
'Addition des commandes par SIRET dans la colonne C
For j = 2 To ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row
ThisWorkbook.Sheets("Détails N").Cells(j, 3) = Application.WorksheetFunction.SumIfs(ThisWorkbook.Sheets("Détails N").Columns(2), ThisWorkbook.Sheets("Détails N").Columns(1), ThisWorkbook.Sheets("Détails N").Cells(j, 1))
Next j
'Purge de la colonne B
ThisWorkbook.Sheets("Détails N").Columns(2).ClearContents
'Suppression des doublons
ThisWorkbook.Sheets("Détails N").Range("A1:C" & ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row).RemoveDuplicates Columns:=1
'Tri par quantité
ThisWorkbook.Sheets("Détails N").Range("A1:C" & ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=ThisWorkbook.Sheets("Détails N").Range("C1" & ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row), order1:=xlDescending
End If
Doevents
 
'TROISIÈME OPÉRATION : Récupération des quantités de Détails N en comparant le SIRET des deux pages
For m = 1 To ThisWorkbook.Sheets("Détails N-1").Range("A" & Rows.Count).End(xlUp).Row
If IsError(Application.Match(ThisWorkbook.Sheets("Détails N-1").Cells(m, 1), Sheets("Détails N").Range("A:A"), 0)) = False Then
ThisWorkbook.Sheets("Détails N-1").Cells(m, 3) = Sheets("Détails N").Cells(Application.Match(ThisWorkbook.Sheets("Détails N-1").Cells(m, 1), Sheets("Détails N").Range("A:A"), 0), 3)
Sheets("Détails N").Cells(Application.Match(ThisWorkbook.Sheets("Détails N-1").Cells(m, 1), Sheets("Détails N").Range("A:A"), 0), 3).EntireRow.ClearContents
Else
ThisWorkbook.Sheets("Détails N-1").Cells(m, 3) = 0 'Quantité à 0 si SIRET introuvable
End If
Next m
'Tri par quantité de Détails N
ThisWorkbook.Sheets("Détails N").Range("A1:C" & ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row).Sort key1:=ThisWorkbook.Sheets("Détails N").Range("C1" & ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row), order1:=xlDescending
'Mise à 0 des valeurs de la colonne B
ThisWorkbook.Sheets("Détails N").Range("B1:B" & ThisWorkbook.Sheets("Détails N").Range("A" & Rows.Count).End(xlUp).Row) = 0
Doevents
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End
End Sub