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 :

[E-03]simplifier un code par une boucle


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut [E-03]simplifier un code par une boucle
    bonjour à tous et toutes, petit nouveau dans le vba sous excel , j'ai crée une petite macro (grâce au forum) qui fonctionne très bien mais je désirerais la simplifier en y insérant une boucle afin d'éviter la répétiton de la fonction if...end if et je n'y arrive pas

    Ma question peut paraître un poil stupide mais je suis vraiment débutant et déjà très content quelle fonctionne déjà c'est un bon début mais je ne veux pas mourir idiot
    merci d'avance à ceux qui pourront éclairer ma lanterne

    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
    Sub cherche_negatif()
      Range("I14:I143").ClearContents
        For lig = 14 To 50
     
            If Cells(lig, 29).Value = "négatif" Then
               Cells(lig - 1, 9).Value = Cells(lig, 3).Value + Cells(lig - 1, 3).Value
        End If
     
         If Cells(lig - 1, 9).Value < 0 Then
            Cells(lig - 2, 9).Value = Cells(lig - 1, 9).Value + Cells(lig - 2, 3).Value
     
        End If
        If Cells(lig - 2, 9).Value < 0 Then
            Cells(lig - 3, 9).Value = Cells(lig - 2, 9).Value + Cells(lig - 3, 3).Value
     
        End If
     
     
        If Cells(lig - 3, 9).Value < 0 Then
            Cells(lig - 4, 9).Value = Cells(lig - 3, 9).Value + Cells(lig - 4, 3).Value
     
        End If
     
        If Cells(lig - 4, 9).Value < 0 Then
            Cells(lig - 5, 9).Value = Cells(lig - 4, 9).Value + Cells(lig - 5, 3).Value
     
        End If
     
        If Cells(lig - 5, 9).Value < 0 Then
            Cells(lig - 6, 9).Value = Cells(lig - 5, 9).Value + Cells(lig - 6, 3).Value
     
        End If
     
        If Cells(lig - 6, 9).Value < 0 Then
            Cells(lig - 7, 9).Value = Cells(lig - 6, 9).Value + Cells(lig - 7, 3).Value
     
        End If
     
        If Cells(lig - 7, 9).Value < 0 Then
            Cells(lig - 8, 9).Value = Cells(lig - 7, 9).Value + Cells(lig - 8, 3).Value
     
        End If
        If Cells(lig - 8, 9).Value < 0 Then
            Cells(lig - 9, 9).Value = Cells(lig - 8, 9).Value + Cells(lig - 9, 3).Value
     
        End If
        If Cells(lig - 9, 9).Value < 0 Then
            Cells(lig - 10, 9).Value = Cells(lig - 9, 9).Value + Cells(lig - 10, 3).Value
     
        End If
        If Cells(lig - 10, 9).Value < 0 Then
            Cells(lig - 11, 9).Value = Cells(lig - 10, 9).Value + Cells(lig - 11, 3).Value
     
        End If
        Next lig
        For x = 14 To 50
        If Cells(x, 9).Value < 0 Then
            Cells(x, 9).Value = 0
         End If
    Next x
     
     
    End Sub

  2. #2
    Membre émérite Avatar de Godzestla
    Homme Profil pro
    Chercheur de bonheur
    Inscrit en
    Août 2007
    Messages
    2 392
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chercheur de bonheur
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2007
    Messages : 2 392
    Points : 2 985
    Points
    2 985
    Par défaut
    Salut,
    et bienvenue sur ce forum.

    essaie ceci
    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
     
    Sub cherche_negatif()
      dim lig as integer
      dim ofs as integer
      Range("I14:I143").ClearContents
        For lig = 14 To 50
     
            If Cells(lig, 29).Value = "négatif" Then
               Cells(lig - 1, 9).Value = Cells(lig, 3).Value + Cells(lig - 1, 3).Value
        End If
     
       for ofs = 1 to 10
     
         If Cells(lig - ofs, 9).Value < 0 Then
            Cells(lig - (ofs + 1), 9).Value = Cells(lig - ofs, 9).Value + Cells(lig - (ofs+ 1), 3).Value
     
         next ofs
        Next lig
        For x = 14 To 50
        If Cells(x, 9).Value < 0 Then
            Cells(x, 9).Value = 0
         End If
    Next x
     
     
    End Sub

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Février 2009
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    merci pour la réponse aussi rapide cela fonctionne parfaitement j'ai juste ajouter end if avant les next et tout roule parfaitement merci de ton aide je plaçait mal le next de la deuxième variable

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

Discussions similaires

  1. génération de code avec une boucle
    Par fandeDB dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 18/11/2007, 11h26
  2. optimiser mon code avec une boucle for?
    Par Invité dans le forum ActionScript 3
    Réponses: 1
    Dernier message: 16/11/2007, 08h33
  3. initialisation d'une listview par une boucle
    Par sam_c_java dans le forum Windows Forms
    Réponses: 3
    Dernier message: 24/10/2007, 11h10
  4. [Conception] Faire une somme de valeurs définies par une boucle
    Par fixbraun dans le forum PHP & Base de données
    Réponses: 9
    Dernier message: 25/09/2006, 23h46
  5. [Timer] timer bloqué par une boucle for?
    Par Jidefix dans le forum Interfaces Graphiques en Java
    Réponses: 3
    Dernier message: 18/09/2006, 17h12

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