Erreur sur boucle For

Florian53

XLDnaute Impliqué
Bonjour à tous,

J'ai réaliser un code avec 3 boucles "For - Next" :

VB:
Sub SommeFamilly()
Dim Ncol As Long
Dim tabBDD()
Dim wsBDD As Object
Dim wsResult As Object

Dim som(11)
Dim crit1, crit2, crit3
Dim cptBDD
Dim i As Integer



With Sheets("Familly")
Derlig = Cells(Rows.Count, 1).End(xlUp).Offset(0, 0).Row ' Dernier ligne du tableau
dercol = Cells(1, Cells.Columns.Count).End(xlToLeft).Offset(0, 0).Column ' Derniere colonne du tableau
End With

For i = 0 To i = (Derlig / 14) - 1 Step 1 'Nombre de petit tableau

    For Ncol = 3 To dercol ' Boucle jusqu'à la derniere colonne

        Set wsBDD = Worksheets("BDD")
        Set wsResult = Worksheets("Familly")
   
             With wsBDD ' Définiction du tableau dans la feuil(BDD)
             tabBDD = Range(.Cells(3, 1), .Cells(.Cells(Rows.Count, 1).End(xlUp).Row, 24))
             End With
   
                 With wsResult
                        som1 = 0
                        som2 = 0
                        som3 = 0
                        som4 = 0
                        som5 = 0
                        som6 = 0
                        som7 = 0
                        som8 = 0
                        som9 = 0
                        som10 = 0
                        som11 = 0
   
                        crit1 = .Cells(1, 2) 'Period
                        crit2 = .Cells(1 + (i * 14), Ncol) 'Familly
       

                            For cptBDD = 1 To UBound(tabBDD, 1)
     
       
                            If (tabBDD(cptBDD, 1) = crit1) And (tabBDD(cptBDD, 24) = crit2) Then
                            som1 = som1 + tabBDD(cptBDD, 11) 'A
                            som2 = som2 + tabBDD(cptBDD, 22) 'B
                            som3 = som3 + tabBDD(cptBDD, 12) 'M
                            som4 = som4 + tabBDD(cptBDD, 14) + tabBDD(cptBDD, 15) + tabBDD(cptBDD, 16) + tabBDD(cptBDD, 18) + tabBDD(cptBDD, 20) 'E
                            som5 = som5 + tabBDD(cptBDD, 19) ' F
                            som6 = som6 + tabBDD(cptBDD, 17) 'G
                            som7 = som7 + tabBDD(cptBDD, 14) 'H
                            som8 = som8 + tabBDD(cptBDD, 15) 'I
                            som9 = som9 + tabBDD(cptBDD, 16) ' J
                            som10 = som10 + tabBDD(cptBDD, 18) 'K
                            som11 = som11 + tabBDD(cptBDD, 20) ' L
               
                            End If
                            Next
       
                            .Cells((2 + i + (i * 14)), Ncol) = som1 'A
                            .Cells((3 + i + (i * 14)), Ncol) = (som2 * -1) 'B
                            If som3 = 0 Then
                            .Cells((4 + i + (i * 14)), Ncol) = 0
                            .Cells((8 + i + (i * 14)), Ncol) = 0
                            Else
                            .Cells((4 + i + (i * 14)), Ncol) = (.Cells((3 + i + (i * 14)), Ncol) / som3) ' C
                            .Cells((8 + i + (i * 14)), Ncol) = (.Cells((5 + i + (i * 14)), Ncol) / som3) 'G
                            End If
                            .Cells((5 + i + (i * 14)), Ncol) = (som4 * -1) 'D
                            .Cells((6 + i + (i * 14)), Ncol) = (som5 * -1) ' E
                            .Cells((7 + i + (i * 14)), Ncol) = (som6 * -1) 'F
                            .Cells((9 + i + (i * 14)), Ncol) = (som7 * -1) 'H
                            .Cells((10 + i + (i * 14)), Ncol) = (som8 * -1) 'I
                            .Cells((11 + i + (i * 14)), Ncol) = (som10 * -1) 'J
                            .Cells((12 + i + (i * 14)), Ncol) = (som9 * -1) ' K
                            .Cells((13 + i + (i * 14)), Ncol) = (som11 * -1) ' L
                            .Cells((14 + i + (i * 14)), Ncol) = (som3 * 1)   'M
       
                            End With
   
        Set wsBDD = Nothing
        Set wsResult = Nothing
   
    Next Ncol
   
Next i ' !!!!!!!!! Erreur : i reste à 0 puis passe à la ligne suivante alors que je souhaiterais qu'il boucle jusqu'à la valeur ("2") ==> (Derlig / 14) - 1 ==> (42/14)-1 !!!!!!!
   
    Cells.EntireColumn.AutoFit
   
    Application.ScreenUpdating = True
   
End Sub

A la fin de la 1er boucle (i), le code ne continue pas jusqu'à "i=2" mais s’arrête à "i=0", je ne comprends pas pourquoi le code passe à la ligne suivante alors que la valeur de "i" n'est pas atteinte.

S'agit il d'une mauvaise structuration du code ?
 

Pièces jointes

  • test3.xlsx
    17 KB · Affichages: 25