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
GENERIC FORMULA
=IF(COUNTA(range)>0, value_if_true, value_if_false)
ARGUMENTS GENERIC FORMULA
=IF(COUNTA(range)>0, value_if_true, value_if_false)
ARGUMENTS 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)). |
Dim ws As Worksheet
If ws.Application.WorksheetFunction.CountA(ws.Range("C5:E5")) > 0 Then
Else
End If
End Sub
Dim ws As Worksheet
If ws.Application.WorksheetFunction.CountA(ws.Range("C9:E9")) > 0 Then
Else
End If
End Sub
Dim ws As Worksheet
'calculate if a cell is not blank in a range by looping through each cell in the specified range
If ws.Application.WorksheetFunction.CountA(ws.Range(ws.Cells(x, 3), ws.Cells(x, 5))) > 0 Then
Else
Next
End Sub
Dim ws As Worksheet
'calculate if a cell is not blank in a range by looping through each cell in the specified range
If ws.Application.WorksheetFunction.CountA(ws.Range(ws.Cells(x, 3), ws.Cells(x, 5))) > 0 Then
Else
Next
End Sub
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.
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.
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.
Note 1: If a cell that is being tested is returning a value of ("") this VBA code will identify the cell as non blank.
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.
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.
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.
Note 1: If a cell that is being tested is returning a value of ("") this VBA code will identify the cell as blank.
Related Topic | Description | Related Topic and Description |
---|---|---|
If a cell is blank | How to test if a cell is blank and return a specified value using Excel and VBA methods | |
If a cell is not blank | How to test if a cell is not blank and return a value using Excel and VBA methods | |
If a cell is blank in a range | How to test if a cell is blank in a range and return a value using Excel and VBA methods | |
Count cells that are blank | How to count cells that are blank using Excel and VBA methods | |
Count cells that are not blank | How to count cells that are not blank using Excel and VBA methods |
Related Functions | Description | Related Functions and Description |
---|---|---|
IF Function | 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 | |
COUNTA Function | The Excel COUNTA function returns the number of non-empty cells from a specified range |