If a cell is not blank in a range

This tutorial shows how to test if a cell is not blank in a range and return a value if the test is True or False through the use of an Excel formula, with the IF and COUNTA functions, or VBA

EXCEL FORMULA 1. If a cell is not blank in a range

EXCEL

Hard coded formula
If a cell is not blank in a range
Cell reference formula
If a cell is not blank in a range
=IF(COUNTA(C5:E5)>0,"Has Value","No Value")
=IF(COUNTA(C9:E9)>0,$C$5,$C$6)
GENERIC FORMULA

=IF(COUNTA(range)>0, value_if_true, value_if_false)

ARGUMENTS
range: A range of cells that you want to test.
value_if_true: Return a value if the logic tests TRUE.
value_if_false: Return a value if the logic tests FALSE.

GENERIC FORMULA

=IF(COUNTA(range)>0, value_if_true, value_if_false)

ARGUMENTS
range: A range of cells that you want to test.
value_if_true: Return a value if the logic tests TRUE.
value_if_false: Return a value if the logic tests FALSE.

EXPLANATION

This formula uses a combination of the IF and COUNTA functions to assess if there are any non-empty cells in a specific range. The COUNTA function returns the number of non blank cells from a specified range. Therefore, if the COUNTA function returns a value greater than 0, it means that the selected range contains a non blank cell. If the COUNTA function returns a value of 0, it means that all of the cells in the specified range are blank.
The IF function will then assess if the COUNTA function has returned a value of greater than 0 and if so then the formula will return a value that has been assigned as the true value, alternatively if the COUNTA function has returned a value of 0 the formula will return a value assigned as the false value.
With this formula you can enter the values, that will be returned if a range does or does not contain any empty cells, directly into the formula or reference them to specific cells that capture these values.
Click on either the Hard Coded or Cell Reference button to view the formula that has the return values directly entered into the formula or referenced to specific cells that capture these values, respectively.
In this example the formula tests if a selected range contains non-blank cells. If a range contains at least one cell that is not empty the formula will return a value of "Has Value" (hard coded example) or value in cell C5 (cell reference example). If a range contains all empty cells the formula will return a value of "No Value" (hard coded example) or value in cell C6 (cell reference example).

If you are using the formula with values entered directly in the formula and want to return a numerical value, instead of a text value, you do not need to apply the double quotation marks around the values that are to be returned e.g. (=IF(COUNTA(C5:E5)>0,1,0)).

VBA CODE 1. If a cell is not blank in a range

VBA

Hard coded against single cell
Sub If_cell_is_not_blank_in_range()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'calculate if a cell is not blank in a range
If ws.Application.WorksheetFunction.CountA(ws.Range("C5:E5")) > 0 Then

ws.Range("F5") = "Has Value"

Else

ws.Range("F5") = "No Value"

End If

End Sub

Cell reference against single cell
Sub If_cell_is_not_blank_in_range()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'calculate if a cell is not blank in a range
If ws.Application.WorksheetFunction.CountA(ws.Range("C9:E9")) > 0 Then

ws.Range("F9") = ws.Range("C5")

Else

ws.Range("F9") = ws.Range("C6")

End If

End Sub

Hard coded against range of cells
Sub If_cell_is_not_blank_in_range()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")

'calculate if a cell is not blank in a range by looping through each cell in the specified range

For x = 5 To 8

If ws.Application.WorksheetFunction.CountA(ws.Range(ws.Cells(x, 3), ws.Cells(x, 5))) > 0 Then

ws.Cells(x, 6) = "Has Value"

Else

ws.Cells(x, 6) = "No Value"
End If

Next

End Sub

Cell reference against range of cells
Sub If_cell_is_not_blank_in_range()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")

'calculate if a cell is not blank in a range by looping through each cell in the specified range

For x = 9 To 12

If ws.Application.WorksheetFunction.CountA(ws.Range(ws.Cells(x, 3), ws.Cells(x, 5))) > 0 Then

ws.Cells(x, 6) = ws.Range("C5")

Else

ws.Cells(x, 6) = ws.Range("C6")
End If

Next

End Sub

