Partage
  • Partager sur Facebook
  • Partager sur Twitter

Simplifier une macro avec des lignes répétitives

Sujet résolu
    14 novembre 2023 à 21:40:23

    Bonjour, j'ai écrit cette macro, et j'ai tenté de la simplifier avec des boucles, mais ça ne fonctionne pas. Je ne suis pas un as du VBA, mais je trouve ce langage très intéressant.

    Voici la macro brut qui fonctionne :

    Dim Aujourdhuii As Date
    Dim Datevaleur As Date
    Dim Livret_A As Currency
    
    Aujourdhuii = Range("B1").Value
    Datevaleur = Range("A4").Value
    Livret_A = Range("F1").Value
    
        If Aujourdhuii >= Range("A4").Value And Aujourdhuii < Range("A5").Value Then
    
            Range("B4").Value = Livret_A
            Datevaleur = Range("A4").Value
    
        ElseIf Aujourdhuii >= Range("A5").Value And Aujourdhuii < Range("A6").Value Then
    
            Range("B5").Value = Livret_A
            Datevaleur = Range("A5").Value
    
        ElseIf Aujourdhuii >= Range("A6").Value And Aujourdhuii < Range("A7").Value Then
    
            Range("B6").Value = Livret_A
            Datevaleur = Range("A6").Value
    
        ElseIf Aujourdhuii >= Range("A7").Value And Aujourdhuii < Range("A8").Value Then
    
            Range("B7").Value = Livret_A
            Datevaleur = Range("A7").Value
    
        ElseIf Aujourdhuii >= Range("A8").Value And Aujourdhuii < Range("A9").Value Then
    
            Range("B8").Value = Livret_A
            Datevaleur = Range("A8").Value
    
        ElseIf Aujourdhuii >= Range("A9").Value And Aujourdhuii < Range("A10").Value Then
    
            Range("B9").Value = Livret_A
            Datevaleur = Range("A9").Value
    
        ElseIf Aujourdhuii >= Range("A10").Value And Aujourdhuii < Range("A11").Value Then
    
            Range("B10").Value = Livret_A
            Datevaleur = Range("A10").Value
    
        ElseIf Aujourdhuii >= Range("A11").Value And Aujourdhuii < Range("A12").Value Then
    
            Range("B11").Value = Livret_A
            Datevaleur = Range("A11").Value
    
        ElseIf Aujourdhuii >= Range("A12").Value And Aujourdhuii < Range("A13").Value Then
    
            Range("B12").Value = Livret_A
            Datevaleur = Range("A12").Value
    
        ElseIf Aujourdhuii >= Range("A13").Value And Aujourdhuii < Range("A14").Value Then
    
            Range("B13").Value = Livret_A
            Datevaleur = Range("A13").Value
    
        ElseIf Aujourdhuii >= Range("A14").Value And Aujourdhuii < Range("A15").Value Then
    
            Range("B14").Value = Livret_A
            Datevaleur = Range("A14").Value
    
        ElseIf Aujourdhuii >= Range("A15").Value And Aujourdhuii < Range("A16").Value Then
    
            Range("B15").Value = Livret_A
            Datevaleur = Range("A15").Value
    
        ElseIf Aujourdhuii >= Range("A16").Value And Aujourdhuii < Range("A17").Value Then
    
            Range("B16").Value = Livret_A
            Datevaleur = Range("A16").Value
    
        ElseIf Aujourdhuii >= Range("A17").Value And Aujourdhuii < Range("A18").Value Then
    
            Range("B17").Value = Livret_A
            Datevaleur = Range("A17").Value
    
        ElseIf Aujourdhuii >= Range("A18").Value And Aujourdhuii < Range("A19").Value Then
    
            Range("B18").Value = Livret_A
            Datevaleur = Range("A18").Value
    
        ElseIf Aujourdhuii >= Range("A19").Value And Aujourdhuii < Range("A20").Value Then
    
            Range("B19").Value = Livret_A
            Datevaleur = Range("A19").Value
    
        ElseIf Aujourdhuii >= Range("A20").Value And Aujourdhuii < Range("A21").Value Then
    
            Range("B20").Value = Livret_A
            Datevaleur = Range("A20").Value
    
        ElseIf Aujourdhuii >= Range("A21").Value And Aujourdhuii < Range("A22").Value Then
    
            Range("B21").Value = Livret_A
            Datevaleur = Range("A21").Value
    
        ElseIf Aujourdhuii >= Range("A22").Value And Aujourdhuii < Range("A23").Value Then
    
            Range("B22").Value = Livret_A
            Datevaleur = Range("A22").Value
    
        ElseIf Aujourdhuii >= Range("A23").Value And Aujourdhuii < Range("A24").Value Then
    
            Range("B23").Value = Livret_A
            Datevaleur = Range("A23").Value
    
        ElseIf Aujourdhuii >= Range("A24").Value And Aujourdhuii < Range("A25").Value Then
    
            Range("B24").Value = Livret_A
            Datevaleur = Range("A24").Value
    
        ElseIf Aujourdhuii >= Range("A25").Value And Aujourdhuii < Range("A26").Value Then
    
            Range("B25").Value = Livret_A
            Datevaleur = Range("A25").Value
    
        ElseIf Aujourdhuii >= Range("A26").Value And Aujourdhuii < Range("A27").Value Then
    
            Range("B26").Value = Livret_A
            Datevaleur = Range("A26").Value
    
        ElseIf Aujourdhuii >= Range("A27").Value And Aujourdhuii < Range("A28").Value Then
        
            Range("27").Value = Livret_A
            Datevaleur = Range("A27").Value
    
        End If
    



    Pourriez- vous m'éclairer ?

    Merci d'avance.

    • Partager sur Facebook
    • Partager sur Twitter
      15 novembre 2023 à 2:45:02

      Bonjour,

      Déplacement vers un forum plus approprié

      Le sujet est déplacé de la section Langages.NET vers la section Autres langages (VBA, Ruby,...)

      • Partager sur Facebook
      • Partager sur Twitter
        15 novembre 2023 à 13:23:19

        un truc du style

        for i = 4 to 27
            If Aujourdhuii >= Range("A" & i).Value And Aujourdhuii < Range("A" & (i+1)).Value Then
         
                Range("B" & i).Value = Livret_A
                Datevaleur = Range("A" & i).Value
                Exit For
            End If
        Next i




        • Partager sur Facebook
        • Partager sur Twitter
          15 novembre 2023 à 22:39:27

          Merci beaucoup, ça fonctionne parfaitement. sujet résolu.
          • Partager sur Facebook
          • Partager sur Twitter

          Simplifier une macro avec des lignes répétitives

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown