Se connecter avec
S'enregistrer | Connectez-vous

Problème double boucle + findnext

Dernière réponse : dans Programmation
Partagez

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 :

  1. Dim i, j As Integer
  2. Dim DesignProjet, DesignProjetGroupe, NomIng, Nom, Nom2, OccupT, OccupB As Range
  3.  
  4. Set total = Sheets("TabReference")
  5. Set brouillon = Sheets("Brouillon")
  6. Set Listes = Sheets("Listes")
  7.  
  8. For i = 5 To 30
  9. Set DesignProjetGroupe = total.Range("A" & i + 1)
  10.  
  11. If Not DesignProjetGroupe.Value = 0 Then
  12.  
  13. ' on cherche un nom de projet dans une première liste
  14. Set Nom = Listes.Range("L8:L50").Find(DesignProjetGroupe.Value, LookIn:=xlValues, Lookat:=xlPart)
  15.  
  16.  
  17. 'mais il peut y avoir plusieurs fois ce projet dans la liste donc j'utilise un findnext avec boucle
  18. If Not Nom Is Nothing Then
  19. firstAddress = Nom.Address
  20.  
  21. Do
  22.  
  23. 'on a trouvé un nom de projet dans la liste,
  24. ' on selectionne la cellule associée (dans la colonne d'avant)
  25. Set DesignProjet = Nom.Offset(0, -1)
  26.  
  27. 'puis on cherche le contenu de cette cellule dans une autre liste d'une autre feuille
  28. ' De même, il peut y avoir plusieurs résultat donc 2eme boucle avec findnext
  29.  
  30. Set Nom2 = brouillon.Range("A6:A300").Find(DesignProjet.Value, LookIn:=xlValues, Lookat:=xlPart)
  31.  
  32. If Not Nom2 Is Nothing Then
  33. firstAddress2 = Nom2.Address
  34.  
  35. Do
  36.  
  37.  
  38. '**DIVERS ACTIONS**
  39.  
  40.  
  41. Set Nom2 = brouillon.Range("A6:A300").FindNext(Nom2)
  42. Loop While Not Nom2 Is Nothing And Nom2.Address <> firstAddress2
  43.  
  44. End If
  45.  
  46. Set Nom = Listes.Range("L8:L50").FindNext(Nom)
  47. Loop While Not Nom Is Nothing And Nom.Address <> firstAddress
  48.  
  49.  
  50. End If
  51. End If
  52. Next
  53.  
  54. End Sub


Merci de votre aide! :hello: 

Autres pages sur : probleme double boucle findnext

  1. ' // Oh, oh. Spa bon, ça. Faut pas confondre BASIC et PASCAL.
  2. ' // Là, tu déclares i comme Variant, et j comme Integer
  3. Dim i, j As Integer
  4.  
  5. ' // Même problème
  6. Dim DesignProjet, DesignProjetGroupe, NomIng, Nom, Nom2, OccupT, OccupB As Range
  7.  
  8. ' // Où as-tu déclaré ces variables ?
  9. ' // Sois pas chiche, sois précis. Utilise Worksheets plutôt que Sheet
  10. Set total = Sheets("TabReference" )
  11. Set brouillon = Sheets("Brouillon" )
  12. Set Listes = Sheets("Listes" )
  13.  
  14. For i = 5 To 30
  15. ' // Gros calcul !
  16. ' // Utilise Cells(i+1, 1)
  17. Set DesignProjetGroupe = total.Range("A" & i + 1)
  18.  
  19. ' // Not XXX = 0 === XXX <> 0
  20. ' // Ce n'est pas une erreur, on est d'accord ;)
  21. If Not DesignProjetGroupe.Value = 0 Then
  22. ' on cherche un nom de projet dans une première liste
  23. Set Nom = Listes.Range("L8:L50" ).Find(DesignProjetGroupe.Value, LookIn:=xlValues, Lookat:=xlPart)
  24. 'mais il peut y avoir plusieurs fois ce projet dans la liste donc j'utilise un findnext avec boucle
  25. If Not Nom Is Nothing Then
  26. firstAddress = Nom.Address
  27. Do
  28. 'on a trouvé un nom de projet dans la liste,
  29. ' on selectionne la cellule associée (dans la colonne d'avant)
  30. Set DesignProjet = Nom.Offset(0, -1)
  31. 'puis on cherche le contenu de cette cellule dans une autre liste d'une autre feuille
  32. ' De même, il peut y avoir plusieurs résultat donc 2eme boucle avec findnext
  33.  
  34. Set Nom2 = brouillon.Range("A6:A300" ).Find(DesignProjet.Value, LookIn:=xlValues, Lookat:=xlPart)
  35. If Not Nom2 Is Nothing Then
  36. firstAddress2 = Nom2.Address
  37. Do
  38. '**DIVERS ACTIONS**
  39. Set Nom2 = brouillon.Range("A6:A300" ).FindNext(Nom2)
  40. Loop While Not Nom2 Is Nothing And Nom2.Address <> firstAddress2
  41. End If
  42. Set Nom = Listes.Range("L8:L50" ).FindNext(Nom)
  43. Loop While Not Nom Is Nothing And Nom.Address <> firstAddress
  44. End If
  45. End If
  46. 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 :
  1. Dim ws_listes As Worksheet
  2. Dim ws_brouill As Worksheet
  3. Dim cel_listes As Range
  4. Dim cel_brouill As Range
  5.  
  6. Set ws_listes = Worksheets("Listes")
  7. Set ws_brouill = Worksheets("Brouillon")
  8.  
  9. For Each cel_listes In ws_listes.Range("L8:L50" ).Cells
  10. For Each cel_brouill In ws_brouill.Range("A6:A300" ).Cells
  11. If cel_listes.Value = cel_brouill.Value Then
  12. ' //ACTION
  13.  
  14. End If
  15. Next
  16. Next
Posez votre question