KEY PARAMETERS
Output Range: Select the output range by changing the cell reference ("F5") in the VBA code.
Range to Test: Select the range that is to be tested by changing the range reference ("C5:E5") in the VBA code.
Worksheet Selection: Select the worksheet which captures the range of cells that you want to test if they are not blank and return a specific value by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
True and False Results: In this example if a single cell is not blank in a range the VBA code will return a value of "Has Value". However, if all of the cells in the selected range are blank the VBA code will return a value of "No Value". Both of these values can be changed to whatever value you desire by directly changing them in the VBA code.
NOTES
Note 1: If a cell that is being tested is returning a value of ("") this VBA code will identify the cell as non blank.
Note 2: If your True or False result is a text value it will need to be captured within quotation marks (""). However, if the result is a numeric value, you can enter it without the use of quotation marks.
KEY PARAMETERS
Output Range: Select the output range by changing the cell reference ("F9") in the VBA code.
Range to Test: Select the range that is to be tested by changing the range reference ("C9:E9") in the VBA code.
Worksheet Selection: Select the worksheet which captures the range of cells that you want to test if they are not blank and return a specific value by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
True and False Results: In this example if a single cell is not blank in a range the VBA code will return a value stored in cell C5. However, if all of the cells in the selected range are blank the VBA code will return a value stored in cell C6. Both of these values can be changed to whatever value you desire by either referencing to a different cell that captures the value that you want to return or change the values in those cells.
NOTES
Note 1: If a cell that is being tested is returning a value of ("") this VBA code will identify the cell as non blank.
PARAMETERS
Output and Data Rows: Select the output rows and the rows that captures the cells that are to be tested by changing the x values (5 to 8). This example assumes that both the output and the associated test cell will be in the same row.
Test Columns: Select the columns that capture the cells that are to be tested by changing numbers 3 and 5, in ws.Range(ws.Cells(x, 3), ws.Cells(x, 5)).
Output Column: Select the output column by changing number 6, in ws.Cells(x, 6).
Worksheet Selection: Select the worksheet which captures the range of cells that you want to test if they are not blank and return a specific value by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
True and False Results: In this example if a single cell is not blank in a range the VBA code will return a value of "Has Value". However, if all of the cells in the selected range are blank the VBA code will return a value of "No Value". Both of these values can be changed to whatever value you desire by directly changing them in the VBA code.
NOTES
Note 1: If a cell that is being tested is returning a value of ("") this VBA code will identify the cell as blank.
Note 2: If your True or False result is a text value it will need to be captured within quotation marks (""). However, if the result is a numeric value, you can enter it without the use of quotation marks.
PARAMETERS
Output and Data Rows: Select the output rows and the rows that captures the cells that are to be tested by changing the x values (9 to 12). This example assumes that both the output and the associated test cell will be in the same row.
Test Columns: Select the columns that capture the cells that are to be tested by changing numbers 3 and 5, in ws.Range(ws.Cells(x, 3), ws.Cells(x, 5)).
Output Column: Select the output column by changing number 6, in ws.Cells(x, 6).
Worksheet Selection: Select the worksheet which captures the range of cells that you want to test if they are not blank and return a specific value by changing the Analysis worksheet name in the VBA code. You can also change the name of this object variable, by changing the name 'ws' in the VBA code.
True and False Results: In this example if a single cell is not blank in a range the VBA code will return a value stored in cell C5. However, if all of the cells in the selected range are blank the VBA code will return a value stored in cell C6. Both of these values can be changed to whatever value you desire by directly changing them in the VBA code.
NOTES
Note 1: If a cell that is being tested is returning a value of ("") this VBA code will identify the cell as blank.

RELATED TOPICS

Related Topic Description Related Topic and Description
How to test if a cell is blank and return a specified value using Excel and VBA methods
How to test if a cell is not blank and return a value using Excel and VBA methods
How to test if a cell is blank in a range and return a value using Excel and VBA methods
How to count cells that are blank using Excel and VBA methods
How to count cells that are not blank using Excel and VBA methods

RELATED FUNCTIONS

Related Functions Description Related Functions and Description
The Excel IF function performs a test on specified conditions entered into the formula and returns a specified value if the result is TRUE or another specified value if the result is FALSE
The Excel COUNTA function returns the number of non-empty cells from a specified range