dubbbdan
dubbbdan

Reputation: 2740

For Loop not executing properly

I have the following VBA code:

Sub Find_Max_Yearly_Precip()
    Dim Year_Rng As Range
    Dim rng As Range
    Dim nyear As Integer
    Dim nL As Integer
    Dim nU As Integer

    For nyear = 0 To nyear = 35
        'See if its the first loop
        If nyear <> 0 Then
            'Decide if its a leap year
            If nyear = 1 Or nyear = 5 Or nyear = 9 Or nyear = 13 _
            Or nyear = 17 Or nyear = 21 Or nyear = 25 Or nyear = 29 _
            Or nyear = 33 Then
                nL = nU + 1
                nU = nL + 366
                Set Year_Rng = Range(Cells(nL, 1), Cells(nU, 1))
                Year_Rng.Select
            Else
                nL = nU + 1
                nU = nL + 365
                Set Year_Rng = Range(Cells(nL, 1), Cells(nU, 1))
                Year_Rng.Select
            End If
        Else
            nL = 2
            nU = 366
            Set Year_Rng = Range(Cells(nL, 1), Cells(nU, 1))
            Year_Rng.Select
        End If

        For Each rng In Year_Rng
            If rng.Offset(, 2) = Application.WorksheetFunction _
            .Max(Year_Rng.Offset(, 2)) Then
                rng.Offset(, 3).Value = "Max"
            End If
        Next rng
    Next
End Sub

And it seems to be not executing the For nyear = 0 to nyear = 35 loop. It properly works for nyear = 0 but for some reason to goes to nyear =1 and then ends the sub routine. Any ideas on why its not working?

Upvotes: 0

Views: 67

Answers (1)

Paresh J
Paresh J

Reputation: 2419

You have to do small change in your code:

For nyear = 0 To nyear = 35

Instead of this line, Use this:

For nyear = 0 To 35

Upvotes: 4

Related Questions