COMBINEARRAYS

COMBINEARRAYS(array_1,array_2,defaultValue,combineHorizontally)
array_1
first array to combine
array_2
second array to combine
defaultValue
enter a value to display if one array is wider/taller than the other. If left blank, the function will return 0 if the first value in the smaller array is a number, otherwise "".
combineHorizontally
enter 0 or blank to stack vertically, or 1 to stack horizontally (across the way)

COMBINEARRAYS combines two 2D arrays into a stacked 2D array, horizontally or vertically, allowing for a default where one array is larger than the other.

RicoS

Board Regular
Joined
May 1, 2019
Messages
62
COMBINEARRAYS combines two 2D arrays into a stacked 2D array, horizontally or vertically, allowing for a default where one array is larger than the other

Excel Formula:
=LAMBDA(array_1,array_2,defaultValue,combineHorizontally,
   LET(
      isH,combineHorizontally,
      rArr_1,ROWS(array_1),
      rArr_2,ROWS(array_2),
      cArr_1,COLUMNS(array_1),
      cArr_2,COLUMNS(array_2),
      isLargestArr1,IF(isH,rArr_1>=rArr_2,cArr_1>=cArr_2),
      dflt,IF(ISBLANK(defaultValue),IF(ISNUMBER(INDEX(IF(isLargestArr1,array_2,array_1),1,1)),0,""),defaultValue),
      arr_1,IF(isLargestArr1,array_1,IF(isH,RESIZEARRAY(array_1,rArr_2,,dflt),RESIZEARRAY(array_1,,cArr_2,dflt))),
      arr_2,IF(isLargestArr1,IF(isH,RESIZEARRAY(array_2,rArr_1,,dflt),RESIZEARRAY(array_2,,cArr_1,dflt)),array_2),
      arrTrns_1,IF(isH,TRANSPOSE(arr_1),arr_1),
      arrTrns_2,IF(isH,TRANSPOSE(arr_2),arr_2),
      rArrT_1,ROWS(arrTrns_1),
      rArrT_2,ROWS(arrTrns_2),
      cArrT,COLUMNS(arrTrns_1),
      seqR,SEQUENCE(rArrT_1+rArrT_2),
      seqC,SEQUENCE(,cArrT),
      combined,IF(seqR<=rArrT_1,INDEX(arrTrns_1,seqR,seqC),INDEX(arrTrns_2,seqR-rArrT_1,seqC)),
      return,IF(isH,TRANSPOSE(combined),combined),
   return)
)

Lambda - Last Cell and Split.xlsx
BCDEFGHIJKL
1Original Array 1Original Array 2
2123def
3abc456
4@£$
5
6
7Vertical arrayHorizontal array
8123123def
9abcabc456
10@£$@£$
11def
12456
13
Sheet1
Cell Formulas
RangeFormula
B8:D12B8=COMBINEARRAYS(B2:D4,F2:H3,,)
F8:K10F8=COMBINEARRAYS(B2:D4,F2:H3,,1)
Dynamic array formulas.


Utilises LAMBDA Functions:
RESIZEARRAY
 
Upvote 0
If some moderator person could switch the "defaultValue,combineHorizontally" in the function header description, I'd be eternally* grateful. I had a last minute change of heart, as I thought the default value is more likely to be used than the horizontal option, as arrays will most likely be combined vertically, but could often be of varying column sizes. Thanks!



*eternally offer expires Sunday
 
If some moderator person could switch the "defaultValue,combineHorizontally" in the function header description, I'd be eternally* grateful. I had a last minute change of heart, as I thought the default value is more likely to be used than the horizontal option, as arrays will most likely be combined vertically, but could often be of varying column sizes. Thanks!
@RicoS - I edited the description.

(Could you let me know what the bottom note in your message (post#2) means? "*eternally offer expires Sunday")
 
Thanks! It was just a little joke. I was offering you my eternal gratitude with "until Sunday" in the small print (so not eternal at all). Interestingly, if you were to read that comment recursively, it gets less and less funny each time until it turns out not to be funny at all.
 
Ah! So the question is, until which Sunday? :)

I am going to remove these extra posts from this thread to keep it as a Lambda function.
Just FYI: In the future, if you need a correction in a post, a vital correction like this one, and 10 minutes editing time is over, then you can click on the Report link at the bottom of the post, and explain what's wrong with the post.
 

Forum statistics

Threads
1,224,829
Messages
6,181,224
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top