Bonjour à tous, j'ai dernièrement commencé à utiliser vba et j'aurais besoin d'aide pour optimiser mon code. En fait, ça prend environ 45 sec. pour passer 500 lignes dans ma ''do until'' loop et je devrais l'utiliser pour environ 9000 lignes, ce qui rend le tout beaucoup trop long.

Pourriez-vous m'aider?

Merci!

Voici mon code:

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
Sub calculate_all_together()
'
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
 
Dim i As Integer
Dim t As Integer
Dim hi As Integer
Dim li As Integer
Dim j As Integer
Dim rate_out_i As Integer
 
Dim hn As Double
Dim ln As Double
 
j = 1
t = Cells(27, 3)
hn = t
ln = t
 
For j = 1 To t
    Cells(j, 39) = 1
Next
 
i = 46
                       ' les délais: nombre de 5sec. qu'une charge de "5 sec." passe dans un équipement
dc1 = Cells(23, 10)         ' Conv1 ....
dc2 = Cells(23, 11)
dc3 = Cells(23, 12)
dc4 = Cells(23, 13)
dc5 = Cells(23, 14)
dc6 = Cells(23, 15)
dc7 = Cells(23, 16)
dc8 = Cells(23, 17)
db1 = Cells(23, 18)         'Bin1 pleine
 
Do Until i = 546
 
'######################### bin1 in ########################################
 
    If dc7 < dc1 Then
 
        If i > 45 And i < (46 + dc7) Then
            Cells(i, 12) = (Cells(20, 6) / 3600 * 5) + (Cells(36, 3) * 2 / 3600 * 5)
 
        ElseIf i > (46 + dc7 - 1) And i < (46 + dc1) Then
            Cells(i, 12) = (Cells(20, 6) / 3600 * 5) + Cells(i - dc7, 31)
 
        Else
            Cells(i, 12) = Cells(i - dc1, 11) + Cells(i - dc7, 31)
        End If
 
    ElseIf dc7 > dc1 Then
 
        If i = 45 And i < (46 + dc1) Then
            Cells(i, 12) = (Cells(20, 6) / 3600 * 5) + (Cells(36, 3) * 2 / 3600 * 5)
 
        ElseIf i > (46 + dc1 - 1) And i < (46 + dc7) Then
            Cells(i, 12) = (Cells(36, 3) * 2 / 3600 * 5) + Cells(i - dc1, 11)
 
        Else
            Cells(i, 12) = Cells(i - dc1, 11) + Cells(i - dc7, 31)
        End If
 
    Else
 
        If i = 46 And i < (46 + dc1) Then
            Cells(i, 12) = (Cells(20, 6) / 3600 * 5) + (Cells(36, 3) * 2 / 3600 * 5)
 
        Else
            Cells(i, 12) = Cells(i - dc1, 11) + Cells(i - dc7, 31)
        End If
 
    End If
'############################ Bin1 out #######################################################
 
    If i = 46 Then
        Cells(i, 13) = Cells(22, 6) / 3600 * 5
 
    Else
        If (Cells(i - 1, 14) + Cells(i - 1, 12) - Cells(i - 1, 13)) < 0 Or (Cells(i - 1, 14) + Cells(i - 1, 12) - Cells(i - 1, 13)) = 0 Then
            Cells(i, 13) = Cells(i, 12)
        Else
            Cells(i, 13) = Cells(22, 6) / 3600 * 5
        End If
    End If
 
'############################ Bin1 level #####################################################
 
    If i = 46 Then
        Cells(i, 14) = Cells(28, 3) * Cells(27, 3)
 
    Else
 
        If (Cells(i - 1, 14) + Cells(i - 1, 12) - Cells(i - 1, 13)) < 0 Then
            Cells(i, 14) = 0
        Else
 
            If (Cells(i - 1, 14) + Cells(i - 1, 12) - Cells(i - 1, 13)) > Cells(27, 3) Then
                Cells(i, 14) = Cells(27, 3)
            Else
                Cells(i, 14) = Cells(i - 1, 14) + Cells(i - 1, 12) - Cells(i - 1, 13)
            End If
 
        End If
    End If
 
