Kailei Zhang
Kailei Zhang

Reputation: 11

Issues with a Loop Exiting

I am trying to write an "if or" statement to compare cells in six rows (A1 compare with B1, B1 to C1, so on and so forth). Somehow my if or statement does not seem to be working. Can someone help and check my code please?

Dim I As Byte

I = 2



If Range("b" & I) - Range("c" & I) > 0 _
Or Range("c" & I) - Range("d" & I) > 0 _
Or Range("d" & I) - Range("e" & I) > 0 _
Or Range("e" & I) - Range("f" & I) > 0 _
Or Range("f" & I) - Range("g" & I) > 0 _
Then

Range("h" & I).Value = y


Else

Range("h" & I).Value = N
End If

I = I + 1

Follow Up, the problem is now solved, thanks for everyone's help!

ThisWorkbook.Activate
Sheet1.Activate

Dim i As Long
Dim theColumn As Long
Dim LastCell As Long
LastCell = Range("b" & Rows.Count).End(xlUp).Row
Range("H2:H" & Cells(Rows.Count, "h").End(xlUp).Row).ClearContents
i = 1
Do

i = i + 1
For theColumn = 2 To 6
    Dim higher As Boolean
    higher = Cells(i, theColumn).Value - Cells(i, theColumn + 1).Value > 0
    If higher Then Exit For
Next

Cells(i, "H").Value = higher
If i = LastCell Then Exit Do
Loop

MsgBox "Analysis Completed"

Upvotes: 0

Views: 68

Answers (1)

Scott Holtzman
Scott Holtzman

Reputation: 27249

This may be a little easier to digest:

Dim theColumn as Long

For theColumn = 2 to 6 'compare columns B-G
    Dim higher as Boolean
    higher = Cells(I,theColumn).Value - Cells(I,theColumn+1).Value > 0
    If higher Then Exit For
Next

Cells(I,"H").Value = higher
'Cells(I,"H").Value = Iif(higher,"Y","N")

Upvotes: 3

Related Questions