简体   繁体   中英

Dropping out of a Do or For Loop- not working for me

I am attempting to mark rows based on criteria, by putting a value in a column. In columns a thru f, about 35,000 rows, I have my data. In columns i thru j, only 51 rows, I have my criteria. Column b is empty and I want to fill it with either "KEEP" or "DELETE" based if columns c and f, meet the criteria in columns i and j. Once I hit a "KEEP" value, I want the inner loop to stop and continue with the outer loop. I don't really know exactly what I'm doing (obviously). I would appreciate some help from someone who does. Thank you!

在此处输入图片说明

Sub test()

Dim row As Integer
Dim row2 As Integer
Dim col As Integer
Dim col2 As Integer
Dim col3 As Integer
Dim col4 As Integer
Dim col5 As Integer
Dim x As String
Dim y As String
Dim a As String
Dim b As String

row = 2
row2 = 2
col5 = 2

Do
    col = 3
    col2 = 6
    col3 = 9
    col4 = 10
    x = Cells(row, col).Value
    y = Cells(row, col2).Value
    Z = Cells(row, col5).Value
       For row2 = 2 To 52
            a = Cells(row2, col3).Value
            b = Cells(row2, col4).Value
            If x = a And y = b Then
                Cells(row, col - 1) = "KEEP"
                If (Z = "KEEP") Then Exit For
                Else
                    Cells(row, col - 1) = "DELETE"
            End If
            row2 = row2 + 1
        Next
    row = row + 1
    row2 = 2
Loop Until row >= 33600


End Sub

Change:

If (Z = "KEEP") Then Exit For

to:

Exit do

EDIT

Put your check in front.

For row2 = 2 To 52
     if z = "KEEP" then
        Exit for
     else
        a = Cells(row2, col3).Value
        b = Cells(row2, col4).Value
        If x = a And y = b Then
            Cells(row, col - 1) = "KEEP"
            Exit For
        Else
            Cells(row, col - 1) = "DELETE"
        End If
        row2 = row2 + 1
    end if
Next

Because the correct way is :

Do Until row >= 33600

'......

Loop

With a for

for row = 2 to 33600
   '...
                If (Z = "KEEP") Then 
                    Exit For
                Else
                   '...
next row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM