Problème double boucle + findnext

DebVBA

Nouveau membre
Bonjour,
J'ai isolé le bug de la macro et le problème semble venir du fait que je combine 2 boucle do-while et surtout deux findnext :/
Voici un apercu de mon code :

Code:
Dim i, j As Integer
Dim DesignProjet, DesignProjetGroupe, NomIng, Nom, Nom2, OccupT, OccupB As Range

Set total = Sheets("TabReference")
Set brouillon = Sheets("Brouillon")
Set Listes = Sheets("Listes")

For i = 5 To 30
    Set DesignProjetGroupe = total.Range("A" & i + 1)
    
    If Not DesignProjetGroupe.Value = 0 Then

' on cherche un nom de projet dans une première liste
         Set Nom = Listes.Range("L8:L50").Find(DesignProjetGroupe.Value, LookIn:=xlValues, Lookat:=xlPart)
 
 
'mais il peut y avoir plusieurs fois ce projet dans la liste donc j'utilise un findnext avec boucle
         If Not Nom Is Nothing Then
         firstAddress = Nom.Address
                     
             Do
                     
    'on a trouvé un nom de projet dans la liste,
            ' on selectionne la cellule associée (dans la colonne d'avant)
                       Set DesignProjet = Nom.Offset(0, -1)

'puis on cherche le contenu de cette cellule dans une autre liste d'une autre feuille 
' De même, il peut y avoir plusieurs résultat donc 2eme boucle avec findnext
                        
            Set Nom2 = brouillon.Range("A6:A300").Find(DesignProjet.Value, LookIn:=xlValues, Lookat:=xlPart)
                          
                          If Not Nom2 Is Nothing Then
                          firstAddress2 = Nom2.Address
                                                  
                              Do


'**DIVERS ACTIONS**


                                    Set Nom2 = brouillon.Range("A6:A300").FindNext(Nom2)
                            Loop While Not Nom2 Is Nothing And Nom2.Address <> firstAddress2
                                        
                         End If
                    
                Set Nom = Listes.Range("L8:L50").FindNext(Nom)
              Loop While Not Nom Is Nothing And Nom.Address <> firstAddress
                                
                    
         End If
    End If
Next
            
End Sub

Merci de votre aide! :hello:
 

DebVBA

Nouveau membre
Bon du coup j'ai contourné le problème avec un for each au lieu du 2eme findnext. Donc ça marche même si c'est moin joli je trouve :p
 

zeb

Modérateur
Code:
' // Oh, oh. Spa bon, ça. Faut pas confondre BASIC et PASCAL.
' // Là, tu déclares i comme Variant, et j comme Integer
Dim i, j As Integer

' // Même problème
Dim DesignProjet, DesignProjetGroupe, NomIng, Nom, Nom2, OccupT, OccupB As Range

' // Où as-tu déclaré ces variables ?
' // Sois pas chiche, sois précis. Utilise Worksheets plutôt que Sheet
Set total     = Sheets("TabReference" )
Set brouillon = Sheets("Brouillon" )
Set Listes    = Sheets("Listes" )

For i = 5 To 30
    ' //                     Gros calcul !
    ' //                     Utilise Cells(i+1, 1)
    Set DesignProjetGroupe = total.Range("A" & i + 1)

    ' // Not XXX = 0    ===    XXX <> 0
    ' // Ce n'est pas une erreur, on est d'accord ;)
    If Not DesignProjetGroupe.Value = 0 Then
        ' on cherche un nom de projet dans une première liste
        Set Nom = Listes.Range("L8:L50" ).Find(DesignProjetGroupe.Value, LookIn:=xlValues, Lookat:=xlPart)
        'mais il peut y avoir plusieurs fois ce projet dans la liste donc j'utilise un findnext avec boucle
        If Not Nom Is Nothing Then
            firstAddress = Nom.Address
            Do
                'on a trouvé un nom de projet dans la liste,
                ' on selectionne la cellule associée (dans la colonne d'avant)
                Set DesignProjet = Nom.Offset(0, -1)
                'puis on cherche le contenu de cette cellule dans une autre liste d'une autre feuille
                ' De même, il peut y avoir plusieurs résultat donc 2eme boucle avec findnext

                Set Nom2 = brouillon.Range("A6:A300" ).Find(DesignProjet.Value, LookIn:=xlValues, Lookat:=xlPart)
                If Not Nom2 Is Nothing Then
                    firstAddress2 = Nom2.Address
                    Do
                        '**DIVERS ACTIONS**	
                        Set Nom2 = brouillon.Range("A6:A300" ).FindNext(Nom2)
                    Loop While Not Nom2 Is Nothing And Nom2.Address <> firstAddress2
                End If
            Set Nom = Listes.Range("L8:L50" ).FindNext(Nom)
            Loop While Not Nom Is Nothing And Nom.Address <> firstAddress
        End If
    End If
Next
Bon, y'a quelques commentaires désagréables, mais c'est pratiquement rien.
Ajoute l'Option Explicit, ça te permettra de ne pas oublier de déclarer tes variables ;)

Y'a un moteur de recherche dans Excel. Mais il n'y en a qu'un :spamafote:
Regarde comment je ferais :
Code:
Dim ws_listes   As Worksheet
Dim ws_brouill  As Worksheet
Dim cel_listes  As Range
Dim cel_brouill As Range

Set ws_listes  = Worksheets("Listes")
Set ws_brouill = Worksheets("Brouillon")

For Each cel_listes In ws_listes.Range("L8:L50" ).Cells
    For Each cel_brouill In ws_brouill.Range("A6:A300" ).Cells
        If cel_listes.Value = cel_brouill.Value Then
            ' //ACTION
            
        End If
    Next
Next
 
Vous devez vous inscrire ou vous connecter pour répondre ici.
Derniers messages publiés
Statistiques globales
Discussions
730 143
Messages
6 718 284
Membres
1 586 411
Dernier membre
corblack
Partager cette page
Haut