运行VBA for for循环

时间:2015-12-07 19:25:36

标签: excel vba excel-vba loops for-loop

我正在尝试使用以下VBA子查找每个后续列的初始负值/最后正值的相应第一列值,但是当我运行它时它立即给了我一个

  

“400”

错误。你们知道可能的原因吗?

Sub findzero()

For i = 225 To 335
    For j = 19 To 10018
        If Range(Cells(j, i)).Value < 0 Then
            Range(Cells(14, i)).Value = Range(Cells(j - 1, 1)).Value
            Exit For
        End If
    Next j
Next i

End Sub

1 个答案:

答案 0 :(得分:1)

当我运行此操作时,我得到的错误与您不同。我得到了&#34;方法&#39;范围&#39;对象&#39; _Global&#39;失败&#34。我不太确定为什么我们的消息不同。

无论如何,我不认为这是使用&#34; Range&#34;对象(对象?)正确。它要么是Range(Cells(j,i),Cells(j,i)).Value(选择&#34;范围&#34;只跨越一个单元格),要么Cells(j,i).

For i = 225 To 335
    For j = 19 To 10018
        If Cells(j, i) < 0 Then
            Cells(14, i) = Cells(j - 1, 1)
            Exit For
        End If
    Next j
Next i

或者,或者,

For i = 225 To 335
    For j = 19 To 10018
        If Range(Cells(j, i), Cells(j, i)).Value < 0 Then
            Range(Cells(14, i), Cells(14, i)).Value = Range(Cells(j - 1, 1), Cells(j - 1, 1)).Value
            Beep
            Exit For
        End If
    Next j
Next i

我个人更喜欢第一种方式。可能有更好的方法来做到这一点,但我不知道它会是什么。

相关问题