ASTACK

ASTACK(ARRAY,[Stack_Into],[By_Col])
Array
Required. Array to stack.
[Stack_Into]
Optional. Number of columns to stack/unstack into; 0 or ignored☛ 1 column.
[By_Col]
Optional. 1☛ stack/unstack column by column; 0 or ignored☛ row by row.

ASTACK stacks or unstacks an array going row by row or column by column given a number of columns.

schardt679

Board Regular
Joined
Mar 27, 2021
Messages
58
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
ASTACK stacks or unstacks an array going row by row or column by column given a number of columns.

Thanks to Rico's ARRAY2DTO1D & ARRAY1DTO2D functions and XLambda's older ASTACK & AUNSTACK functions for the inspiration.
It combines the functionality of an array stacking function and an array unstacking function.
If Stack_Into < array columns, the array is stacked. If Stack_Into > array columns, the array is unstacked
If skipping arguments, a comma is required. Calls IFBLANK, CELLCOUNT, & AINDEXERL.
Other functions on minisheet: AFORMULATEXT.

Screen Tip/ Comment: =ASTACK(Array☛ range to stack/unstack, [Stack_Into]☛ number of columns to stack/unstack into; 0/❎=1✅, [By_Col]☛ 1=stack/unstack column by column; 0/❎=row by row✅) ⁂[]=optional; ✅=default; ❎=omit

Excel Formula:
=LAMBDA(Array,[Stack_Into],[By_Col],
      LET(Arr, IFBLANK(Array,  ""),          Into, IFERROR(--(Stack_Into),),          Col?, By_Col,
             IntoScan, Into<0,          IntoMSG, "Stack_Into>=0",
             ColScan, AND(Col?<>{1,0}),          ColMSG, "By_Col=1,0",
             ERRORS, IFS(IntoScan, IntoMSG,  ColScan, ColMSG,  1, 0),
             RowSeq1, AINDEXERL(Arr,, Col?),          ColSeq1, AINDEXERL(Arr, 1, Col?),
             STACK, INDEX(Arr, RowSeq1, ColSeq1),          TotCt, CELLCOUNT(Arr),
             ColCt, MAX(Into, 1),          RowCt, ROUNDUP(TotCt / ColCt,),
             RowSeq2, SEQUENCE(RowCt, ColCt),          ColSeq2, TRANSPOSE(SEQUENCE(ColCt, RowCt)),
             Seq, IF(Col?, ColSeq2,  RowSeq2),          UNSTACK, INDEX(STACK, Seq),
             Result, IF(Seq>TotCt, "",  UNSTACK),          Return, IF(ERRORS<>0, ERRORS,  Result),
             Result
       )
  )
LAMBDA Examples.xlsx
ABCDEFGHIJKLMN
1ASTACK
2
3Original DataStack_Into:Result
4ProductSalesBy_Col:Product
5Pepper Deseeder19Sales
6Knife Set4Pepper Deseeder
7Cutting Board719
8Knife Set
9Data is stacked into 1 column4
10Data is stacked row by rowCutting Board
11Formula in cell J4☛ =ASTACK(B4:C7, H3, H4)7
12
13
14
15Original DataStack_Into:4Result
16ProductSalesBy_Col:1ProductProductSalesSales
17Pepper Deseeder19Pepper DeseederPepper Deseeder1926
18Knife Set4Knife SetKnife Set428
19Cutting Board7Cutting BoardCutting Board716
20ProductSales
21Pepper Deseeder26
22Knife Set28
23Cutting Board16
24
25Data is unstacked into 4 columns
26Data is unstacked column by column
27Formula in cell J16☛ =ASTACK(B16:C23, H15, H16)
28
29
30
31Original DataStack_Into:2Result
32Item 1.1Item 2.1Item 3.1Item 4.1By_Col:1Item 1.1Item 3.1
33Item 1.2Item 2.2Item 3.2Item 4.2Item 1.2Item 3.2
34Item 1.3Item 2.3Item 3.3Item 4.3Item 1.3Item 3.3
35Item 1.4Item 2.4Item 3.4Item 4.4Item 1.4Item 3.4
36Item 2.1Item 4.1
37Data is stacked into 2 columnsItem 2.2Item 4.2
38Data is stacked column by columnItem 2.3Item 4.3
39Formula in cell J32☛ =ASTACK(B32:E35, H31, H32)Item 2.4Item 4.4
40
41
42
43Original DataStack_Into:4Result
44Item 1.1Item 2.1By_Col:0Item 1.1Item 2.1Item 3.1Item 4.1
45Item 3.1Item 4.1Item 1.2Item 2.2Item 3.2Item 4.2
46Item 1.2Item 2.2Item 1.3Item 2.3Item 3.3Item 4.3
47Item 3.2Item 4.2Item 1.4Item 2.4Item 3.4Item 4.4
48Item 1.3Item 2.3
49Item 3.3Item 4.3
50Item 1.4Item 2.4
51Item 3.4Item 4.4
52
53Data is unstacked into 4 columns
54Data is unstacked row by row
55Formula in cell J44☛ =ASTACK(B44:C51, H43, H44)
56
ASTACK
Cell Formulas
RangeFormula
J4:J11J4=ASTACK(B4:C7, H3, H4)
B11,B39B11=AFORMULATEXT(J4)
J16:M19,J44:M47J16=ASTACK(B16:C23, H15, H16)
B27,B55B27=AFORMULATEXT(J16)
J32:K39J32=ASTACK(B32:E35, H31, H32)
Dynamic array formulas.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,575
Messages
6,173,148
Members
452,502
Latest member
PQCurious

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