Using conditional logic to return different cell ranges in Google Sheets

I’m trying to create a formula that returns different cell ranges depending on what value is in a specific cell. Right now I have something like =SWITCH(B2,1,C10:F14,2,G10:J14) but I keep getting an error message saying the ranges don’t match the expected size. The error mentions it expects 1 row and 1 column but gets 5 rows and 4 columns instead.

I’ve also experimented with using ARRAYFORMULA and INDIRECT functions but they give me the same problem. Even when I switched to using IFS instead of SWITCH, I still get the same range size error.

Is there a way to make a formula that can dynamically select and return entire ranges based on a condition? I need to be able to pull different blocks of cells depending on the trigger value.

The Problem:

You’re trying to create a Google Sheets formula that returns different cell ranges based on the value in a specific cell. Your current attempts using SWITCH, ARRAYFORMULA, and INDIRECT result in a range size mismatch error because Google Sheets formulas cannot directly return different-sized ranges conditionally. The error message indicates that the formula expects a single cell (1 row, 1 column) but receives a multi-cell range (e.g., 5 rows and 4 columns).

:thinking: Understanding the “Why” (The Root Cause):

Google Sheets formulas evaluate all possible outcomes of a conditional statement before determining which branch to execute. Even if only one branch will ultimately be used, Sheets checks the dimensions of all potential return values. Since your SWITCH statement (or IFS) returns ranges of different sizes, Sheets throws the error because it can’t determine a consistent output size beforehand. This is a fundamental limitation of how Google Sheets handles formulas; it can’t dynamically adjust the output size based on the conditional result in the way you’re attempting.

:gear: Step-by-Step Guide:

The most reliable solution is to avoid trying to return ranges directly within a formula. Instead, use a helper cell or script to handle the dynamic range selection and then use the result to reference the correct range. Here’s how:

Step 1: Implement the Dynamic Range Selection using a Helper Cell:

  1. Choose a helper cell (e.g., A1).

  2. In this cell, create a formula to select the appropriate range name based on the value in B2: =IF(B2=1,"C10:F14",IF(B2=2,"G10:J14","")) This formula will return the address of the desired range as text (“C10:F14” or “G10:J14”). Extend this IF statement to handle more conditions as needed.

  3. In the cell where you need the data, use the INDIRECT function to reference the range returned by your helper cell: =INDIRECT(A1) This dynamically references the range whose address is in A1.

Step 2: (Optional) Handle different output sizes:

Since your ranges are of different sizes you’ll need to make sure that the output area for =INDIRECT(A1) is large enough to accomodate the largest potential range. Alternatively, you may want to consider normalizing your data so that all ranges have the same dimensions, padding smaller ones with empty cells.

:mag: Common Pitfalls & What to Check Next:

  • Helper Cell Location: Ensure your helper cell (A1 in the example) is not accidentally overwritten or moved, as this will break your INDIRECT formula.
  • Range Names: Double-check the accuracy of the range addresses (“C10:F14”, “G10:J14”, etc.) in your IF statement. Typos in these strings will cause errors.
  • Formula Errors: After implementing the solution, thoroughly test it by changing the value in cell B2 to ensure the correct range is returned and that it doesn’t overwrite other data. Consider adding error handling to your formula using IFERROR to gracefully manage potential errors.
  • Alternative: Google Apps Script: For very complex scenarios or large numbers of conditional ranges, consider writing a custom Google Apps Script function to handle the range selection. This gives you more flexibility but requires some programming knowledge.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

You’re hitting a core limitation in Google Sheets - it can’t handle conditional range returns the way you’d expect. I ran into this same issue building financial models and found that INDIRECT works way better than SWITCH here. Try =INDIRECT(IF(B2=1,"C10:F14",IF(B2=2,"G10:J14",""))) instead. INDIRECT evaluates the text string as a range reference after the IF statement picks which range to use. Another trick that saved me tons of time: create a helper column that builds the range addresses based on your condition, then use INDIRECT to reference that. Makes debugging so much easier when you add more conditions later. Just make sure your output area is big enough for the largest range you might return - you’ll still get size errors otherwise, even with the right formula.

Try CHOOSE instead of SWITCH - it handles ranges better. Also check that your destination cell has room for the largest range you’re returning. Had the same problem last week and CHOOSE worked when SWITCH failed. Worth trying before automating it.

Google Sheets throws that range size error because it expects every branch in SWITCH to return identical dimensions. Even though only one branch executes, Sheets checks all possible outputs upfront.

I hit this same wall building a project tracker where departments had different data layouts. My fix was using QUERY with INDIRECT: =QUERY(INDIRECT("C10:F14"),"SELECT *") for your first range, then wrap the whole thing in your conditional.

What worked even better was creating a staging area. I normalized all possible ranges to match the largest one - just pad smaller ranges with empty columns. Then SWITCH stops complaining about mismatched sizes.

Basically, Sheets needs to know output dimensions before it evaluates your condition. That’s why it fails even though logically it should work fine.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.