How to Use Excel LAMBDA to Replace an Element in an Array
Question details
The user needs an Excel LAMBDA formula to replace a specific value in one array with a corresponding value from a second array, ensuring the output retains the original array's horizontal or vertical orientation.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Creating advanced dynamic array formulas to conditionally replace data elements based on their index position.
- Observed behavior
- Standard array manipulation or incorrect MAKEARRAY formulas cause the result to lose its vertical orientation (defaulting to horizontal) or mistakenly repeat the first element across all rows.
Ensure you are using Microsoft 365 or Excel for the Web, as the LAMBDA function is not supported in older standalone desktop versions of Excel.
Create a Custom LAMBDA Formula to Swap Array Elements
Define a dynamic LAMBDA formula that tests the position of each element against a target index and returns the replacement value while preserving the original dimensions.
To ensure the orientation remains correct regardless of whether the input array is horizontal or vertical, the formula must reference the exact rows and columns of the first array.
Navigate to the Formulas tab on the Excel ribbon and click on 'Name Manager'.
Click 'New', enter a name like 'REPLACE_AT_INDEX' in the Name field, and leave the Scope set to Workbook.
In the 'Refers to' box, enter a formula that checks the exact dimensions. For example: =LAMBDA(arr1, arr2, idx, IF(SEQUENCE(ROWS(arr1), COLUMNS(arr1))=idx, INDEX(arr2, idx), arr1)). This ensures the output matrix matches the input's dimensions.
Return to your spreadsheet and type =REPLACE_AT_INDEX(A1:A5, B1:B5, 3) to test replacing the 3rd element. The original vertical or horizontal layout will be preserved.

Try WPS Office for Seamless Spreadsheet Management
While advanced features like LAMBDA are specific to Microsoft 365, WPS Office provides a powerful, lightweight, and free alternative to Microsoft Excel for handling extensive data, dynamic arrays, and complex formulas. Experience robust format compatibility without the costly subscription fees.
- 1. Download and Install: Visit the official WPS website to download the free WPS Office suite and install it on your device.
- 2. Open Your Excel File: Launch WPS Spreadsheet and easily open your existing .xlsx files with full formatting retained.
- 3. Edit and Analyze Data: Use familiar formulas, arrays, and built-in analysis tools to process your spreadsheet data efficiently.

Frequently Asked Questions
Why does my LAMBDA formula change a vertical array to a horizontal one?
This typically happens if the function generates a new sequence based on a default 1D array. Always use both ROWS() and COLUMNS() of the original array to define the output dimensions.
Why is MAKEARRAY repeating the first element in every row?
If MAKEARRAY repeats values, it usually means the row (r) and column (c) iterators inside the LAMBDA definition aren't being correctly mapped to dynamically fetch indices from the source array.
Can I use the LAMBDA function in older versions of Excel?
No, the LAMBDA function is exclusive to Microsoft 365 subscriptions and Excel for the Web. Standalone older versions like Excel 2019 or 2016 do not support it.




