HomeGoogle DocsSpreadsheetVarying Array Sizes in Countifs in Google Sheets

Varying Array Sizes in Countifs in Google Sheets

Published on

If you use varying array sizes in a Countifs formula in Google Sheets, you will get an error. It’s because the function doesn’t support such usage.

It usually happens when you specify mismatching columns in criteria range 1 and 2 in Countifs.

You may please go through the function syntax first. It’s as follows.

Syntax: COUNTIFS(criteria_range1, criterion1, [criteria_range2, …], [criterion2, …])

When you specify multiple criteria ranges, all the arrays/ranges should have the same size vertically and horizontally.

That means the number of rows and columns should match or be the same in all the specified criteria ranges/arrays.

Let’s quickly go to an example to understand it.

=countifs(A2:D10,"Apple",E2:E10,"Yes")
Varying Array Sizes in Countifs and Error in Google Sheets

Here are the four arguments in use.

1. criteria_range1 – A2:D10

2. criterion1 – “Apple”

3. criteria_range2 – E2:E10

4. criterion2 – “Yes”

As you can see, the above two array arguments (criteria_range1 and criteria_range2) to COUNTIFS are different in size.

There are four columns in the first array (argument 1) and one column in the second array (argument 3).

In the above example, I want to count the “Apple” in multiple column range A2:D10 if the values in E2:E10=”Yes.”

Workarounds to Use Varying Array Sizes in Countifs in Google Sheets

I have three workaround formulas for you to try when you have varying array sizes in Countifs that cause the #VALUE! as above.

I may be able to come up with more. But the below options will be sufficient for you.

Option 1 – Varying Array Sizes in Countifs with Virtual Criteria Ranges

We can follow different approaches to use varying array sizes in Countifs in Google Sheets.

To logically fit the Countifs rule, we can create a virtual criteria_range2 with an equal number of columns.

We have earlier adopted this method in SUMIF, i.e., to return the Sum of Matrix Rows or Columns Using Sumif in Google Sheets.

I am following the same technique with COUNTIFS here.

Formula # 1:

=countifs(A2:D10,"Apple", ARRAYFORMULA(if(E2:E10="Yes",column(A2:D2)^0)),1)

Result: 9

The above is the first option to specify mismatching columns in criteria range 1 and 2 in Countifs.

Want to learn the above COUNTIFS formula in detail?

Formula Explanation

criteria_range1 – A2:D10

criterion1 – “Apple”

criteria_range2 – ARRAYFORMULA(if(E2:E10="Yes",column(A2:D2)^0))

criterion2 – 1

In the Countifs (Formula # 1), the criteria_range2 is an array formula.

It would return an array with the number of columns equal to the criteria_range1.

The number of rows is already equal in both arrays, i.e., in criteria_range1 and 2.

This virtual array will have the number 1 in each column corresponding to “Yes” in E2:E10. That’s why I have used # 1 instead of “Yes” in criterion_2.

Varying Array Sizes in Countifs - Solution

Here are the other two options that you can consider over the above formula.

Option 2 – Based on Filtering and Logical Tests

Without virtually matching varying array sizes in Countifs, we can get our desired result.

You can find below two such formula combinations, and I have marked my recommended formula also.

Formula # 2 (Recommended):

=countif(flatten(filter(A2:D10,E2:E10="Yes")),"Apple")

Here I have used Countif instead of Countifs as we have only one column after manipulating the data with FILTER and FLATTEN.

It works like this.

  1. The Filter filters the rows in the range A2:D10 if E2:E10 is “Yes.”
  2. The Flatten makes four columns into one column.
  3. The Countif does the rest (counts the “Apple”).

Formula # 3:

=sum(ArrayFormula(if((A2:D10="Apple")*(E2:E10="Yes")=1,1,)))

The above is another formula to consider when we have varying array sizes to specify in Countifs in Google Sheets.

How does this formula return the count of values conditionally from multiple mismatching columns?

Here is how!

The logical test, the formula enclosed within SUM, returns the # 1 wherever both conditions match. Please see the below illustration.

Mismatching Columns in Criteria Ranges in Countifs

The SUM function returns the sum of those values.

If you have doubts about the array formula that I have used within SUM, you can learn that here – How to Use IF, AND, OR in Array in Google Sheets.

It’s just an alternative way to code AND logical test in array form.

I hope you could understand the above tips.

Thanks for the stay. Enjoy!

Countif | Countifs Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Running Total By Month in Excel

This tutorial demonstrates how to calculate the running total by month in a range...

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

More like this

Creating Sequential Dates in Equally Merged Cells in Google Sheets

Do you know how to create sequential dates in equally merged cells across a...

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.