简体   繁体   中英

What is wrong with this code? VBA

The value of the cell is always "error" no matter if the condition is true or not. I've tried using else but it doesn't work either.

Sub ejer_4()
    Cells(3, 1).Value = "hola"
    For i = 2 To 21:
        If Int(Cells(i, 3).Value) <> Int(Cells(i + 1, 3).Value) - 1 Then
            Cells(3, 1).Value = "Error"
        End If
    Next
End Sub

I think that should solve your problem:

Sub ejer_4()
    For i = 2 To 21:
        If Int(Cells(i, 3).Value) <> Int(Cells(i + 1, 3).Value) - 1 Then
            Cells(i, 1).Value = "Error"
        Else
            Cells(i, 1).Value = "hola"
        End If
    Next
End Sub

You problem is that you loop thorugh range of cells, but after checking condition you write into one cell. So, this cell will contain oonly the result of the last check. Previous values will be simply overwritten.

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