Function Problems

How to Use Excel LAMBDA to Replace an Element in an Array

Huma Ashraf ChHuma Ashraf Ch Oct 23, 2026 870 views

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.

How to Use Excel LAMBDA to Replace an Element in an Array
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Name Manager

Navigate to the Formulas tab on the Excel ribbon and click on 'Name Manager'.

2
Define the New Function

Click 'New', enter a name like 'REPLACE_AT_INDEX' in the Name field, and leave the Scope set to Workbook.

3
Enter the LAMBDA Formula

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.

4
Test in Your Worksheet

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.

Create a Custom LAMBDA Formula to Swap Array Elements
Preserving Orientation: By explicitly checking ROWS() and COLUMNS() inside the SEQUENCE function, the LAMBDA matrix correctly maps to your original horizontal or vertical layout.
Free Microsoft Office alternative

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. 1. Download and Install: Visit the official WPS website to download the free WPS Office suite and install it on your device.
  2. 2. Open Your Excel File: Launch WPS Spreadsheet and easily open your existing .xlsx files with full formatting retained.
  3. 3. Edit and Analyze Data: Use familiar formulas, arrays, and built-in analysis tools to process your spreadsheet data efficiently.
Fully compatible with Microsoft Excel formats, including .xlsx and .csv.Supports a vast library of advanced formulas and array calculations for data analysis.Free and lightweight with a highly familiar user interface.No steep learning curve for users migrating from Microsoft Office.
QA img-9

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.