Mattia
Mattia

Reputation: 1

Reset index cycle for-next

Good morning, I have a problem with the for-next loop. At the second iteration of the cycle, the exit from the cycle occurs even if the exit condition on variable j is not respected. I would have solved the problem with the goTo statement. However I am wondering why index reset does not work in this case. Thank you.

For j = LBound(AttivitaTemp) To UBound(AttivitaTemp)
    
     If confronta(AttivitaTemp(j), AttivitaFinali) = "#N/D" Then
     
        activityTemp = AttivitaTemp(j)
        rigaTemp = confronta(activityTemp, Attivita)
        SuccessioniTemp = estrai_riga(MatriceElenchiSuccessioni, rigaTemp)
        SuccessioniTemp = cancella_vuoti_vettore(SuccessioniTemp)
        ESsuccessioni = EF(rigaTemp)
        For k = LBound(SuccessioniTemp) To UBound(SuccessioniTemp)
            rigaTemp = confronta(SuccessioniTemp(k), Attivita)
            ES(rigaTemp) = Application.WorksheetFunction.Max(ES(rigaTemp), ESsuccessioni)
            EF(rigaTemp) = ES(rigaTemp) + Durate(rigaTemp)
        Next
        
        VettoreSuccessioniTemp = unisci_vettori(VettoreSuccessioniTemp, SuccessioniTemp)
        
        If j = UBound(AttivitaTemp) Then
            AttivitaTemp = VettoreSuccessioniTemp
            ReDim VettoreSuccessioniTemp(0)
            j = LBound(AttivitaTemp) - 1
            'GoTo ricomincia_ciclo_j
        End If
        
     End If
    
Next

Observing the variable j at the second iteration it results j = 0, with the next it goes to j = 1 but the for loop is not re-executed, although UBound (AttivitaTemp) is equal to 1.

In other words, why does this simple cycle work instead, which conceptually does the same thing?

For x = 0 To 2
    If x = 2 Then
        x = -1
    End If
Next

Upvotes: 0

Views: 103

Answers (1)

Mattia
Mattia

Reputation: 1

I solved it by myself: the problem is that even if it is possible to reset the iterator, it is not possible to update the conditions of the for loop (the values of "from" and "to" remain "frozen" even if during the loop they are changed).

Upvotes: 0

Related Questions