'############################ adjustement des fins dans Bin 1 ################################
 
    If Cells(i, 13) > Cells(i, 12) Then
 
        For j = 1 To t                                   'décalage
            rate_out_i = Cells(i, 13)
            Cells(j, 39) = Cells(j + rate_out_i, 39)
        Next
 
        ln = hn - Cells(i, 13)
        hn = hn - Cells(i, 13) + Cells(i, 12)
 
        If ln < 1 Then
        ln = 1
        End If
        If hn < 1 Then
        hn = 1
        End If
 
        hi = hn
        li = ln
 
 
        If Cells(i, 12) <= Cells(32, 6) / 3600 * 5 Then            'Bin 1 in plus petit ou egale a 4.4444
            For j = li To hi
                Cells(j, 39) = 0
            Next
            For j = (hi + 1) To t
                Cells(j, 39) = 0
            Next
        Else                                                'Bin 1 in plus garnd que 4.4444
            For j = li To hi
                Cells(j, 39) = 1
            Next
            For j = (hi + 1) To t
                Cells(j, 39) = 0
            Next
        End If
 
'----------------------------------------------
 
    ElseIf Cells(i, 13) = Cells(i, 12) Then
 
        For j = 1 To t                                  'décalage
            rate_out_i = Cells(i, 13)
            Cells(j, 39) = Cells(j + rate_out_i, 39)
        Next
 
        ln = hn - Cells(i, 13)
 
        If ln < 1 Then
        ln = 1
        End If
        If hn < 1 Then
        hn = 1
        End If
 
        hi = hn
        li = ln
 
        If Cells(i, 12) > Cells(32, 6) / 3600 * 5 Then                     'Bin 1 in plus grand que 4.4444
            For j = li To hi
                Cells(j, 39) = 1
            Next
            For j = (hi + 1) To t
                Cells(j, 39) = 0
            Next
        ElseIf Cells(i, 12) = Cells(32, 6) / 3600 * 5 Then                  'Bin 1 in est egal a 4.4444
            For j = li To hi
                Cells(j, 39) = 0
            Next
            For j = (hi + 1) To t
                Cells(j, 39) = 0
            Next
        End If
 
'-----------------------------------------
 
    Else                                                  'Cells(i, 13) < Cells(i, 12) Then
 
        For j = 1 To t                                    'décalage
            rate_out_i = Cells(i, 13)
            Cells(j, 39) = Cells(j + rate_out_i, 39)
        Next
 
        ln = hn - Cells(i, 12)
        hn = hn - Cells(i, 13) + Cells(i, 12)
 
        If ln > t Then
        ln = t
        End If
        If hn > t Then
        hn = t
        End If
        If ln < 1 Then
        ln = 1
        End If
        If hn < 1 Then
        hn = 1
        End If
 
        hi = hn
        li = ln
 
        If Cells(i, 12) > Cells(32, 6) / 3600 * 5 Then                     'Bin 1 in plus grand que 4.4444
            For j = li To hi
                Cells(j, 39) = 1
            Next
            For j = (hi + 1) To t
                Cells(j, 39) = 0
            Next
        ElseIf Cells(i, 12) = Cells(32, 6) / 3600 * 5 Then                  'Bin 1 in est egal a 4.4444
            For j = li To hi
                Cells(j, 39) = 0
            Next
            For j = (hi + 1) To t
                Cells(j, 39) = 0
            Next
        End If
 
    End If
 
'---------------------------------------
 
    Cells(i, 37) = Cells(1, 39)
 
'############################ con2 and conv3 charged on ######################################
 
    Cells(i, 16) = Cells(i, 13) / 2
    Cells(i, 17) = Cells(i, 13) / 2
 
'############################ screen1 in and screen2 in ######################################
 
    If i > 45 And i < (46 + dc2) Then             'screen1
        Cells(i, 18) = Cells(46, 13) / 2
 
    Else
        Cells(i, 18) = Cells(i - dc2, 16)
    End If
 
    If i > 45 And i < (46 + dc3) Then             'screen2 &&&&&& si convoyeur 2 et 3 sont de la meme longueur je pourrais dire que screen 1 égale screen 2.
        Cells(i, 19) = Cells(46, 13) / 2
 
    Else
        Cells(i, 19) = Cells(i - dc3, 17)
    End If
 
