If a range does not contain a specific value
This tutorial shows how to test if a range does not contain a specific value and return a specified value if the test is True or False through the use of an Excel formula or VBA
Example: If a range does not contain a specific value
This formula uses the Excel COUNTIF function to count the number of cells in a range (C8:C14) that have a value equal to the value in cell C5. The Excel IF function is then used to test if the Excel COUNTIF function does not find any cells in the range that have a value equal to the value in cell C5. If the test is TRUE the formula will return a "Not in Range" value, alternatively if the test is FALSE the formula will return a "In Range" value.
|
Sub If_a_range_does_not_contain_a_specific_value()
'declare a variable
Dim ws As Worksheet
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'calculate if a range does not contain the value in cell (C5) and then return a specified value
If Application.WorksheetFunction.CountIf(ws.Range("C8:C14"), ws.Range("C5")) = 0 Then
If Application.WorksheetFunction.CountIf(ws.Range("C8:C14"), ws.Range("C5")) = 0 Then
ws.Range("E8") = "Not in Range"
Else
ws.Range("E8") = "In Range"
End If
End Sub
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("E8") in the VBA code.
Range to Test: Select the range that you want to search through by changing the range reference ("C8:C14") in the VBA code.
Specific Value: Select the specific value that you want to test for by changing the value in cell ("C5").
Worksheet Selection: Select the worksheet which captures the range that you want to test if it doesn't contain 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 range doesn't contain a specific value the VBA code will return a value of "Not in Range". If a range contains a specific value the VBA code will return a value of "In Range". Both of these values can be changed to whatever value you desire by directly changing them in the VBA code.
Output Range: Select the output range by changing the cell reference ("E8") in the VBA code.
Range to Test: Select the range that you want to search through by changing the range reference ("C8:C14") in the VBA code.
Specific Value: Select the specific value that you want to test for by changing the value in cell ("C5").
Worksheet Selection: Select the worksheet which captures the range that you want to test if it doesn't contain 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 range doesn't contain a specific value the VBA code will return a value of "Not in Range". If a range contains a specific value the VBA code will return a value of "In Range". Both of these values can be changed to whatever value you desire by directly changing them in the VBA code.
ADDITIONAL NOTES
Note 1: 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.
Note 1: 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.
Explanation about the formula used to check if a range does not contain a specific value
EXPLANATION
EXPLANATION
This tutorial shows how to test if a range does not contain a specific value and return a specified value if the formula tests true or false, by using an Excel formula and VBA.
This tutorial provides one Excel method that can be applied to test if a range does not contain a specific value and return a specified value by using an Excel IF and COUNTIF functions. In this example, if the Excel COUNTIF function returns a value of 0, meaning the range does not have cells with a value of 505, the test is TRUE and the formula will return a "Not in Range" value. Alternatively, if the Excel COUNTIF function returns a value of greater than 0, meaning the range has cells with a value of 505, the test is FALSE and the formula will return a "In Range" value.
This tutorial provides one VBA method that can be applied to test if a range does not contain a specific value and return a specified value and return a specified value.
FORMULA
=IF(COUNTIF(range, value)=0, value_if_true, value_if_false)
=IF(COUNTIF(range, value)=0, value_if_true, value_if_false)
ARGUMENTS
range: The range of cells you want to count from.
value: he value that is used to determine which of the cells should be counted, from a specified range, if the cells' value is equal to this value.
value_if_true: Value to be returned if the range does not contains the specific value.
value_if_false: Value to be returned if the range contains the specific value.
range: The range of cells you want to count from.
value: he value that is used to determine which of the cells should be counted, from a specified range, if the cells' value is equal to this value.
value_if_true: Value to be returned if the range does not contains the specific value.
value_if_false: Value to be returned if the range contains the specific value.
RELATED TOPICS
Related Topic | Description | Related Topic and Description |
---|---|---|
If a range contains a specific value by column | How to test if a range contains a specific value by column and return a specified value using Excel and VBA methods | |
If a range contains a value greater than | How to test if a range contains a value greater than a specific value and return a specified value using Excel and VBA methods | |
If a range contains a specific value by row | How to test if a range contains a specific value by row and return a specified value using Excel and VBA methods | |
If a range contains a value greater than or equal to | How to test if a range contains a value greater than or equal to a specific value and return a specified value using Excel and VBA methods | |
If a range contains a specific value | How to test if a range contains a specific value and return a specified value using Excel and VBA methods |
RELATED FUNCTIONS
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 | |
COUNTIF Function | The Excel COUNTIF function returns the number of cells in a range that meet a specified criteria |