检查单元格是否为空

时间:2015-06-08 11:16:16

标签: excel vba excel-vba

我想编写一个代码,“如果行中的单元格(A)不为空,则在同一行的单元格B中设置文本”是“,这应该遍历整个工作表。

我没有代码。

Sub Check()
    Dim N As Long, i As Long, j As Long
    N = Cells(Rows.Count, "A").End(xlUp).row
    j = 2
    For i = 2 To N
        If Cells(i, "A").Value = "??" Then
            Cells(j, "B").Value = "Yes"
            j = j + 1
        End If
    Next i
End Sub

但是如何让它检查“if not empty”作为值?

2 个答案:

答案 0 :(得分:2)

只需使用""检查空:

If .Cells(i, "A").Value <> "" Then ...

答案 1 :(得分:0)

您应该使用vbNullString这是常量名称来指定""(空字符串)值。

所以可能是这两个选项:

If .Cells(i, "A").Value <> vbNullString Then

或者

If .Cells(i, "A").Value <> "" Then

试试这个:

Dim IsRunning as Boolean
Sub Check()
    If IsRunning Then Exit Sub
    Dim N As Long, i As Long, j As Long
    IsRunning = True
    With ActiveSheet
        N = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 2 To N
            If .Cells(i, "A").Value <> vbNullString Then
                .Cells(i, "B").Value = "Yes"
            End If
        Next i
    End With
    IsRunning= False
End Sub