'#################################### Cacul de loverflow ######################################
 
    If i > 45 And i < (46 + db1 + dc1 + dc2) Then
        Cells(i, 20) = (Cells(22, 6) - Cells(32, 6)) / 3600 * 5 / 2 * Cells(28, 6)
    Else
 
        If Cells(i - dc2, 14) = 0 And Cells(i - dc1 - dc2, 11) = 0 Then                                 'si bin 1 est vide et conv 1 (D) est vide (+cells(23,12)est le delai du conv 2)
            Cells(i, 20) = 0                                                                                            'loverflow du screen est 0
 
        ElseIf Cells(i - dc2, 14) = 0 And Cells(i - dc1 - dc2, 11) > 0 Then                                 'si bin 1 est vide et conv 1 (D) est plein
            Cells(i, 20) = Cells(i - dc1 - dc2, 11) * Cells(28, 6) / 2                                  'loverflow est egal a la charge du conv 1(D) * %that goes overflow /2 (2 Screens)
 
        ElseIf Cells(i - dc2, 14) > 0 And Cells(i - dc2, 37) = 0 Then                                         'a reviser
            Cells(i, 20) = 0
 
        ElseIf Cells(i - dc2, 14) > 0 And Cells(i - dc2, 37) = 1 Then                                          'a reviser
            Cells(i, 20) = (Cells(22, 6) - Cells(32, 6)) / 3600 * 5 / 2 * Cells(28, 6)
        End If
 
    End If
 
    Cells(i, 21) = Cells(i, 20)                                                                          'for screen2 overflow = screen 1 overflow
 
'#################################### conv4 charge on ########################################
 
    Cells(i, 22) = Cells(i, 20) + Cells(i, 21)
 
'#################################### Bin 2 in ###############################################
 
    If i > 45 And i < (46 + dc4) Then
        Cells(i, 23) = Cells(32, 6) / 3600 * 5
    Else
        Cells(i, 23) = Cells(i - dc4, 22)
    End If
 
'#################################### Bin 2 out ##############################################
 
    If i > 45 And i < (46 + dc4) Then
        Cells(i, 24) = Cells(23, 6) / 3600 * 5
 
    Else
 
        If (Cells(i - 1, 25) + Cells(i - 1, 23) - Cells(i - 1, 24)) < 0 Or (Cells(i - 1, 25) + Cells(i - 1, 23) - Cells(i - 1, 24)) = 0 Then
            Cells(i, 24) = Cells(i, 23)
        Else
            Cells(i, 24) = Cells(23, 6) / 3600 * 5
        End If
 
    End If
 
'#################################### Bin 2 level ##############################################
 
 
    If i = 46 Then
        Cells(i, 25) = Cells(33, 3) * Cells(34, 3)
    Else
 
        If (Cells(i - 1, 25) + Cells(i - 1, 23) - Cells(i - 1, 24)) < 0 Or (Cells(i - 1, 25) + Cells(i - 1, 23) - Cells(i - 1, 24)) = 0 Then
            Cells(i, 25) = 0
        Else
            If (Cells(i - 1, 25) + Cells(i - 1, 23) - Cells(i - 1, 24)) > Cells(33, 3) Then
                Cells(i, 25) = Cells(33, 3)
            Else
                Cells(i, 25) = Cells(i - 1, 25) + Cells(i - 1, 23) - Cells(i - 1, 24)
            End If
        End If
    End If
 
'#################################### Conv-5 & Conv-6 charged on #############################
 
    Cells(i, 27) = Cells(i, 24) / 2
    Cells(i, 28) = Cells(i, 24) / 2
 
'#################################### Cone-1 & Cone-2 out ####################################
 
    If i > 45 And i < (45 + dc5) Then         'Cone-1
        Cells(i, 29) = Cells(36, 3) / 3600 * 5
 
    Else
        Cells(i, 29) = Cells(i, 27)
    End If
 
    If i > 45 And i < (45 + dc6) Then         'Cone-2
        Cells(i, 30) = Cells(36, 3) / 3600 * 5
 
    Else
        Cells(i, 30) = Cells(i, 28)
    End If
 
'#################################### Conv-7 charged on ######################################
 
    Cells(i, 31) = Cells(i, 29) + Cells(i, 30)
 
'#################################### Screen-1 Underflow #####################################
 
    Cells(i, 32) = Cells(i, 18) - Cells(i, 20)
 
'#################################### Screen-2 Underflow #####################################
 
    Cells(i, 33) = Cells(i, 19) - Cells(i, 21)
 
'#################################### Conv-8 charged on ######################################
 
    Cells(i, 34) = Cells(i, 32) + Cells(i, 33)
 
'#################################### Stock piles in #########################################
 
    If i > 45 And i < (46 + dc8) Then
        Cells(i, 35) = Cells(19, 3) / 3600 * 5
    Else
        Cells(i, 35) = Cells(i - dc8, 34)
    End If
 
'#############################################################################################
 
i = i + 1
Loop
 
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
 
End Sub