首页 > 解决方案 > 连续检查负值

问题描述

我正在尝试使用以下 vba 代码来检查Excel 工作表的第 1 行中带有数字的负值1, 2, -3, 4, 5, 6。宏应该选择-3值并停在那里。不幸的是,它不起作用。发生错误告诉我End If without block If。看不到我的错误。

Sub SelectNegativeValue()
    Dim Cell As Range

    For Each Cell In Range("1:1")
        If Cell.Value < 0 Then Cell.Select
            Exit For
        End If
    Next Cell
End Sub

标签: excelvba

解决方案


宏应该选择“-3”值并停在那里。

你需要移动Cell.Select到下一行......就在之前Exit For

Dim Cell As Range
For Each Cell In Range("1:1")
    If Cell.Value < 0 Then
        Cell.Select
        Exit For
    End If
Next Cell

推荐阅读