For Loop未正确执行

时间:2015-01-22 04:31:04

标签: excel vba excel-vba

我有以下VBA代码:

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

似乎没有执行For nyear = 0 to nyear = 35循环。它适用于nyear = 0但由于某种原因转到nyear =1然后结束子例程。关于它为什么不起作用的任何想法?

1 个答案:

答案 0 :(得分:4)

您必须对代码进行少量更改:

For nyear = 0 To nyear = 35

取代此行,请使用此:

For nyear = 0 To 35