簡體   English   中英

此代碼有什么問題? VBA

[英]What is wrong with this code? VBA

無論條件是否為真,單元格的值始終為“錯誤”。 我嘗試過使用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

我認為應該可以解決您的問題:

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

您的問題是您循環了許多單元格,但是在檢查條件之后將其寫入一個單元格。 因此,此單元格將僅包含上一次檢查的結果。 以前的值將被簡單地覆蓋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM