Count cells that contain a specific value
This tutorial shows how to count cells that contain a specific value through the use of Excel formulas or VBA
Example: Count cells that contain a specific value
=COUNTIF(B5:B7,"*"&D5&"*")
|
This formula uses the Excel COUNTIF function to count the number of cells in range (B5:B7) that contain a value specified in cell D5. If a cell contains the specific value in addition to other content, this formula will still recognise and count this cell.
Given the formula references to a cell that contains a specific value that we are searching for there is a need to apply the double quotation marks around the asterisk (*). |
METHOD 2. Count cells that contain a specific value with the value entered directly into the formula
EXCEL
=COUNTIF(B5:B7,"*Exceldome*")
|
This formula uses the Excel COUNTIF function to count the number of cells in range (B5:B7) that contain a value of Exceldome, which is directly entered into the formula. If a cell contains the specific value in addition to other content, this formula will still recognise and count this cell.
|
Sub Count_cells_that_contain_a_specific_value()
'declare a variable
Dim ws As Worksheet
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'apply the formula to count the number of cells in range (B5:B7) that contain a value specified in cell (D5)
ws.Range("E5") = Application.WorksheetFunction.CountIf(ws.Range("B5:B7"), "*" & ws.Range("D5") & "*")
ws.Range("E5") = Application.WorksheetFunction.CountIf(ws.Range("B5:B7"), "*" & ws.Range("D5") & "*")
End Sub
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("E5") in the VBA code.
Range: Select the range from which you want to count cells that contain a specific value by changing the range reference ("B5:B7") in the VBA code.
Specific Value: Select the value that you want to count for by changing the cell reference ("D5") in the VBA code.
Worksheet Selection: Select the worksheet which captures a range of cells from which you want to count for the 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.
Output Range: Select the output range by changing the cell reference ("E5") in the VBA code.
Range: Select the range from which you want to count cells that contain a specific value by changing the range reference ("B5:B7") in the VBA code.
Specific Value: Select the value that you want to count for by changing the cell reference ("D5") in the VBA code.
Worksheet Selection: Select the worksheet which captures a range of cells from which you want to count for the 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.
METHOD 2. Count cells that contain a specific value with the value entered directly into the VBA code
VBA
Sub Count_cells_that_contain_a_specific_value()
'declare a variable
Dim ws As Worksheet
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'apply the formula to count the number of cells in range (B5:B7) that contain Exceldome
ws.Range("E5") = Application.WorksheetFunction.CountIf(ws.Range("B5:B7"), "*Exceldome*")
ws.Range("E5") = Application.WorksheetFunction.CountIf(ws.Range("B5:B7"), "*Exceldome*")
End Sub
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("E5") in the VBA code.
Range: Select the range from which you want to count cells that contain a specific value by changing the range reference ("B5:B7") in the VBA code.
Specific Value: Select the value that you want to count for by changing the 'Exceldome' value in the VBA code.
Worksheet Selection: Select the worksheet which captures a range of cells from which you want to count for the 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.
Output Range: Select the output range by changing the cell reference ("E5") in the VBA code.
Range: Select the range from which you want to count cells that contain a specific value by changing the range reference ("B5:B7") in the VBA code.
Specific Value: Select the value that you want to count for by changing the 'Exceldome' value in the VBA code.
Worksheet Selection: Select the worksheet which captures a range of cells from which you want to count for the 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.
EXPLANATION
This tutorial shows and explains how to count cells that contain a specific value by using Excel formulas or VBA.
This tutorial provides two Excel methods that can be applied to count cells that contain a specific value in a selected range by using an Excel COUNTIF function. The first method reference to a cell that capture the value that we want to count for, whilst the second method has the value (Exceldome) directly entered into the formula.
This tutorial provides two VBA methods that can be applied to count cells that contain a specific value in a selected range. The first method reference to a cell that capture the value that we want to count for, whilst the second method has the value (Exceldome) directly entered into the VBA code.
By including asterisk (*) in front and behind the value that we are searching for it will ensure that the formula will still count a cell if there is other content, in addition to the specified value, in the cell.
FORMULA (value manually entered)
=COUNTIF(range, "*value*")
=COUNTIF(range, "*value*")
FORMULA (value sourced from cell reference)
=COUNTIF(range, "*"&value&"*")
=COUNTIF(range, "*"&value&"*")
ARGUMENTS
range: The range of cells you want to count from.
value: The value that is used to determine which of the cells should be counted, from a specified range, if the cells contain this value.
range: The range of cells you want to count from.
value: The value that is used to determine which of the cells should be counted, from a specified range, if the cells contain this value.
RELATED TOPICS
Related Topic | Description | Related Topic and Description |
---|---|---|
If a cell contains text | How to test if a cell contains text and return a specified value using Excel and VBA methods | |
Count cells that contain text | How to count cells that contain text using Excel and VBA methods | |
Count cells that do not contain a specific value | How to count cells that do not contain a specific 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
Related Functions | Description | Related Functions and Description |
---|---|---|
COUNTIF Function | The Excel COUNTIF function returns the number of cells in a range that meet a specified criteria |