82

Via VBA how can I check if a cell is empty from another with specific information?

For example:

If A:A = "product special" And B:B is null Then

C1 = "product special"

Illustration of example

Additionally, how can I use a For Each loop on theRange and how can I return the value in the other cell?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Regis Santos
  • 3,469
  • 8
  • 43
  • 65

3 Answers3

85

You could use IsEmpty() function like this:

...
Set rRng = Sheet1.Range("A10")
If IsEmpty(rRng.Value) Then ...

you could also use following:

If ActiveCell.Value = vbNullString Then ...
Zoe
  • 27,060
  • 21
  • 118
  • 148
Sylca
  • 2,523
  • 4
  • 31
  • 51
  • 4
    Note that `If ActiveCell.Value = vbNullString` will fail in the event that the cell contain an error such as #N/A! Better to use IsEmpty(rRang) – jeffreyweir Apr 24 '15 at 22:04
23

IsEmpty() would be the quickest way to check for that.

IsNull() would seem like a similar solution, but keep in mind Null has to be assigned to the cell; it's not inherently created in the cell.

Also, you can check the cell by:

count()

counta()

Len(range("BCell").Value) = 0

Deafdan
  • 393
  • 2
  • 13
11

This site uses the method isEmpty().

Edit: content grabbed from site, before the url will going to be invalid.

Worksheets("Sheet1").Range("A1").Sort _
    key1:=Worksheets("Sheet1").Range("A1")
Set currentCell = Worksheets("Sheet1").Range("A1")
Do While Not IsEmpty(currentCell)
    Set nextCell = currentCell.Offset(1, 0)
    If nextCell.Value = currentCell.Value Then
        currentCell.EntireRow.Delete
    End If
    Set currentCell = nextCell
Loop

In the first step the data in the first column from Sheet1 will be sort. In the second step, all rows with same data will be removed.

Reporter
  • 3,897
  • 5
  • 33
  • 47