我从开始时就不断获得“ Next for for”

时间:2019-02-26 08:26:42

标签: excel vba

我有一个运行单元格更改的宏,但是每次我要运行它时,它都会不断给我“ Next for”或“给定范围内的双重声明” 这是代码:

Dim Lvl As Range
Set Lvl = Range("A5:A44")
Dim Full, Medium, Poor
Dim r As Long
For r = 5 To 44
Full = StrComp(Range("A" & r), Data.Range("A2"), 0)
Medium = StrComp(Range("A" & r), Data.Range("A3"), 0)
Poor = StrComp(Range("A" & r), Data.Range("A4"), 0)
If Not Intersect(Target, Lvl) Is Nothing Then
    Dim i As Integer
    For i = r To 44
        If Full = 0 Then
            Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * 1), 0)
        ElseIf Medium = 0 Then
            Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (3 / 4)), 0)
        ElseIf Poor = 0 Then
            Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (1 / 2)), 0)
        Else
            Range("I" & r).Value = 0
        End If


        If Not Range("A" & r) = Range("A" & r).Offset(-1, 0) And Not Range("A" & r) = A5 Then
            If Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Medium Then
                Range("I" & i).Value = Range("I" & i).Value + 1
            ElseIf Range("A" & r).Offset(-1, 0) = Medium And Range("A" & r) = Poor Then
                Range("I" & i).Value = Range("I" & i).Value + 1
            ElseIf Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Poor Then
                Range("I" & i).Value = Range("I" & i).Value + 2
            End If
        End If
    Next
End If
Next

我遇到的双重声明错误可能与我使用r = 5至44 AND i = r至44的事实有关 但是我需要一种使Range("I" & i).value工作的方法

1 个答案:

答案 0 :(得分:2)

End If之前有Next丢失

Dim Lvl As Range
Set Lvl = Range("A5:A44")
Dim Full, Medium, Poor
r = 5
Full = StrComp(Range("A" & r), Data.Range("A2"), 0)
Medium = StrComp(Range("A" & r), Data.Range("A3"), 0)
Poor = StrComp(Range("A" & r), Data.Range("A4"), 0)

If Not Intersect(Target, Lvl) Is Nothing Then
    Dim i As Integer
    For i = r To 44
        If Full = 0 Then
            Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * 1), 0)
        ElseIf Medium = 0 Then
            Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (3 / 4)), 0)
        ElseIf Poor = 0 Then
            Range("I" & r).Value = Application.WorksheetFunction.RoundDown((Range("B" & r) * (1 / 2)), 0)
        Else
            Range("I" & r).Value = 0
        End If


        If Not Range("A" & r) = Range("A" & r).Offset(-1, 0) And Not Range("A" & r) = A5 Then 
        '^^-- this one is not closed by End If

            If Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Medium Then
            '^^-- this is AAA
                Range("I" & i).Value = Range("I" & i).Value + 1
            ElseIf Range("A" & r).Offset(-1, 0) = Medium And Range("A" & r) = Poor Then
                Range("I" & i).Value = Range("I" & i).Value + 1
            ElseIf Range("A" & r).Offset(-1, 0) = Full And Range("A" & r) = Poor Then
                Range("I" & i).Value = Range("I" & i).Value + 2
            End If '<-- this closes the one I marked with AAA

        End If  '<-- missing here !!!

    Next
End If