XL 2016 Problème Exit For

Maxime26

XLDnaute Nouveau
Bonjour a tous ,
j'ai une problématique sur mon bout de code , problématique que je n'arrive pas a résoudre d'où cette appel a l'aide :)

Je ne comprend pas pourquoi lorsque je mets une condition avec Exit For celle ci n'est pas prise en compte .

Le but est de parcourir la colonne 14 est d'arrêter la boucle si il rencontre une ligne vide cela permettra notamment de ne pas parcourir les ligne de 10 a 300 inutilement

Merci

VB:
Private Sub Worksheet_Change(ByVal Target As Range)


        Dim i As Integer
         Dim j As Integer


 
 
    If (Range("P1")) = "" Then


  
    If Not Intersect(Target, Range("N10:Z200")) Is Nothing Then
  
  
 Range("A10:L240").Interior.ColorIndex = 0

 Range("E10:I240") = ""




For i = 10 To 300
For j = 1 To 150


If Cells(i, 14) = "" Then

Exit For

End If

 If Cells(i, 24) = ("AA") Then
      
     Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(188, 121, 255)
  
       ElseIf Cells(i, 24) = ("BB") Then
  
     Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(248, 203, 173)
  
      ElseIf Cells(i, 12) = ("CC") Then
 
 Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(255, 45, 0)
End If
 


  

 
  If Cells(i, 20) <> Worksheets(2).Cells(j, 2) And Cells(i, 20) <> "" Then
  
     Cells(i, 5) = 7

   Cells(i, 7) = 21
  
    Cells(i, 9) = 21
  

  
End If
 


Next j
Next i

End If

Else
End If

End Sub
 
C

Compte Supprimé 979

Guest
Bonjour Maxime26

Vous indenteriez votre code correctement, ce serait plus lisible et facile à voir ou se trouve le problème 🤔
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Integer
  Dim j As Integer
  If (Range("P1")) = "" Then
    If Not Intersect(Target, Range("N10:Z200")) Is Nothing Then
      Range("A10:L240").Interior.ColorIndex = 0
      Range("E10:I240") = ""
      For i = 10 To 300
        For j = 1 To 150
          If Cells(i, 14) = "" Then Exit For  ' Sortie de la boucle J
          If Cells(i, 24) = ("AA") Then
            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(188, 121, 255)
          ElseIf Cells(i, 24) = ("BB") Then
            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(248, 203, 173)
          ElseIf Cells(i, 12) = ("CC") Then
            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(255, 45, 0)
          End If
          If Cells(i, 20) <> Worksheets(2).Cells(j, 2) And Cells(i, 20) <> "" Then
            Cells(i, 5) = 7
            Cells(i, 7) = 21
            Cells(i, 9) = 21
          End If
        Next j
      Next i
    End If
  End If
End Sub

Dans ce cas avec 2 boucles, c'est "Exit Sub

A+

Edit : bonjour @dysorthographie
 

xUpsilon

XLDnaute Accro
Bonjour,

Par pitié, faites attention à l'indentation, pour la lisibilité ;)
Sinon, le Exit For te sort du For dans lequel tu es, sauf que tu as deux For imbriqués l'un dans l'autre, donc il sort de l'un pour retourner dans l'autre.
Ton code devrait ressembler à ça :
VB:
For i = 10 To 300
    If Cells(i, 14) = "" Then
        Exit For
    For j = 1 To 150

Ou sinon si tu veux arrêter la macro tu peux utiliser Exit Sub, qui mettra fin à l'exécution de toute la macro plutôt que juste sortir de la boucle.

Bonne journée,

Edit : Bonjour @dysorthographie , @BrunoM45 :)
 

patricktoulon

XLDnaute Barbatruc
bonjour
mais bon sang de bon soir
pourquoi mettre un exit for sur une condition
pourquoi ne pas (faire ou pas faire)avec cette simple condition
VB:
Private Sub Worksheet_Change(ByVal Target As Range)


    Dim i As Integer
    Dim j As Integer
    Dim sortir_de_J As Boolean



    If (Range("P1")) = "" Then



        If Not Intersect(Target, Range("N10:Z200")) Is Nothing Then


            Range("A10:L240").Interior.ColorIndex = 0

            Range("E10:I240") = ""




            For i = 10 To 300
                For j = 1 To 150


                    If Cells(i, 14) <> "" Then

                        If Cells(i, 24) = ("AA") Then

                            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(188, 121, 255)

                        ElseIf Cells(i, 24) = ("BB") Then

                            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(248, 203, 173)

                        ElseIf Cells(i, 12) = ("CC") Then

                            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(255, 45, 0)
                        End If
                        If Cells(i, 20) <> Worksheets(2).Cells(j, 2) And Cells(i, 20) <> "" Then

                            Cells(i, 5) = 7

                            Cells(i, 7) = 21

                            Cells(i, 9) = 21
                        End If
                    End If
                Next j
            Next i
        End If
    Else 'et ce else aussi il sert a quoi  ?????
    End If
End Sub
 
C

Compte Supprimé 979

Guest
Salut Patrick

Parce que... on n'aime ou pas 😜 🤣
bonjour
mais bon sang de bon soir
pourquoi mettre un exit for sur une condition
pourquoi ne pas (faire ou pas faire)avec cette simple condition
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    Dim j As Integer
    Dim sortir_de_J As Boolean
    If (Range("P1")) = "" Then
        If Not Intersect(Target, Range("N10:Z200")) Is Nothing Then
            Range("A10:L240").Interior.ColorIndex = 0
            Range("E10:I240") = ""
            For i = 10 To 300
                For j = 1 To 150
                    If Cells(i, 14) <> "" Then
                        If Cells(i, 24) = ("AA") Then
                            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(188, 121, 255)
                        ElseIf Cells(i, 24) = ("BB") Then
                            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(248, 203, 173)
                        ElseIf Cells(i, 12) = ("CC") Then
                            Range(Cells(i, 1), Cells(i, 12)).Interior.Color = RGB(255, 45, 0)
                        End If
                        If Cells(i, 20) <> Worksheets(2).Cells(j, 2) And Cells(i, 20) <> "" Then
                            Cells(i, 5) = 7
                            Cells(i, 7) = 21
                            Cells(i, 9) = 21
                        End If
                    End If
                Next j
            Next i
        End If
    Else 'et ce else aussi il sert a quoi  ?????
    End If
End Sub
Chacun sa façon de développer 😁
 

Statistiques des forums

Discussions
311 720
Messages
2 081 926
Membres
101 842
dernier inscrit
seb0390