(Excel examples for Beginners)
In this end-to-end excel example, you will learn – How to Count rows that contain specific values in Excel.
How to Count rows that contain specific values in Excel
Generic formula
=SUM(--(MMULT(--(criteria),TRANSPOSE(COLUMN(data)))>0))
Explanation
To count rows that contain specific values, you can use an array formula based on the MMULT, TRANSPOSE, COLUMN, and SUM functions. In the example shown, the formula in G5 is:
{=SUM(--(MMULT(--(data=90),TRANSPOSE(COLUMN(data)))>0))}
where data is the named range B4:B12.
Note: this is an array formula and must be entered with control shift enter.
How this formula works
Working from the inside out, the logical criteria used in this formula is:
--(data=90)
where data is the named range B4:D12. This generates a TRUE / FALSE result for every value in data, and the double negative coerces the TRUE FALSE values to 1 and 0 to yield an array like this:
{1,0,0;0,0,0;0,1,1;1,0,0;0,0,0;0,0,0;0,0,0;0,0,0;0,0,1}
Like the original data, this array is 9 rows by 3 columns (9 x 3) and goes into the MMULT function as array1.
Array2 is derived with:
TRANSPOSE(COLUMN(data))
This is the tricky and fun part of this formula. The COLUMN function is used simply for convenience as a way to generate a numeric array of the right size. To perform matrix multiplication with MMULT, the column count in array1 (3) must equal the row count in array2.
COLUMN returns the 3-column array {2,3,4}, and TRANSPOSE changes this array to the the 3-row array {2;3;4}. MMULT then runs and returns a 9 x 1 array result:
=SUM(--({2;0;7;2;0;0;0;0;4}>0))
We check for non-zero entries with >0 and again coerce TRUE FALSE to 1 and 0 with a double negative to get a final array inside SUM:
=SUM({1;0;1;1;0;0;0;0;1})
In this final array, a 1 represents a row where the logical test (data=90) returned true. The total returned by SUM is a count of all rows that contain the number 90.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
C++ for Beginners: C++ Program to Find Transpose of a Matrix
Learn Java by Example: Java Program to Find Transpose of a Matrix