=LAMBDA( array_a, array_b, keep_duplicates, LET( arr, MAKEARRAY( ROWS(array_a)+ROWS(array_b), 1, LAMBDA(r, c, IF(r<=ROWS(array_a), INDEX(array_a,r), INDEX(array_b,r-ROWS(array_a)) ) ) ), IF(keep_duplicates,arr,UNIQUE(arr)) ) )
You may have found yourself wanting to join two arrays together in Excel. This function will quickly append and optionally deduplicate two single-column arrays or ranges of data. This is done with the LAMBDA shown above, which you can define in Excel’s Name Manager with the name ARRAYUNION.
It accepts 3 arguments:
- array_a – a single-column array or range
- array_b – a single-column array or range
- keep_duplicates – a TRUE/FALSE value indicating whether to keep or remove duplicate values after appending array_b to array_a
It couldn’t be simpler to use:
The approach uses MAKEARRAY to create an array that has ROWS(array_a)+ROWS(array_b) rows and one column.
The elements of the array to be made are provided by the LAMBDA shown starting on line 10.
The first two arguments of that LAMBDA are r and c. Within the MAKEARRAY function, these represent row and column positions in the array being made.
Here we are saying if the row of the new array is less than or equal to the number of rows in array_a, then populate that row with the value from the same position in array_a (as retrieved by the INDEX function).
If the row of the new array is greater than the number of rows in array_a, then we will populate it with a value from array_b.
Let ‘arr’ be the array created by MAKEARRAY as described above. Then, if keep_duplicates is TRUE, return ‘arr’ unmodified. Otherwise, apply the UNIQUE function to ‘arr’ and return the result of that function call. This has the effect of removing duplicates from the joined arrays. This can be particularly useful if you have two lists of people on different sheets and you think there might be some people in both lists.
Or perhaps you have two reports with lists of products your company sells and you want to quickly create a combined report or check that the right products are included.