angular2neewbie
angular2neewbie

Reputation: 129

Check if cell not empty

I want to write a code that does "if cell(A) in row not empty then set text "Yes" in cell B in the same row, and this should loop through the whole sheet.

I do not have a code.

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

but how do I make it check "if not empty" as the value?

Upvotes: 1

Views: 24361

Answers (2)

R3uK
R3uK

Reputation: 14537

You should use vbNullString which is the constant name to designate "" (empty string) value.

So it could be these two options :

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

Or

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

Try this :

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

Upvotes: 2

Paul Ogilvie
Paul Ogilvie

Reputation: 25266

Simply use "" to check for empty:

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

Upvotes: 3

Related Questions