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 :

Boucle dans modules differents


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    119
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 119
    Par défaut Boucle dans modules differents
    bonjour

    je souhaiterai savoir si il est possible de commencer une boucle dans un module (for i= 1 to ...) et de la terminer dans un autre (next i)?
    j'ai un script qui est trop long j'ai du le scinder en 2 modules

    merci de vos lumières

  2. #2
    Membre Expert
    Femme Profil pro
    Ingénieur
    Inscrit en
    Octobre 2016
    Messages
    1 703
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 30
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Ingénieur
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2016
    Messages : 1 703
    Par défaut
    Ce serait plus facile de répondre en ayant une idée du code, mais a priori, je vois mal comment ça pourrait être possible ...
    Est-ce que tu ne pourrais pas plutôt créer un sub ayant pour paramètre i (et éventuellement d'autres paramètres) que tu appelerais dans la boucle. Quelque chose comme :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Sub principal
       Dim i as integer
       For i = 1 to ...
          Call methode1(i)
          Call methode2(i)
       Next i
    End Sub

  3. #3
    Expert confirmé

    Homme Profil pro
    Curieux
    Inscrit en
    Juillet 2012
    Messages
    5 169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Curieux
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2012
    Messages : 5 169
    Billets dans le blog
    5
    Par défaut
    Citation Envoyé par ricoree78 Voir le message
    j'ai un script qui est trop long j'ai du le scinder en 2 modules
    Bonjour,

    il est là ton problème exact, et la solution t'as été génériquement soufflée dans le message précédent

    plutôt que de couper un unique programme en deux de façon un peu brute, il faut :

    - faire le schéma des actions réalisées par le programme
    - en définir des blocs de traitements
    - chaque bloc = une sous-procédure
    - tu crées une procédure principale qui viendra emboiter chaque sous-traitement : c'est cette procédure principale qui porte ta bloc For/Next et qui distribue les ordres/actions à tes sous-procédure qui seront appelées à chaque tour de boucle


    revient avec cette base, le schéma des tâches découpées.
    ainsi que le code que tu auras essayé de faire, ce n'est que là que nous pourrons être réellement utiles et efficaces pour de donner les bonnes clés

  4. #4
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    119
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 119
    Par défaut
    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
    Dim i As Integer
     
    Application.ScreenUpdating = False
    Sheets("toto1").Select
    Erase tableau
    nbligne = Range("A1").End(xlDown).Row
    ReDim tableau(nbligne - 2, 165)
     
    For i = 0 To nbligne - 2
    tableau(i, 0) = "***"
    tableau(i, 1) = "CAE"
    tableau(i, 2) = Range("A" & i + 2)
    tableau(i, 3) = Range("B" & i + 2)
    tableau(i, 4) = Range("C" & i + 2)
    tableau(i, 5) = Range("D" & i + 2)
    tableau(i, 6) = Range("E" & i + 2)
    tableau(i, 7) = Range("F" & i + 2)
    tableau(i, 8) = Range("G" & i + 2)
    tableau(i, 9) = Range("H" & i + 2)
    tableau(i, 10) = Range("I" & i + 2)
    tableau(i, 11) = Range("J" & i + 2)
    tableau(i, 12) = Range("K" & i + 2)
    tableau(i, 13) = Range("L" & i + 2)
    tableau(i, 14) = Range("M" & i + 2)
    tableau(i, 15) = Range("N" & i + 2)
    tableau(i, 16) = Range("O" & i + 2)
    tableau(i, 17) = Range("P" & i + 2)
    tableau(i, 18) = Range("Q" & i + 2)
    tableau(i, 19) = Range("R" & i + 2)
    tableau(i, 20) = Range("S" & i + 2)
    tableau(i, 21) = Range("T" & i + 2)
    tableau(i, 22) = Range("U" & i + 2)
    tableau(i, 23) = Range("V" & i + 2)
    tableau(i, 24) = Range("W" & i + 2)
    tableau(i, 25) = Range("X" & i + 2)
    tableau(i, 26) = Range("Y" & i + 2)
    tableau(i, 27) = Range("Z" & i + 2)
    tableau(i, 28) = Range("AA" & i + 2)
    tableau(i, 29) = Range("AB" & i + 2)
    tableau(i, 30) = Range("AC" & i + 2)
    tableau(i, 31) = Range("AD" & i + 2)
    tableau(i, 32) = Range("AE" & i + 2)
    tableau(i, 33) = Range("AF" & i + 2)
    tableau(i, 34) = Range("AG" & i + 2)
    tableau(i, 35) = Range("AH" & i + 2)
    tableau(i, 36) = Range("AI" & i + 2)
    tableau(i, 37) = Range("AJ" & i + 2)
    tableau(i, 38) = Range("AK" & i + 2)
    tableau(i, 39) = Range("AL" & i + 2)
    tableau(i, 40) = Range("AM" & i + 2)
    tableau(i, 41) = Range("AN" & i + 2)
    tableau(i, 42) = Range("AO" & i + 2)
    tableau(i, 43) = Range("AP" & i + 2)
    tableau(i, 44) = Range("AQ" & i + 2)
    tableau(i, 45) = Range("AR" & i + 2)
    tableau(i, 46) = Range("AS" & i + 2)
    tableau(i, 47) = Range("AT" & i + 2)
    tableau(i, 48) = Range("AU" & i + 2)
    tableau(i, 49) = Range("AV" & i + 2)
    tableau(i, 50) = Range("AW" & i + 2)
    tableau(i, 51) = Range("AX" & i + 2)
    tableau(i, 52) = Range("AY" & i + 2)
    tableau(i, 53) = Range("AZ" & i + 2)
    tableau(i, 54) = Range("BA" & i + 2)
    tableau(i, 55) = Range("BB" & i + 2)
    tableau(i, 56) = Range("BC" & i + 2)
    tableau(i, 57) = Range("BD" & i + 2)
    tableau(i, 58) = Range("BE" & i + 2)
    tableau(i, 59) = Range("BF" & i + 2)
    tableau(i, 60) = Range("BG" & i + 2)
    tableau(i, 61) = Range("BH" & i + 2)
    tableau(i, 62) = Range("BI" & i + 2)
    tableau(i, 63) = Range("BJ" & i + 2)
    tableau(i, 64) = Range("BK" & i + 2)
    tableau(i, 65) = Range("BL" & i + 2)
    tableau(i, 66) = Range("BM" & i + 2)
    tableau(i, 67) = Range("BN" & i + 2)
    tableau(i, 68) = Range("BO" & i + 2)
    tableau(i, 69) = Range("BP" & i + 2)
    tableau(i, 70) = Range("BQ" & i + 2)
    tableau(i, 71) = Range("BR" & i + 2)
    tableau(i, 72) = Range("BS" & i + 2)
    tableau(i, 73) = Range("BT" & i + 2)
    tableau(i, 74) = Range("BU" & i + 2)
    tableau(i, 75) = Range("BV" & i + 2)
    tableau(i, 76) = Range("BW" & i + 2)
    tableau(i, 77) = Range("BX" & i + 2)
    tableau(i, 78) = Range("BY" & i + 2)
    tableau(i, 79) = Range("BZ" & i + 2)
    tableau(i, 80) = Range("CA" & i + 2)
    tableau(i, 81) = Range("CB" & i + 2)
    tableau(i, 82) = Range("CC" & i + 2)
    tableau(i, 83) = Range("CD" & i + 2)
    tableau(i, 84) = Range("CE" & i + 2)
    tableau(i, 85) = Range("CF" & i + 2)
    tableau(i, 86) = Range("CG" & i + 2)
    tableau(i, 87) = Range("CH" & i + 2)
    tableau(i, 88) = Range("CI" & i + 2)
    tableau(i, 89) = Range("CJ" & i + 2)
    tableau(i, 90) = Range("CK" & i + 2)
    tableau(i, 91) = Range("CL" & i + 2)
    tableau(i, 92) = Range("CM" & i + 2)
    tableau(i, 93) = Range("CN" & i + 2)
    tableau(i, 94) = Range("CO" & i + 2)
    tableau(i, 95) = Range("CP" & i + 2)
    tableau(i, 96) = Range("CQ" & i + 2)
    tableau(i, 97) = Range("CR" & i + 2)
    tableau(i, 98) = Range("CS" & i + 2)
    tableau(i, 99) = Range("CT" & i + 2)
    tableau(i, 100) = Range("CU" & i + 2)
    tableau(i, 101) = Range("CV" & i + 2)
    tableau(i, 102) = Range("CW" & i + 2)
    tableau(i, 103) = Range("CX" & i + 2)
    tableau(i, 104) = Range("CY" & i + 2)
    tableau(i, 105) = Range("CZ" & i + 2)
    tableau(i, 106) = Range("DA" & i + 2)
    tableau(i, 107) = Range("DB" & i + 2)
    tableau(i, 108) = Range("DC" & i + 2)
    tableau(i, 109) = Range("DD" & i + 2)
    tableau(i, 110) = Range("DE" & i + 2)
    tableau(i, 111) = Range("DF" & i + 2)
    tableau(i, 112) = Range("DG" & i + 2)
    tableau(i, 113) = Range("DH" & i + 2)
    tableau(i, 114) = Range("DI" & i + 2)
    tableau(i, 115) = Range("DJ" & i + 2)
    tableau(i, 116) = Range("DK" & i + 2)
    tableau(i, 117) = Range("DL" & i + 2)
    tableau(i, 118) = Range("DM" & i + 2)
    tableau(i, 119) = Range("DN" & i + 2)
    tableau(i, 120) = Range("DO" & i + 2)
    tableau(i, 121) = Range("DP" & i + 2)
    tableau(i, 122) = Range("DQ" & i + 2)
    tableau(i, 123) = Range("DR" & i + 2)
    tableau(i, 124) = Range("DS" & i + 2)
    tableau(i, 125) = Range("DT" & i + 2)
    tableau(i, 126) = Range("DU" & i + 2)
    tableau(i, 127) = Range("DV" & i + 2)
    tableau(i, 128) = Range("DW" & i + 2)
    tableau(i, 129) = Range("DX" & i + 2)
    tableau(i, 130) = Range("DY" & i + 2)
    tableau(i, 131) = Range("DZ" & i + 2)
    tableau(i, 132) = Range("EA" & i + 2)
    tableau(i, 133) = Range("EB" & i + 2)
    tableau(i, 134) = Range("EC" & i + 2)
    tableau(i, 135) = Range("ED" & i + 2)
    tableau(i, 136) = Range("EE" & i + 2)
    tableau(i, 137) = Range("EF" & i + 2)
    tableau(i, 138) = Range("EG" & i + 2)
    tableau(i, 139) = Range("EH" & i + 2)
    tableau(i, 140) = Range("EI" & i + 2)
    tableau(i, 141) = Range("EJ" & i + 2)
    tableau(i, 142) = Range("EK" & i + 2)
    tableau(i, 143) = Range("EL" & i + 2)
    tableau(i, 144) = Range("EM" & i + 2)
    tableau(i, 145) = Range("EN" & i + 2)
    tableau(i, 146) = Range("EO" & i + 2)
    tableau(i, 147) = Range("EP" & i + 2)
    tableau(i, 148) = Range("EQ" & i + 2)
    tableau(i, 149) = Range("ER" & i + 2)
    tableau(i, 150) = Range("ES" & i + 2)
    tableau(i, 151) = Range("ET" & i + 2)
    tableau(i, 152) = Range("EU" & i + 2)
    tableau(i, 153) = Range("EV" & i + 2)
    tableau(i, 154) = Range("EW" & i + 2)
    Next i
     
    For i = 0 To nbligne - 2
     
    'code auxilliaire
    Select Case Len(tableau(i, 2))
    Case Is = 0
    tableau(i, 2) = tableau(i, 2) & "                 "
    Case Is = 1
    tableau(i, 2) = tableau(i, 2) & "                "
    Case Is = 2
    tableau(i, 2) = tableau(i, 2) & "               "
    Case Is = 3
    tableau(i, 2) = tableau(i, 2) & "              "
    Case Is = 4
    tableau(i, 2) = tableau(i, 2) & "             "
    Case Is = 5
    tableau(i, 2) = tableau(i, 2) & "            "
    Case Is = 6
    tableau(i, 2) = tableau(i, 2) & "           "
    Case Is = 7
    tableau(i, 2) = tableau(i, 2) & "          "
    Case Is = 8
    tableau(i, 2) = tableau(i, 2) & "         "
    Case Is = 9
    tableau(i, 2) = tableau(i, 2) & "        "
    Case Is = 10
    tableau(i, 2) = tableau(i, 2) & "       "
    Case Is = 11
    tableau(i, 2) = tableau(i, 2) & "      "
    Case Is = 12
    tableau(i, 2) = tableau(i, 2) & "     "
    Case Is = 13
    tableau(i, 2) = tableau(i, 2) & "    "
    Case Is = 14
    tableau(i, 2) = tableau(i, 2) & "   "
    Case Is = 15
    tableau(i, 2) = tableau(i, 2) & "  "
    Case Is = 16
    tableau(i, 2) = tableau(i, 2) & " "
    Case Is = 17
    tableau(i, 2) = tableau(i, 2) & ""
    End Select
    je vous met en exemple le debut du script
    sachant que je fais un select case pour quasi toute les lignes du tableau d'où la longueur du script
    peut etre que je peux faire le select case dans un autre module et l'appeler dans le module principal ?

    je voulais dire toutes le colonnes du tableau

  5. #5
    Expert confirmé

    Homme Profil pro
    Curieux
    Inscrit en
    Juillet 2012
    Messages
    5 169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Curieux
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2012
    Messages : 5 169
    Billets dans le blog
    5
    Par défaut
    à main levée, c'est juste pour te montrer le principe d'une factorisation

    en gros, à part les deux premières colonnes de ton tableau, toutes les autres ne font que prendre les colonnes les unes après les autres, dans l'ordre
    donc on fait une boucle pour passer de 165 lignes de code à 1 seule

    et le traitement de ton Select Case : tous les Case génèrent la même action
    et toutes les conditions se résument à une seule : la longueur doit être inférieure à 18

    pour finir, ton Select Case traite de la troisième colonne de chaque ligne du tableau, plutôt que de reboucler sur ton tableau, fait ta seconde action à la fin de la première (quand tu as fini de remplir une ligne du tableau)

    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
    Sub ooo()
    Dim i As Integer
        With Sheets("toto1")
            nbligne = .Range("A1").End(xlDown).Row
            ReDim tableau(nbligne - 2, 165)
     
            For i = 0 To nbligne - 2
                tableau(i, 0) = "***"
                tableau(i, 1) = "CAE"
     
                For j = LBound(tableau, 2) + 2 To UBound(tableau, 2)
                    tableau(i, j) = .Cells(i + 2, j - 1) ' si Option Base 1 alors Cells(i + 2, j - 2)
                Next j
     
                If Len(tableau(i, 2)) < 18 Then tableau(i, 2) = tableau(i, 2) & " "
            Next i
        End With
    End Sub
    EDIT : si on avait la finalité de tes premières colonnes ("***" et "CAE") on pourrait également juger s'il est opportun de les inclure dans la variable tableau
    car si pas besoin de les inclure, on a même plus besoin de boucle dans ton programme ... on fait tout en 5 lignes de code

  6. #6
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    119
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 119
    Par défaut
    je n'ai pas été assez précis

    la longueur doit être inférieure à 18 => malheureusement non elle est très variables elle va de 1 a 70 en fonction de mes colonnes

  7. #7
    Expert confirmé

    Homme Profil pro
    Curieux
    Inscrit en
    Juillet 2012
    Messages
    5 169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Curieux
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2012
    Messages : 5 169
    Billets dans le blog
    5
    Par défaut
    le traitement réalisé reste-t-il le même ou non

    si c'est le même, il suffit de changer 18 par 71
    sinon, tu découpes en autant de Case que de traitements différents

    MAIS, il ne faut surtout pas créer un Case par cas possible ... on regroupe tous les cas ayant un traitement identique dans un seul bloc Case

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Select Case Len(...)
     
    Case is < 18
        ' Un Traitement
     
    Case is < 30
        ' Un Traitement
     
    End Select
    comme je l'ai souligné, c'est une illustration de ce que tu dois faire que j'ai montré, pas un code à utiliser tel quel ... on n'a juste vu un aperçu de ton code

    si tu as compris là où je veux t'emmener : avant de découper en sous-procédure, on s'assure déjà d'avoir optimisé son code
    alors la finalité de mon intervention est une réussite

  8. #8
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    119
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 119
    Par défaut
    mon script n'a pas été correctement copier ...

    je padde avec des espace a gauche en fonction de la longueur

    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
    Select Case Len(tableau(i, 2))
    Case Is = 0
    tableau(i, 2) = tableau(i, 2) & "                 "
    Case Is = 1
    tableau(i, 2) = tableau(i, 2) & "                "
    Case Is = 2
    tableau(i, 2) = tableau(i, 2) & "               "
    Case Is = 3
    tableau(i, 2) = tableau(i, 2) & "              "
    Case Is = 4
    tableau(i, 2) = tableau(i, 2) & "             "
    Case Is = 5
    tableau(i, 2) = tableau(i, 2) & "            "
    Case Is = 6
    tableau(i, 2) = tableau(i, 2) & "           "
    Case Is = 7
    tableau(i, 2) = tableau(i, 2) & "          "
    Case Is = 8
    tableau(i, 2) = tableau(i, 2) & "         "
    Case Is = 9
    tableau(i, 2) = tableau(i, 2) & "        "
    Case Is = 10
    tableau(i, 2) = tableau(i, 2) & "       "
    Case Is = 11
    tableau(i, 2) = tableau(i, 2) & "      "
    Case Is = 12
    tableau(i, 2) = tableau(i, 2) & "     "
    Case Is = 13
    tableau(i, 2) = tableau(i, 2) & "    "
    Case Is = 14
    tableau(i, 2) = tableau(i, 2) & "   "
    Case Is = 15
    tableau(i, 2) = tableau(i, 2) & "  "
    Case Is = 16
    tableau(i, 2) = tableau(i, 2) & " "
    Case Is = 17
    tableau(i, 2) = tableau(i, 2) & ""
    End Select

  9. #9
    Expert confirmé

    Homme Profil pro
    Curieux
    Inscrit en
    Juillet 2012
    Messages
    5 169
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Curieux
    Secteur : Arts - Culture

    Informations forums :
    Inscription : Juillet 2012
    Messages : 5 169
    Billets dans le blog
    5
    Par défaut
    je ne comprend pas

    tu ajoutes un unique espace dans ton select case là ? Ou tu expliques qu'il faut ajouter autant d'espace que la longueur initiale de la chaine ?

  10. #10
    Membre confirmé
    Inscrit en
    Août 2006
    Messages
    119
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 119
    Par défaut
    new 1.vb

    je joins le 1er module qui appelle ensuite le 2eme

Discussions similaires

  1. [FLASH MX2004] [AS2] Valeur d'indice de boucle dans evenement
    Par Demco dans le forum ActionScript 1 & ActionScript 2
    Réponses: 1
    Dernier message: 02/05/2006, 10h43
  2. [Tableaux] Boucle dans un tableau
    Par apprenti-fab dans le forum Langage
    Réponses: 9
    Dernier message: 24/04/2006, 10h14
  3. [Tableaux] Boucle dans une boucle
    Par spawns dans le forum Langage
    Réponses: 2
    Dernier message: 12/02/2006, 13h11
  4. Creer une boucle dans une requête ???
    Par fdloisel dans le forum MS SQL Server
    Réponses: 4
    Dernier message: 26/10/2004, 14h05
  5. Notion de boucles dans Business Object
    Par lionelEIGIP dans le forum Deski
    Réponses: 1
    Dernier message: 08/04/2004, 11h26

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