IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Optimisation du temps d'exécution d'un code vba [XL-2007]


Sujet :

Macros et VBA Excel

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Stagiaire en génie des procédés
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Stagiaire en génie des procédés
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Points : 8
    Points
    8
    Par défaut Optimisation du temps d'exécution d'un code vba
    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

  2. #2
    Membre chevronné Avatar de ZebreLoup
    Homme Profil pro
    Ingénieur Financier
    Inscrit en
    Mars 2010
    Messages
    994
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur Financier
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 994
    Points : 2 131
    Points
    2 131
    Par défaut
    Bonjour,

    J'avoue que je n'ai pas eu le courage de lire toute la procédure, mais le truc, c'est de passer par un tableau. Tu définis toute la zone sur laquelle tu va travailler et tu la mets dans un Range. Tu définis ensuite un Variant et tu mets le Range dedans. Tu travailles sur le Variant, il n'y a plus d'accès à la feuille et ça va 10 fois plus vite. A la fin tu remets le Variant dans le Range d'un seul coup.

    Un exemple fictif
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Dim rg As Range
    Dim v as Variant
    Set rg = Worksheets("MaFeuille").Range("A1:G10000")
    v = rg
    'On travaille sur v ...
    rg = v

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Stagiaire en génie des procédés
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Stagiaire en génie des procédés
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Points : 8
    Points
    8
    Par défaut
    Ok, ça semble plutôt simple, mais de quel façon puis-je accèder aux locations de mon variant?

  4. #4
    Membre chevronné Avatar de ZebreLoup
    Homme Profil pro
    Ingénieur Financier
    Inscrit en
    Mars 2010
    Messages
    994
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur Financier
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 994
    Points : 2 131
    Points
    2 131
    Par défaut
    Après c'est un tableau en fait, tu fais
    Il me semble juste que par défaut le premier indice est 1, contrairement aux tableaux créés à partir de rien

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Stagiaire en génie des procédés
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Stagiaire en génie des procédés
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Points : 8
    Points
    8
    Par défaut
    Merci beaucoup. Maintenant ca ne prend même pas 5 secondes pour complété le calcul pour toute la feuille!!!

  6. #6
    Membre chevronné Avatar de ZebreLoup
    Homme Profil pro
    Ingénieur Financier
    Inscrit en
    Mars 2010
    Messages
    994
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur Financier
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 994
    Points : 2 131
    Points
    2 131
    Par défaut
    Cool !

    Règle d'or : évitez au maximum les accès aux feuilles

  7. #7
    Futur Membre du Club
    Homme Profil pro
    Stagiaire en génie des procédés
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Stagiaire en génie des procédés
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Points : 8
    Points
    8
    Par défaut
    J'ai maintenant un autre petit problème;

    Est-t-il possible à la fin de mon code d'écrire que seulement une partie de mon variant sera égale à une partie de mon range sur ma feuille afin de ne pas tout effacer les formules utiles dèjà entrées sur ma feuille?

    Quelque chose comme:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Range("L12:G10000") = v(''L12:G10000'')

  8. #8
    Membre chevronné Avatar de ZebreLoup
    Homme Profil pro
    Ingénieur Financier
    Inscrit en
    Mars 2010
    Messages
    994
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur Financier
    Secteur : Finance

    Informations forums :
    Inscription : Mars 2010
    Messages : 994
    Points : 2 131
    Points
    2 131
    Par défaut
    On ne peut pas faire comme ça, mais on peut toujours créer un deuxième variant plus petit dans lequel on ne copie que ce que l'on va écrire ensuite.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Optimisation de temps d'exécution d'un Code VBA
    Par Adilleroy dans le forum Macros et VBA Excel
    Réponses: 18
    Dernier message: 26/07/2010, 13h16
  2. [datenum] Optimiser le temps d'exécution
    Par xduris dans le forum MATLAB
    Réponses: 11
    Dernier message: 08/08/2007, 16h07
  3. [Fait]Temps d'exécution d'un code
    Par JeremieT dans le forum Contribuez
    Réponses: 2
    Dernier message: 30/08/2006, 06h20
  4. optimiser le temps d'exécution de l'explorateur windows
    Par ben_iap dans le forum Autres Logiciels
    Réponses: 6
    Dernier message: 31/01/2006, 22h04
  5. Réponses: 9
    Dernier message: 20/06/2005, 12h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo