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 :

Modification d'une variable durant l'exécution d'un code


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut Modification d'une variable durant l'exécution d'un code
    Bonjour,
    Est-il possible de modifier une variable durant l'exécution d'un code.

    Par exemple, si j'ai une boucle:

    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
     
    Dim i As Integer, MaVariable As Long, MonResultat As Long
     
    MaVariable = Range("A1").Value
     
    i = 2
    While i <= 1000
     
    MonResultat = MaVariable + i
     
    Range("A" & i).Value = MonResultat
     
    i = i + 1
     
    Wend
    Existe-t-il un moyen pour pouvoir intervenir sur MaVariable durant le déroulement de la macro?

    Un UserForm ou une InPutBox qui resterait ouvert durant le code et dans lequel je pourrais inscrire MaVariable = (ce que je veux) à tout moment? Ou qui se rafraichirait à chaque boucle pour m'offrir cette possibilité (sans que le code soit trop ralenti?

    Ou tout autre solution.


    Je vous remercie pour tout aide!!!!!!!!

  2. #2
    Membre Expert Avatar de Transitoire
    Homme Profil pro
    Auditeur informatique
    Inscrit en
    Décembre 2017
    Messages
    733
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Auditeur informatique

    Informations forums :
    Inscription : Décembre 2017
    Messages : 733
    Par défaut
    Bonjour, je comprend pas?
    Vous avez une boucle, elle va mettre peut être 2 secondes à s'effectuer et vous dites:
    Existe-t-il un moyen pour pouvoir intervenir sur MaVariable durant le déroulement de la macro?
    Faudrais vous entrainer avec Lucky luke?
    Vous pouvez installer dans la boucle un MsgBox ou un inputBox, mais si vous avez 900 passages, bonjour l'angoisse.
    Pardonnez moi, mais je ne vois pas trop ce que vous pourriez faire compte tenu du fait que même un userform non modal; ne vous laisserai pas le temps de réaction!!!!
    Mais je peut me tromper?
    Cordialement

  3. #3
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut
    La boucle est en fait un peu plus longue (plusieurs milliers de lignes), j'ai simplifié, et la modification que je ferai sera à prendre en compte dans le calcul n lignes plus loin

    Dans le Userform (par exemple), j'aurai un textbox fixe dans lequel je pourrais rentrer une valeur x qui modifierait le calcul n lignes plus loin, le temps que mon action soit prise en compte.

    J'ai mis un Userform avec mais si je clic dedans, l'affichage se fige et le calcul continue à tourner sans que je ne puisse plus intervenir jusqu'à la fin du code.

    Même sans cliquer, l'affichage se fige

    Le code en question si c'est plus clair:

    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
    Option Explicit
    Sub GRAVIT()
     
    Application.Volatile
     
    Range("C9").Clear
     
    Dim XtempsX_debut As Single
       Dim XdureeX As Single
     
       XtempsX_debut = Timer
      Range("B7").Value = Format(XtempsX_debut / 86400, "hh:mm:ss")
     
     
     Pas = Range("B17").Value
     MA = Range("A2").Value
     MB = Range("B2").Value
     MC = Range("C2").Value
     G = 6.67408 ^ 10 - 11
     
     xA = Range("B27").Value
     yA = Range("B28").Value
     zA = Range("B29").Value
     xB = Range("B31").Value
     yB = Range("B32").Value
     zB = Range("B33").Value
     xC = Range("B35").Value
     yC = Range("B36").Value
     zC = Range("B37").Value
     vxA = Range("B41").Value
     vyA = Range("B42").Value
     vzA = Range("B43").Value
     vxB = Range("B45").Value
     vyB = Range("B46").Value
     vzB = Range("B47").Value
     vxC = Range("B49").Value
     vyC = Range("B50").Value
     vzC = Range("B51").Value
     
     
     
     
    If Range("A15").Value = "OK" Then
     
     UserForm1.Show vbModeless
     
     
    Range("D2:AD100000").Clear
    Range("B8:B9").Clear
    Range("B11").ClearContents
     
    i = 2
    jusk = Range("B10").Value
    While i = 2
     
     
     
     Range("D" & i).Select
     
     
     
    Range("B11").Value = (ActiveCell.Row) / jusk * 100
     
    Application.ScreenUpdating = False
     
    Dim temps_debut As Single
       Dim duree As Single
     
     
       temps_debut = Timer
     
     
     
     
     
     
     
    axA = G * (((MB * (xB - xA)) / ((((xB - xA) ^ 2 + (yB - yA) ^ 2 + (zB - zA) ^ 2) ^ 1 / 2) ^ 3) + (MC * (xC - xA)) / ((((xC - xA) ^ 2 + (yC - yA) ^ 2 + (zC - zA) ^ 2) ^ 1 / 2) ^ 3)))
    ayA = G * (((MB * (yB - yA)) / ((((xB - xA) ^ 2 + (yB - yA) ^ 2 + (zB - zA) ^ 2) ^ 1 / 2) ^ 3) + (MC * (yC - yA)) / ((((xC - xA) ^ 2 + (yC - yA) ^ 2 + (zC - zA) ^ 2) ^ 1 / 2) ^ 3)))
    azA = G * (((MB * (zB - zA)) / ((((xB - xA) ^ 2 + (yB - yA) ^ 2 + (zB - zA) ^ 2) ^ 1 / 2) ^ 3) + (MC * (zC - zA)) / ((((xC - xA) ^ 2 + (yC - yA) ^ 2 + (zC - zA) ^ 2) ^ 1 / 2) ^ 3)))
    axB = G * (((MA * (xA - xB)) / ((((xA - xB) ^ 2 + (yA - yB) ^ 2 + (zA - zB) ^ 2) ^ 1 / 2) ^ 3) + (MC * (xC - xB)) / ((((xC - xB) ^ 2 + (yC - yB) ^ 2 + (zC - zB) ^ 2) ^ 1 / 2) ^ 3)))
    ayB = G * (((MA * (xA - xB)) / ((((xA - xB) ^ 2 + (yA - yB) ^ 2 + (zA - zB) ^ 2) ^ 1 / 2) ^ 3) + (MC * (yC - xB)) / ((((xC - xB) ^ 2 + (yC - yB) ^ 2 + (zC - zB) ^ 2) ^ 1 / 2) ^ 3)))
    azB = G * (((MA * (xA - xB)) / ((((xA - xB) ^ 2 + (yA - yB) ^ 2 + (zA - zB) ^ 2) ^ 1 / 2) ^ 3) + (MC * (xC - xB)) / ((((xC - xB) ^ 2 + (yC - yB) ^ 2 + (zC - zB) ^ 2) ^ 1 / 2) ^ 3)))
    axC = G * (((MA * (xA - xC)) / ((((xA - xC) ^ 2 + (yA - yC) ^ 2 + (zA - zC) ^ 2) ^ 1 / 2) ^ 3) + (MB * (xB - xC)) / ((((xB - xC) ^ 2 + (yB - yC) ^ 2 + (zB - zC) ^ 2) ^ 1 / 2) ^ 3)))
    ayC = G * (((MA * (xA - xC)) / ((((xA - xC) ^ 2 + (yA - yC) ^ 2 + (zA - zC) ^ 2) ^ 1 / 2) ^ 3) + (MB * (xB - xC)) / ((((xB - xC) ^ 2 + (yB - yC) ^ 2 + (zB - zC) ^ 2) ^ 1 / 2) ^ 3)))
    azC = G * (((MA * (xA - xC)) / ((((xA - xC) ^ 2 + (yA - yC) ^ 2 + (zA - zC) ^ 2) ^ 1 / 2) ^ 3) + (MB * (xB - xC)) / ((((xB - xC) ^ 2 + (yB - yC) ^ 2 + (zB - zC) ^ 2) ^ 1 / 2) ^ 3)))
     
     
     
     Range("V" & i).Value = axA
     Range("W" & i).Value = ayA
     Range("X" & i).Value = azA
     Range("Y" & i).Value = axB
     Range("Z" & i).Value = ayB
     Range("AA" & i).Value = azB
     Range("AB" & i).Value = axC
     Range("AC" & i).Value = ayC
     Range("AD" & i).Value = azC
     
    DEMIT = Range("AE" & i).Value
     
     
     
    vxA = vxA + axA * DEMIT
    vyA = vyA + ayA * DEMIT
    vzA = vzA + azA * DEMIT
    vxB = vxB + axB * DEMIT
    vyB = vyB + ayB * DEMIT
    vzB = vzB + azB * DEMIT
    vxC = vxC + axC * DEMIT
    vyC = vyC + ayC * DEMIT
    vzC = vzC + azC * DEMIT
     
     
     
     Range("M" & i).Value = vxA
     Range("N" & i).Value = vyA
     Range("O" & i).Value = vzA
     Range("P" & i).Value = vxB
     Range("Q" & i).Value = vyB
     Range("R" & i).Value = vzB
     Range("S" & i).Value = vxC
     Range("T" & i).Value = vyC
     Range("U" & i).Value = vzC
     
     UserForm1.Repaint
     
     UserForm1.Label10.Caption = vxA
     UserForm1.Label11.Caption = vyA
     UserForm1.Label12.Caption = vzA
     UserForm1.Label13.Caption = vxB
     UserForm1.Label14.Caption = vyB
     UserForm1.Label15.Caption = vzB
     UserForm1.Label16.Caption = vxC
     UserForm1.Label17.Caption = vyC
     UserForm1.Label18.Caption = vzC
     
     If UserForm1.CommandButton1.Value = "Vrai" Then
     Unload UserForm1
     Exit Sub
     Else
     
     
     T = Range("AE" & i + 1).Value
     
     xA = xA + vxA * T
     yA = yA + vyA * T
     zA = zA + vzA * T
     xB = xB + vxB * T
     yB = yB + vyB * T
     zB = zB + vzB * T
     xC = xC + vxC * T
     yC = yC + vyC * T
     zC = zC + vzC * T
     
     Application.ScreenUpdating = True
     
     Range("D" & i).Value = xA
     Range("E" & i).Value = yA
     Range("F" & i).Value = zA
     Range("G" & i).Value = xB
     Range("H" & i).Value = yB
     Range("I" & i).Value = zB
     Range("J" & i).Value = xC
     Range("K" & i).Value = yC
     Range("L" & i).Value = zC
     
     
     
     xA = Range("D" & i).Value
     yA = Range("E" & i).Value
     zA = Range("F" & i).Value
     xB = Range("G" & i).Value
     yB = Range("H" & i).Value
     zB = Range("I" & i).Value
     xC = Range("J" & i).Value
     yC = Range("K" & i).Value
     zC = Range("L" & i).Value
     
     
     
     
       duree = Timer - temps_debut
     
     
     
     
       Range("B8").Value = Format(duree / 86400, "hh:mm:ss,ms")
       Range("B9").Value = Format(duree * jusk / 86400, "hh:mm:ss,ms")
     
     Application.ScreenUpdating = False
     
     
     
      i = i + 1
     
      End If
     
      Wend
     
      j = i
     
    jusk = Range("B10").Value
    While j <= jusk
     
     
     Range("D" & j).Select
     
     
       Range("B11").Value = (ActiveCell.Row) / jusk * 100
     
    Application.ScreenUpdating = False
     
     
    axA = G * (((MB * (xB - xA)) / ((((xB - xA) ^ 2 + (yB - yA) ^ 2 + (zB - zA) ^ 2) ^ 1 / 2) ^ 3) + (MC * (xC - xA)) / ((((xC - xA) ^ 2 + (yC - yA) ^ 2 + (zC - zA) ^ 2) ^ 1 / 2) ^ 3)))
    ayA = G * (((MB * (yB - yA)) / ((((xB - xA) ^ 2 + (yB - yA) ^ 2 + (zB - zA) ^ 2) ^ 1 / 2) ^ 3) + (MC * (yC - yA)) / ((((xC - xA) ^ 2 + (yC - yA) ^ 2 + (zC - zA) ^ 2) ^ 1 / 2) ^ 3)))
    azA = G * (((MB * (zB - zA)) / ((((xB - xA) ^ 2 + (yB - yA) ^ 2 + (zB - zA) ^ 2) ^ 1 / 2) ^ 3) + (MC * (zC - zA)) / ((((xC - xA) ^ 2 + (yC - yA) ^ 2 + (zC - zA) ^ 2) ^ 1 / 2) ^ 3)))
    axB = G * (((MA * (xA - xB)) / ((((xA - xB) ^ 2 + (yA - yB) ^ 2 + (zA - zB) ^ 2) ^ 1 / 2) ^ 3) + (MC * (xC - xB)) / ((((xC - xB) ^ 2 + (yC - yB) ^ 2 + (zC - zB) ^ 2) ^ 1 / 2) ^ 3)))
    ayB = G * (((MA * (xA - xB)) / ((((xA - xB) ^ 2 + (yA - yB) ^ 2 + (zA - zB) ^ 2) ^ 1 / 2) ^ 3) + (MC * (yC - xB)) / ((((xC - xB) ^ 2 + (yC - yB) ^ 2 + (zC - zB) ^ 2) ^ 1 / 2) ^ 3)))
    azB = G * (((MA * (xA - xB)) / ((((xA - xB) ^ 2 + (yA - yB) ^ 2 + (zA - zB) ^ 2) ^ 1 / 2) ^ 3) + (MC * (xC - xB)) / ((((xC - xB) ^ 2 + (yC - yB) ^ 2 + (zC - zB) ^ 2) ^ 1 / 2) ^ 3)))
    axC = G * (((MA * (xA - xC)) / ((((xA - xC) ^ 2 + (yA - yC) ^ 2 + (zA - zC) ^ 2) ^ 1 / 2) ^ 3) + (MB * (xB - xC)) / ((((xB - xC) ^ 2 + (yB - yC) ^ 2 + (zB - zC) ^ 2) ^ 1 / 2) ^ 3)))
    ayC = G * (((MA * (xA - xC)) / ((((xA - xC) ^ 2 + (yA - yC) ^ 2 + (zA - zC) ^ 2) ^ 1 / 2) ^ 3) + (MB * (xB - xC)) / ((((xB - xC) ^ 2 + (yB - yC) ^ 2 + (zB - zC) ^ 2) ^ 1 / 2) ^ 3)))
    azC = G * (((MA * (xA - xC)) / ((((xA - xC) ^ 2 + (yA - yC) ^ 2 + (zA - zC) ^ 2) ^ 1 / 2) ^ 3) + (MB * (xB - xC)) / ((((xB - xC) ^ 2 + (yB - yC) ^ 2 + (zB - zC) ^ 2) ^ 1 / 2) ^ 3)))
     
     Range("V" & j).Value = axA
     Range("W" & j).Value = ayA
     Range("X" & j).Value = azA
     Range("Y" & j).Value = axB
     Range("Z" & j).Value = ayB
     Range("AA" & j).Value = azB
     Range("AB" & j).Value = axC
     Range("AC" & j).Value = ayC
     Range("AD" & j).Value = azC
     
    DEMIT = Range("AE" & j).Value
     
    vxA = vxA + axA * DEMIT
    vyA = vyA + ayA * DEMIT
    vzA = vzA + azA * DEMIT
    vxB = vxB + axB * DEMIT
    vyB = vyB + ayB * DEMIT
    vzB = vzB + azB * DEMIT
    vxC = vxC + axC * DEMIT
    vyC = vyC + ayC * DEMIT
    vzC = vzC + azC * DEMIT
     
     Range("M" & j).Value = vxA
     Range("N" & j).Value = vyA
     Range("O" & j).Value = vzA
     Range("P" & j).Value = vxB
     Range("Q" & j).Value = vyB
     Range("R" & j).Value = vzB
     Range("S" & j).Value = vxC
     Range("T" & j).Value = vyC
     Range("U" & j).Value = vzC
     
     UserForm1.Repaint
     
     UserForm1.Label10.Caption = vxA
     UserForm1.Label11.Caption = vyA
     UserForm1.Label12.Caption = vzA
     UserForm1.Label13.Caption = vxB
     UserForm1.Label14.Caption = vyB
     UserForm1.Label15.Caption = vzB
     UserForm1.Label16.Caption = vxC
     UserForm1.Label17.Caption = vyC
     UserForm1.Label18.Caption = vzC
     
     If UserForm1.CommandButton1.Value = "Vrai" Then
     Unload UserForm1
     Exit Sub
     Else
     
     T = Range("AE" & j + 1).Value
     
     xA = xA + vxA * T
     yA = yA + vyA * T
     zA = zA + vzA * T
     xB = xB + vxB * T
     yB = yB + vyB * T
     zB = zB + vzB * T
     xC = xC + vxC * T
     yC = yC + vyC * T
     zC = zC + vzC * T
     
     Application.ScreenUpdating = True
     
     Range("D" & j).Value = xA
     Range("E" & j).Value = yA
     Range("F" & j).Value = zA
     Range("G" & j).Value = xB
     Range("H" & j).Value = yB
     Range("I" & j).Value = zB
     Range("J" & j).Value = xC
     Range("K" & j).Value = yC
     Range("L" & j).Value = zC
     
     Application.ScreenUpdating = False
     
     j = j + 1
     
     
     
     
     End If
     
     Wend
     
     
     XdureeX = Timer - XtempsX_debut
     
     
     
       Range("C9").Value = Format(XdureeX / 86400, "hh:mm:ss,ms")
     
     
     
     
     
    Else
     
     
    MsgBox Range("A10").Value & " doit être " & Range("A11").Value & " " & Range("A12").Value
    Exit Sub
     
     
    End If
     
     
    ThisWorkbook.Save
     
     
    End Sub
    Je cherche donc à tracer les trajectoires de 3 corps A; B; C sont une formule de la gravitation.

    Je cherche à intervenir sur les vitesses des 3 corps durant l'exécution du code (qui peut se dérouler sur 10 000 lignes, ce qui prend ~20 minutes, avec une boucle à ~120 millisecondes)

  4. #4
    Membre Expert
    Avatar de pijaku
    Homme Profil pro
    Inscrit en
    Août 2010
    Messages
    1 817
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Août 2010
    Messages : 1 817
    Billets dans le blog
    10
    Par défaut
    Bonjour,

    Si tu connais à l'avance les indices de lignes ou ta variable doit changer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Sub Macro2()
    Dim i As Long, mavariable As String
       mavariable = "toto"
       For i = 1 To 1000000
          If i Mod 200000 = 0 Then
             mavariable = InputBox("Saisie à l'indice de boucle : " & i, "Nouvelle variable", mavariable)
          End If
       Next
    End Sub

  5. #5
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut
    Non, malheureusement, c'est en réaction à l'évolution des résultats que je souhaite intervenir sur mes variable.

  6. #6
    Membre Expert
    Avatar de pijaku
    Homme Profil pro
    Inscrit en
    Août 2010
    Messages
    1 817
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Nord (Nord Pas de Calais)

    Informations forums :
    Inscription : Août 2010
    Messages : 1 817
    Billets dans le blog
    10
    Par défaut
    c'est en réaction à l'évolution des résultats
    Est-ce que cette évolution est "prévisible"?
    Si oui, tu dois pouvoir déterminer à quel moment elle aura lieu.
    Et donc, via un test if, proposer une saisie.


    Si non, ça me semble plus que compromis ton histoire.

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

Discussions similaires

  1. Réponses: 33
    Dernier message: 28/04/2010, 04h44
  2. Sauvegarder les modifications sur une variable
    Par maaike dans le forum VBA Access
    Réponses: 5
    Dernier message: 21/08/2007, 23h28
  3. Modification d'une variable & rafraichissement! HELP!
    Par u115php dans le forum Langage
    Réponses: 4
    Dernier message: 09/09/2006, 01h04
  4. [Eclipse 3.1]Modification d'une variable
    Par thecaptain dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 18/01/2006, 09h17
  5. Réponses: 2
    Dernier message: 29/08/2005, 16h35

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