循环内循环。如果满足条件,则停止第二次循环

时间:2015-12-12 22:59:42

标签: vba excel-vba loops for-loop excel

我在代码下面写了。它是循环内循环。第一个循环运行以在所有第二个循环的所有范围(sheet2)中搜索第一个范围的值(sheet1)。如果满足条件,我希望第二个循环停止并继续下一个值。

我正在使用"退出"如果满足条件,则停止第二次循环。但问题是......

当First loop = 2时,第二个循环也是2且条件满足,第二个循环停止。第一个循环变为= 3但是第二个循环仍然保持= 2,这导致所有的不匹配和失败。请帮忙。

    For I = 2 To REND        
         For P = 2 To ENDROW
          If Range("D" & I) = D.Range("c" & P) Then
            If Range("H" & I) = D.Range("F" & P) Then
                Range("A" & I) = "MATCHED"
                Exit For

            Else
                Range("A" & I) = "DIFFERENCE OF " & Range("G" & I) - D.Range("F" & P)
            End If
          Else
            Range("A" & I) = "AP NOT FOUND"
          End If
         Next P
Next I

声明在哪里:

Dim D As Worksheet, F As Worksheet, R As WorksheetDim X As String
Dim I As Integer, ENDROW As Integer, FEND As Integer
Dim P As Integer, REND As      Integer, L As Integer 
Set D = ActiveWorkbook.Sheets("DWAC")
Set F = ActiveWorkbook.Sheets("FPS")
Set R = ActiveWorkbook.Sheets("RAW")
ENDROW = D.Range("C2").End(xlDown).Row
FEND = F.Range("C2").End(xlDown).Row
REND = R.Range("C2").End(xlDown).Row

2 个答案:

答案 0 :(得分:2)

根据您的问题,我假设Sheets("DWAC") column C只有一个结果。考虑到这一点,我们可以使用find而不是循环,直到你得到一个匹配。

如果我们清理了你的Dim语句(代码中没有使用一张表,那么行变量应该是long

    Dim D As Worksheet, R As Worksheet
    Dim ENDROW As Long, REND As Long
    Dim rng1 As Range, rng2 As Range
    Dim c As Range, FndC As Range

现在当然要设置工作表

Set D = Sheets("DWAC")
Set R = Sheets("RAW")

然后找到最后一行并设置范围。

这将找到列c Sheets(“DWAC”)中的最后一行,设置范围是我们要从Sheets(“RAW”)中找到原始值的位置

 With D
    ENDROW = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set rng2 = .Range("C2:C" & ENDROW) 'find value in column C sheet D
End With

然后从图纸R设置循环范围,这是我们将循环的范围。

With R
        REND = .Cells(.Rows.Count, "D").End(xlUp).Row
        Set rng1 = .Range("D2:D" & REND) 'Loop through column D in sheet R
    End With

现在我们可以开始循环了。 c将循环通过rng1并在rng2中找到自己,如果找到c,FndC将是范围。

 For Each c In rng1.Cells
        Set FndC = rng2.Find(what:=c, lookat:=xlWhole)

如果找到c,那就做点什么。

   If Not FndC Is Nothing Then

            If c.Offset(, 4) = FndC.Offset(, 3) Then
                c.Offset(, -3) = "Match"
            Else
                c.Offset(, -3) = "DIFFERENCE OF " & c.Offset(, 3) - FndC.Offset(, 3)
            End If
        Else: c.Offset(, -3) = "Not Found"

        End If
    Next c

如果我的偏移顺序错误,您可以根据自己的要求进行编辑。 这就是抵消所代表的。

c.Offset(, 4)'=Column H Sheet "R"
FndC.Offset(, 3)'=Column F Sheet "D"
c.Offset(, -3)'=Column A Sheet "R"
c.Offset(, 3)'=Column G Sheet "R"

这是完整的代码。

Sub FindAndStuff()
    Dim D As Worksheet, R As Worksheet
    Dim ENDROW As Long, REND As Long
    Dim rng1 As Range, rng2 As Range
    Dim c As Range, FndC As Range

    Set D = Sheets("DWAC")
    Set R = Sheets("RAW")

    With D
        ENDROW = .Cells(.Rows.Count, "C").End(xlUp).Row
        Set rng2 = .Range("C2:C" & ENDROW) 'find value in column C sheet D
    End With


    With R
        REND = .Cells(.Rows.Count, "D").End(xlUp).Row
        Set rng1 = .Range("D2:D" & REND) 'Loop through column D in sheet R
    End With

    For Each c In rng1.Cells
        Set FndC = rng2.Find(what:=c, lookat:=xlWhole)

        If Not FndC Is Nothing Then

            If c.Offset(, 4) = FndC.Offset(, 3) Then
                c.Offset(, -3) = "Match"
            Else
                c.Offset(, -3) = "DIFFERENCE OF " & c.Offset(, 3) - FndC.Offset(, 3)
            End If
        Else: c.Offset(, -3) = "Not Found"

        End If
    Next c

End Sub

答案 1 :(得分:1)

我知道的最好的方法是使用布尔值:

Dim chck As Boolean
Dim chck2 As Boolean
For I = 2 To REND
    chck = False
    chck2 = False
    For P = 2 To ENDROW
        If Range("D" & I) = D.Range("c" & P) Then
            chck = True
            If Range("H" & I) = D.Range("F" & P) Then
                chck2 = True
                Exit For
            End If
        end if
    Next P    
    If chck And chck2 Then
        Range("A" & I) = "MATCHED"
    ElseIf chck Then
        Range("A" & I) = "DIFFERENCE OF " & Range("G" & I) - D.Range("F" & P)
    Else
        Range("A" & I) = "AP NOT FOUND"
    End If
Next I