Function Problems

How to Troubleshoot Thunks with SCAN and LAMBDA in Excel

Maira MehtabMaira Mehtab Nov 10, 2026 869 views

Question details

The user needs to correctly implement and troubleshoot Excel SCAN formulas that utilize thunks to pass multiple state variables through repeated LAMBDA iterations.

How to Troubleshoot Thunks with SCAN and LAMBDA in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Writing complex dynamic array formulas where multiple variables must be maintained and updated across each loop of an iterative function.
Observed behavior
Errors occur when carrying multiple states through iterations, often triggered by invalid LET variable names, overly complex array constructions, or returning intermediate states when only the final state is desired.
Before you start

Ensure you are using a version of Microsoft Excel that supports Dynamic Arrays and the LAMBDA, SCAN, and REDUCE functions, such as Microsoft 365.

Solution 1Recommended

Use Standard Callable LAMBDAs for Thunks

Wrap the state in a callable LAMBDA and evaluate it inside the SCAN function to safely pass variables between iterations.

A thunk delays calculation by encapsulating values in a parameter-less LAMBDA. This allows SCAN to carry complex arrays or multiple states without breaking.

1
Wrap the state

Create a LAMBDA function to encapsulate your multiple state variables so they can be passed as a single object.

2
Unwrap inside SCAN

Within your SCAN iteration, call the previous thunk (e.g., previousLambda()) to unpack the current states.

3
Calculate the next state

Perform your mathematical or logical operations on the unpacked variables to determine the new values.

4
Avoid invalid LET names

When defining variables with LET, never use names that match cell references (like x1 or A1). Use distinct names like _x1 or varA.

5
Return a new thunk

Re-wrap the newly calculated state variables into a new callable LAMBDA and return it for the next iteration.

Use Standard Callable LAMBDAs for Thunks
SCAN vs REDUCE: If you only need the final accumulated state rather than an array of all intermediate steps, replace SCAN with REDUCE to simplify the output and save memory.

Master Advanced Data Analysis with WPS Spreadsheet

WPS Office offers a powerful, free Spreadsheet application that natively supports advanced data analysis, dynamic arrays, and complex formulas. You can seamlessly open, edit, and troubleshoot your complex workbooks.

  1. 1. Download and Install: Get WPS Office Free from the official website and install it on your computer.
  2. 2. Open your Workbook: Launch WPS Spreadsheet and open the Excel file containing your complex LAMBDA and SCAN iterations.
  3. 3. Edit the Formula: Select the cell with your thunk logic and use the Formula Bar to easily edit, format, and audit the code.
  4. 4. Debug Variables: Use the built-in error checking tools to identify reference conflicts or invalid variable names within your LET statements.
Fully compatible with Microsoft Excel .xlsx and .xls formatsSupports advanced array formulas and nested function logicFree, lightweight, and fast processing for large datasetsCross-platform support for Windows, Mac, and Linux
microsoft office alternative - wps office

Frequently Asked Questions

What is a thunk in Excel formulas?

A thunk is a parameter-less LAMBDA function used to encapsulate and delay the evaluation of a value or expression. This allows you to pass complex arrays or multiple state variables through iterative functions like SCAN and REDUCE without triggering immediate calculation errors.

Why do my LET variable names cause #NAME? errors?

If you use variable names in the LET function that match standard Excel cell references (such as A1, X1, or FD1), the calculation engine interprets them as conflicting cell references rather than custom variables. Always use distinct prefixes, like _x or var1.

Should I use SCAN or REDUCE for my LAMBDA iterations?

Use SCAN if you need to output an array showing the intermediate results of every step in the iteration. If you only care about the final accumulated state at the very end of the loop, use the REDUCE function to save memory and simplify your output.

How do I extract values from a CHOOSE-based LAMBDA?

By passing an index number to the CHOOSE-based LAMBDA (for example, myLambda(2)), the internal CHOOSE function evaluates the index and returns the corresponding variable stored at that specific position.