简体   繁体   English

此代码有什么问题? VBA

[英]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. 我尝试过使用else,但是也不起作用。

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. 以前的值将被简单地覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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