Excel: How to check if a cell is empty with VBA?

ExcelVba

Excel Problem Overview


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?

Excel Solutions


Solution 1 - Excel

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 ...

Solution 2 - Excel

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

Solution 3 - Excel

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRegis SantosView Question on Stackoverflow
Solution 1 - ExcelSylcaView Answer on Stackoverflow
Solution 2 - ExcelDeafdanView Answer on Stackoverflow
Solution 3 - ExcelReporterView Answer on Stackoverflow