Sub topic 8
Keeping ranges the same size
This chapter assumes you can write a basic COUNTIFS, SUMIFS, or AVERAGEIFS formula and that you know how A1 range notation and $ anchoring work. Here you learn the rule that all range arguments must have the same shape, how to diagnose the error when they don't, and how to catch the harder case where ranges match in size but are misaligned.
The examples use a sales table with headers in row 1 and data in rows 2 to 100:
| Column | Contents |
|---|---|
| A | Region |
| B | Product |
| C | Date |
| D | Units |
| E | Revenue |
The rule
SUMIFS, COUNTIFS, and AVERAGEIFS work row by row. For each position in the first criteria range, the function looks at the matching position in every other range. If row 5 of the Region range equals "West", the function takes row 5 of the Revenue range. That pairing only works if every range has the same number of rows and the same number of columns.
This formula is valid because each range is 99 rows by 1 column:
=SUMIFS(E2:E100, A2:A100, "West")This formula is not, because the sum range is 98 rows tall and the criteria range is 99 rows tall:
=SUMIFS(E2:E99, A2:A100, "West")Google Sheets returns #VALUE!. Hovering over the cell shows a message like "Array arguments to SUMIFS are of different sizes." COUNTIFS and AVERAGEIFS return the same error with their own names in the message.
The rule applies to every range in the formula:
- In
SUMIFS, thesum_rangeand every criteria range must match. - In
AVERAGEIFS, theaverage_rangeand every criteria range must match. - In
COUNTIFS, there is no value range, but all criteria ranges must still match each other.
The criteria themselves ("West", ">=10", G2) are single values and have no size. The rule concerns only the ranges.
What "same size" means
Size means shape, not the count of cells. Both dimensions must match.
| Range pair | Same shape? | Why |
|---|---|---|
| `A2:A100` and `E2:E100` | Yes | 99 rows by 1 column each |
| `A2:A100` and `E3:E101` | Yes | 99 rows by 1 column each, but misaligned (see below) |
| `A2:A100` and `E2:E99` | No | 99 rows against 98 rows |
| `A2:A100` and `B2:C100` | No | 1 column against 2 columns |
| `A2:A100` and `A2:G2` | No | 99 rows by 1 column against 1 row by 7 columns. Both have different shapes even though a column and a row might look similar |
| `A:A` and `E:E` | Yes | Both span the whole column |
| `A:A` and `E2:E100` | No | The whole column is far taller than 99 rows |
Position does not matter. The ranges can sit in different columns, on different sheets, or in different parts of a sheet. Only the dimensions have to agree.
=SUMIFS(Data!E2:E100, Data!A2:A100, "West", Data!D2:D100, ">=10")Here three ranges, all 99 rows by 1 column, are used together. The formula is valid.
Common causes of mismatches
Most mismatches come from editing, not from typing the first formula.
- Retyping one end by hand. You extend the criteria range to row 150 and forget the sum range.
- Mixing whole-column and bounded ranges.
E2:E100withA:Afails. Pick one style for the whole formula. - Mixing open-ended ranges and bounded ranges.
A2:Aruns to the bottom of the sheet. Pair it withE2:E, notE2:E100. - Inserting cells instead of rows. If you insert a row across the whole sheet, every range grows together. If you insert cells in one column only and choose "Shift down", only the ranges covering that column grow. The ranges then differ by one row.
- Selecting a header. One range starts at row 1 and the others at row 2.
- Copying a formula to a new area with partly absolute references, so one range moves and another stays fixed. Movement alone doesn't change a range's size, but anchoring one end of a range (
$A2:A$100style mistakes) can.
Diagnosing a `#VALUE!` error
You can check the shape of each range with ROWS and COLUMNS:
=ROWS(E2:E99)
=ROWS(A2:A100)
=COLUMNS(E2:E99)These return 98, 99, and 1. The 98 against 99 shows the mismatch immediately.
You can also test all ranges in one cell. This returns TRUE only when the two ranges have the same shape:
=AND(ROWS(E2:E100)=ROWS(A2:A100), COLUMNS(E2:E100)=COLUMNS(A2:A100))The silent failure: same size, wrong alignment
A size mismatch is the friendly failure, because Sheets tells you. The dangerous case is a pair of ranges that are the same size but start on different rows:
=SUMIFS(E3:E101, A2:A100, "West")Both ranges are 99 rows by 1 column, so there is no error. But the revenue in row 3 is now paired with the region in row 2, row 4 with row 3, and so on. The result is a number that looks plausible and is wrong.
To protect against this:
- Write every range in a formula with the same first and last row numbers.
- Compare your formula with a known case. Filter the table by "West", select the revenue column, and check the sum shown in the bottom-right corner against your formula.
- Use whole-column references (
E:E,A:A) or open-ended references (E2:E,A2:A) when the table grows, so the start and end rows cannot drift apart.
Absolute references and fill-down
A summary table copies one formula down many rows, so anchoring matters. In this formula, the ranges are anchored with $ and only the criterion G2 changes as you fill down:
=IFERROR(AVERAGEIFS($E$2:$E$100, $A$2:$A$100, G2), "No matches")Each copy keeps the same 99-row ranges. If you forget the $ signs, the ranges shift down by one row for each row you fill. Their sizes stay equal, but both slide away from the data. The second copy would use E3:E101 and A3:A101, then E4:E102 and A4:A102. These ranges still match each other, so Sheets raises no error. The results are wrong because the ranges no longer cover the full table.
If only some of the references are anchored, the ranges slide apart and you get the silent misalignment described above. Anchor all of the ranges, or none, depending on whether the table should move with the formula.
Making ranges resilient as data grows
If you add rows below row 100, a fixed range like A2:A100 ignores them. This is a different kind of silent error. Two ways to handle it:
- Use open-ended ranges for every range in the formula:
=SUMIFS(E2:E, A2:A, "West"). - Put the formula on a different sheet from the data and refer to whole columns, skipping the header with a criterion if needed.
Open-ended ranges keep both ranges equal because both run to the last row of the sheet. Do not mix them with bounded ranges.
Summary of checks
Before you trust a conditional aggregation formula, check that:
- Every range has the same
ROWSand the sameCOLUMNS. - Every range starts and ends on the same row numbers (when the ranges are in columns of the same table).
- Anchoring (
$) is applied consistently to all ranges. - The range covers all the data now and, if the table grows, in the future.
Exercise
A colleague's formula =COUNTIFS(A2:A100, "West", D2:D99, ">=10") returns #VALUE!. Explain why, and give the corrected formula.
Show answer
The first criteria range, A2:A100, has 99 rows. The second, D2:D99, has 98 rows. COUNTIFS requires all criteria ranges to have the same number of rows and columns, so it returns an error. Corrected formula: =COUNTIFS(A2:A100, "West", D2:D100, ">=10").
Exercise
The formula =SUMIFS(E3:E101, A2:A100, "West") returns a number with no error. Why is the number wrong, and how would you confirm it?
Show answer
Both ranges are 99 rows by 1 column, so the sizes match and no error appears. But the ranges start on different rows, so each revenue value is paired with the region from the row above it. Fix it by using E2:E100 with A2:A100. To confirm, filter the table to "West" and compare the sum of the revenue column shown in the bottom-right corner with the formula's result.
Exercise
Write a single formula that returns TRUE when the ranges B2:B200 and F2:F200 have the same number of rows and the same number of columns.
Show answer
=AND(ROWS(B2:B200)=ROWS(F2:F200), COLUMNS(B2:B200)=COLUMNS(F2:F200))