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 :

Simplication Macro avec "Case"


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Avril 2009
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 22
    Par défaut Simplication Macro avec "Case"
    Bonjour,

    je souhaiterais simplifier la macro ci dessous afin de gagner en temps de calcul. Je vous ai mis le fichier en pièce jointe.

    j'ai un calcul à faire en fonction du type de ces éléments ci dessous:

    ROUTEUR
    ANNEAU
    BRASSEUR
    BAS
    ASBC
    MGW
    RTC
    VOD

    Je fais des boucles "if" à chaque fois et cela alourdit le temps. Y aurais-t-il un moyen pour simplifier cette macro ?

    Merci d'avance pour votre aide



    -------------------------------------------------------------------------


    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
    Sub suivi_charge_perspective()
     
    Dim Lg As Long
    Dim LgDer As Long
    Dim ClDer As Long
    Dim I As Long
    Dim J As Long
     
     
    Sheets("Suivi_charge_ingenieristes").Select
     
      LgDer = Range("A65536").End(xlUp).Row
      fin = Range("A" & Cells.Rows.Count).End(xlUp).Row
      ClDer = Range("IV1").End(xlToLeft).Column
     
        Range("BD4:CR600").Select
        Selection.ClearContents
     
      Date_MAD_souhaite = 12
      Operation = 54
     
      For I = 4 To fin
        For J = 56 To 96
     
            If Cells(I, Operation) = "ROUTEUR" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 3
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
    '.........................
        End If
     
            If Cells(I, Operation) = "ANNEAU" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 5
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 0.5
     
     
        End If
    '.........................
     
        End If
     
     
            If Cells(I, Operation) = "WDM" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 5
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
    '.........................
     
        End If
     
     
            If Cells(I, Operation) = "BRASSEUR" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 4
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
     '.........................
        End If
     
     
            If Cells(I, Operation) = "BAS" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 4
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
    '.........................
        End If
     
     
            If Cells(I, Operation) = "ASBC" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 4
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
    '.........................
        End If
     
     
            If Cells(I, Operation) = "RTC" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 2
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
     
    '.........................
        End If
     
     
            If Cells(I, Operation) = "VOD" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 3
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
     
    '.........................
        End If
     
     
            If Cells(I, Operation) = "MGW" Then
     
            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
             Cells(I, J) = 1
            toto = J - 2
            If toto < 56 Then toto = 56
            Range(Cells(I, 56), Cells(I, toto)) = ""
            Range(Cells(I, toto), Cells(I, J)) = 1
     
     
        End If
     
        End If
     
     
     
        Next J
      Next I
     
      MsgBox ("Calcul Terminé")
     
     
    End Sub
    Fichiers attachés Fichiers attachés

  2. #2
    Membre chevronné Avatar de Bear the french
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    352
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Saône et Loire (Bourgogne)

    Informations forums :
    Inscription : Mai 2012
    Messages : 352
    Par défaut
    Bonjour choudoudou15,

    Une refonte comme celle là (?) :

    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
            Select Case Cells(I, Operation)
            Case "ROUTEUR"
               If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
                    Cells(I, J) = 1
                    toto = J - 3
                    If toto < 56 Then toto = 56
                        Range(Cells(I, 56), Cells(I, toto)) = ""
                        Range(Cells(I, toto), Cells(I, J)) = 1
                    End If
               End If
     
     
            Case "ANNEAU"
     
    ...
     
     
            End Select
    Bertrand

  3. #3
    Membre averti
    Inscrit en
    Avril 2009
    Messages
    22
    Détails du profil
    Informations forums :
    Inscription : Avril 2009
    Messages : 22
    Par défaut
    Super Merci, c'est exactement ce que je recherchais

  4. #4
    Membre Expert
    Homme Profil pro
    Retraité
    Inscrit en
    Avril 2011
    Messages
    1 858
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Avril 2011
    Messages : 1 858
    Par défaut
    Bonjour,

    Sinon, tu aurais pu faire quelque chose comme ça
    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
    Sub suivi_charge_perspective()
    Dim Lg As Long, LgDer As Long, ClDer As Long, I As Long, J As Long, k As Long
    Dim ListeNom, ListeToto, ListeS
    Dim Nom As String
     
        Date_MAD_souhaite = 12
        Operation = 54
        ListeNom = Array("ROUTEUR", "ANNEAU", "WDM", "BRASSEUR", "BAS", "ASBC", "RTC", "VOD", "MGW")
        ListeToto = Array(3, 5, 5, 4, 4, 4, 2, 3, 2)
        ListeS = Array(1, 0.5, 1, 1, 1, 1, 1, 1, 1)
        Application.ScreenUpdating = False
     
        With Worksheets("Suivi_charge_ingenieristes")
            LgDer = .Range("A65536").End(xlUp).Row
            fin = .Range("A" & .Cells.Rows.Count).End(xlUp).Row
            ClDer = .Range("IV1").End(xlToLeft).Column
            .Range("BD4:CR600").ClearContents
            For I = 4 To fin
                For J = 56 To 96
                    For k = 0 To UBound(ListeNom)
                        If Cells(I, Operation) = ListeNom(k) Then
                            If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
                                Cells(I, J) = 1
                                toto = J - ListeToto(k)
                                If toto < 56 Then toto = 56
                                Range(Cells(I, 56), Cells(I, toto)) = ""
                                Range(Cells(I, toto), Cells(I, J)) = ListeS(k)
                            End If
                        End If
                    Next k
                Next J
            Next I
        End With
        Application.ScreenUpdating = True
        MsgBox ("Calcul Terminé")
    End Sub
    Cordialement.

  5. #5
    Rédacteur
    Avatar de Philippe Tulliez
    Homme Profil pro
    Formateur, développeur et consultant Excel, Access, Word et VBA
    Inscrit en
    Janvier 2010
    Messages
    13 186
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Formateur, développeur et consultant Excel, Access, Word et VBA

    Informations forums :
    Inscription : Janvier 2010
    Messages : 13 186
    Billets dans le blog
    53
    Par défaut
    Bonjour,
    Ou comme ceci
    Juste le code de la boucle.
    Ajouter la variable avcol
    Et comme pour tous les cas il faut donner la valeur 1 excepté "ANNEAU" où il faut 0.5
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Range(Cells(I, toto), Cells(I, J)) = 0.5 + (0.5 * Abs(Cells(I, Operation) <> "ANNEAU"))
    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
     Dim avcol As Byte ' Variable
     For I = 4 To fin
       For J = 56 To 96
        Select Case Cells(I, Operation)
         Case "ROUTEUR", "VOD"
          avcol = 3
         Case "ANNEAU", "WDM"
          avcol = 5
         Case "BRASSEUR", "BAS", "ASBC"
          avcol = 4
         Case "RTC", "WGM"
          avcol = 2
        End Select
        If CDate(Cells(I, Date_MAD_souhaite)) >= CDate(Cells(1, J)) And CDate(Cells(I, Date_MAD_souhaite)) <= CDate(Cells(2, J)) Then
          Cells(I, J) = 1: toto = J - avcol
          If toto < 56 Then toto = 56
          Range(Cells(I, 56), Cells(I, toto)) = ""
          Range(Cells(I, toto), Cells(I, J)) = 0.5 + (0.5 * Abs(Cells(I, Operation) <> "ANNEAU"))
        End If
        Next J
      Next I
    Philippe Tulliez
    Ce que l'on conçoit bien s'énonce clairement, et les mots pour le dire arrivent aisément. (Nicolas Boileau)
    Lorsque vous avez la réponse à votre question, n'oubliez pas de cliquer sur et si celle-ci est pertinente pensez à voter
    Mes tutoriels : Utilisation de l'assistant « Insertion de fonction », Les filtres avancés ou élaborés dans Excel
    Mon dernier billet : Utilisation de la fonction Dir en VBA pour vérifier l'existence d'un fichier

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

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