Forum Discussion
Creating a dynamic repeating sequence of arrays
Hi Excel community,
I'm trying to find a way to create a dynamic array of repeating cells. I was attempting a similar technique on Google Sheets where you can use a Rept and TextJoin to create a string of the right number of elements, and then a Split function to separate them into rows.
However I haven't been able to find a way to do this in Excel; I get stumped on the split step. Would anyone have any suggestions on how to go about this?
Here's a visual example of what I'm trying to achieve:
For reference I'm using Office 365 on a mac. Perhaps I am thinking about this problem in the wrong way. Any direction I could investigate would be really helpful.
Thanks!
ElRafaVaz33 There are lots of possibilities. Here is one:
=LET(in,F1:F6,rep,G1,s,SEQUENCE(ROWS(in)*rep,1,0),INDEX(in,QUOTIENT(s,rep)+1))
23 Replies
- Patrick2788Silver Contributor
A recursive solution with an option for directional output vertical or horizontal.
RepeatElements = LAMBDA(arr, reps, [direction], LET( vector, IF(ISOMITTED(direction), TOCOL(arr), TOROW(arr)), acc, TAKE(arr, , 1), stack, HSTACK(acc, arr), IF(reps = 1, vector, RepeatElements(stack, reps - 1, direction)) ) )
- PeterBartholomew1Silver Contributor
The attached workbook contains the basic CHOOSECOLS/TOCOL solution as well as the REDUCE/VSTACK approach. The main intention, however, was to test a function BYROWλ against the different use cases. The function description is
/* DESCRIPTION: Implements a version of BYROW that will return an array of arrays */It is used first simply to create the 4 repetition problem and then the variable repetition version. In each case the array of array problem of Excel is solved out of sight of the user.Note: The variable repetition version would appear neater were the MAP helper function generalised but I have yet to tackle that - PeterBartholomew1Silver Contributor
Sometimes the challenge is to know when to stop!
You could start with a basic kind of formula
= TOCOL(CHOOSECOLS(names, SEQUENCE(1,repeat,1,0)))
but then, by the time you have used LET to remove the nesting and describe the intermediate steps, then wrapped the formula with a named LAMBDA to describe the operation and to list its arguments, you might finish up with
Repeatλ = LET( index, SEQUENCE(1, repeat, 1, 0), block, CHOOSECOLS(names, index), TOCOL(block, , order) )
so the worksheet formulae are
= Repeatλ(names, repeat) = Repeatλ(names, repeat, 1)
to give the results
respectively.
It all depends how much effort one is willing to commit to making things 'simple'!
- SergeiBaklanDiamond Contributor
One more for the collection
=REDUCE( "Result", MAP(list, LAMBDA(v, LAMBDA(IF(SEQUENCE(n), v)))), LAMBDA(a,v, VSTACK(a, v())) )
Close to what Patrick2788 suggested.
- Patrick2788Silver Contributor
With REDUCE
list - list of names to repeat
rep - number of times to repeat each
=REDUCE("Result",list,LAMBDA(a,v,VSTACK(a,EXPAND(v,rep,,v))))
- Patrick2788Silver ContributorPresuming names in A1:A3:
=TOCOL(CHOOSECOLS(A1:A3,1,SEQUENCE(3,1,1,0)))
*TOCOL and CHOOSECOLS are available with Insider (beta channel).- ElRafaVaz33Copper ContributorThanks all for your answers! There definitely seems to be multiple ways to address this 🙂
- OliverScheurichGold Contributor
- mtarlerSilver Contributor
ElRafaVaz33 There are lots of possibilities. Here is one:
=LET(in,F1:F6,rep,G1,s,SEQUENCE(ROWS(in)*rep,1,0),INDEX(in,QUOTIENT(s,rep)+1))
- limey1475Copper Contributor
you are a total f-ing genius
- ElRafaVaz33Copper ContributorThis is perfect, thank you! I'd never heard of the LET function, but reading on it, it looks super powerful.
For anyone reading this after me, here the source list is given from F1:F6 while G1 holds the number of repetitions.- mtarlerSilver ContributorYES, LET() is SUPER awesome addition. If you don't know about it definitely learn. Also look into FILTER, SORT, UNIQUE, and then LAMBDA (which gets a little more complicated but even more powerful). Some great things happening in Excelverse
- OliverScheurichGold Contributor
Sub names_array() Dim i As Integer Dim lngzeilemax As Integer Dim z As Integer Dim y As Integer With Tabelle1 lngzeilemax = .Range("B" & .Rows.Count).End(xlUp).Row For i = 3 To lngzeilemax * Cells(1, 2).Value For z = 1 To Cells(1, 2).Value Cells(z + y, 3).Value = Cells(i, 2).Value Next z y = y + Cells(1, 2).Value Next i End With End Sub
Maybe with this code. You can click the button in cell E2 in the attached file to start the macro.