Consolidating non-contiguous data ranges in Excel often forces analysts into a frustrating struggle with bloated, error-prone formulas. While standard consolidation tools work well when evaluating traditional reports from standard funding sources, they fail to handle highly fragmented layouts. Fortunately, custom LAMBDA functions grant immediate, dynamic scalability, completely bypassing the need for legacy VBA workarounds.
An important educational stipulation is that this advanced approach requires Excel 365 compatibility to support dynamic arrays. By leveraging concrete examples-such as nesting CHOOSECOLS within a recursive REDUCE function-you can elegantly unify disparate tables. Below, we will dissect the exact formula construction and steps to implement this solution.
Excel has undergone a massive evolution over the last few years. The introduction of dynamic arrays, helper functions like MAP, REDUCE, and BYROW, and the revolutionary LAMBDA function have turned Excel's formula engine into a turing-complete programming language. However, even with these modern enhancements, a classic Excel limitation continues to plague advanced modelers: handling non-contiguous (disjointed) ranges.
Standard legacy functions like SUM or AVERAGE can natively handle non-contiguous selections-for example, =SUM(A2:A10, C2:C10, E2:E10) works flawlessly. But if you try to pass these non-contiguous ranges into modern array-processing functions or attempt to write a custom LAMBDA that loops through them as a single cohesive dataset, Excel will typically return a #VALUE! or parameter mismatch error. This article explores how to overcome this limitation by building custom, reusable LAMBDA functions that aggregate non-contiguous ranges seamlessly without requiring helper columns or VBA.
In Excel's traditional terminology, a non-contiguous range selection (like A2:A10, C2:C10) is treated as a Range Reference with Multiple Areas. Excel's calculation engine treats these "Areas" differently than single, contiguous arrays.
When you pass a reference like (A2:A10, C2:C10) to a dynamic array function like MAP, the function expects a single, continuous array or grid. It does not know how to automatically jump from the end of the first area to the beginning of the second. To make these ranges compatible with modern custom logic, we must first unify them into a single, logical virtual array inside our formula memory space.
The simplest and most performant way to unify disjointed ranges in modern Excel is by utilizing the array shaping functions: VSTACK (Vertical Stack) and HSTACK (Horizontal Stack). These functions take multiple disparate ranges and align them into a single, continuous virtual array.
Consider a scenario where you want to calculate the median of three disconnected columns: B2:B10, D2:D10, and F2:F10, but only for values greater than zero. Normally, MEDIAN doesn't allow a nested IF across disjointed ranges. By using VSTACK, we can create a single array and filter it:
=LET(
UnifiedArray, VSTACK(B2:B10, D2:D10, F2:F10),
FilteredArray, FILTER(UnifiedArray, UnifiedArray > 0),
MEDIAN(FilteredArray)
)
This works beautifully for simple formulas. But what if you need to perform this complex aggregation repeatedly across different sheets or workbooks? Writing out this verbose LET statement every time is tedious and error-prone. This is where custom LAMBDA functions come into play.
AGGREGATE_DISJOINTWe can construct a reusable LAMBDA function that accepts an arbitrary number of ranges, combines them, filters out unwanted values (like zeros, errors, or blanks), and applies a custom aggregation logic. Let's design a custom function called AGGREGATE_DISJOINT.
To define this custom function, we will use Excel's Name Manager. Here is the formula code we will register:
=LAMBDA(Range1, Range2, Range3, Aggregator,
LET(
Combined, VSTACK(Range1, Range2, Range3),
Cleaned, FILTER(Combined, ISNUMBER(Combined)),
Aggregator(Cleaned)
)
)
Ctrl + F3 (or go to the Formulas tab and click Name Manager).AGGREGATE_DISJOINT.LAMBDA formula shown above.Now, you can use this function anywhere in your workbook like a native Excel function. For instance, to calculate the standard deviation of three disjointed columns, you can pass a standard lambda helper function as the Aggregator parameter:
=AGGREGATE_DISJOINT(A2:A10, C2:C10, E2:E10, LAMBDA(x, STDEV.S(x)))
The limitation of the previous AGGREGATE_DISJOINT function is that it has a fixed number of inputs (three ranges). If you have four ranges, or only two, the formula will throw an error or require rewriting. To build a truly robust utility, we need a way to pass an arbitrary, dynamic list of ranges.
Since Excel's LAMBDA does not support parameter arrays (optional infinite arguments like ...args in JavaScript) natively in a clean way yet, we can work around this by passing our range addresses as an array of text strings, and then resolving them using the INDIRECT function inside a recursive or looping REDUCE pattern.
SUM_NONCONTIGUOUS_DYN LAMBDALet's write a function that takes an array of text-based range references (e.g., {"A2:A10", "C5:C12", "F2:F20"}), stacks their contents dynamically, and aggregates them. We'll use REDUCE to iterate through the text array and progressively stack the ranges.
=LAMBDA(RangeStrings, Aggregator,
LET(
FirstRange, INDIRECT(INDEX(RangeStrings, 1)),
StackedData, REDUCE(FirstRange, DROP(RangeStrings, 1), LAMBDA(Accumulator, CurrentValue,
VSTACK(Accumulator, INDIRECT(CurrentValue))
)),
CleanedData, FILTER(StackedData, NOT(ISERR(StackedData)) * (StackedData <> "")),
Aggregator(CleanedData)
)
)
INDEX(RangeStrings, 1): Extracts the first range address from our list of strings and uses INDIRECT to convert it into a real Excel reference to initialize our accumulator.DROP(RangeStrings, 1): Returns the rest of the range strings, skipping the first one we already processed.REDUCE(...): Loops through each subsequent range string, converts it to a live reference using INDIRECT, and vertically appends it to our growing dataset using VSTACK.FILTER(...): Cleans the compiled dataset by stripping out any calculation errors (#N/A, #VALUE!, etc.) and blank spaces.Aggregator(CleanedData): Evaluates your specified mathematical operation on the finalized, clean, unified virtual column.Let us look at a practical application. Imagine you run a retail company with data laid out in fragmented, non-contiguous tables on a single worksheet. Department A is in B4:B15, Department B is in F4:F15, and Department C is in J4:J15. You want to calculate the 90th percentile of all transactions across these departments, but only for transactions that exceeded $100.
Using our custom registered SUM_NONCONTIGUOUS_DYN function, this complex statistical analysis becomes a clean, one-line formula:
=SUM_NONCONTIGUOUS_DYN(
{"B4:B15", "F4:F15", "J4:J15"},
LAMBDA(Dataset, PERCENTILE.INC(FILTER(Dataset, Dataset > 100), 0.9))
)
This approach bypasses the mess of duplicating tables or creating temporary staging worksheets, keeping your workbook architecture clean, lean, and highly professional.
While custom LAMBDA functions coupled with INDIRECT offer immense power, it is important to understand how they impact workbook performance:
INDIRECT function is volatile. This means any change to any cell in the workbook will trigger Excel to recalculate your custom formula, even if the change occurred in a completely unrelated sheet. If you are applying this pattern to massive workbooks with tens of thousands of rows, opt for the first technique (explicitly passing ranges to VSTACK) rather than string references, as VSTACK is non-volatile.LAMBDA functions, use Microsoft's Advanced Formula Environment add-in. It provides syntax highlighting, auto-formatting, and error checking, making custom formula development significantly easier than writing in the native Name Manager dialog.The ability to aggregate non-contiguous ranges is a massive step forward for advanced financial modelers, data analysts, and everyday power users. By wrapping range-stacking logic inside custom LAMBDA functions, we bridge the gap between traditional workbook structures and modern dynamic array formulas. This approach ensures your spreadsheets remain highly readable, easily maintainable, and free of redundant helper tables.
Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.