REPEATBYNUMBER

=REPEATBYNUMBER(range)

range
Required. The range (or table) has the first column as the item name and the last column as the repeat number.

REPEATBYNUMBER takes a range (or table) that has the first column as the item name and the last column as the repeat number then returns a new column of rows with the item names repeated by the required number of times.

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
2,656
REPEATBYNUMBER function takes a range (or table) that has the first column as the item name and the last column as the repeat number then returns a new column of rows with the item names repeated by the required number of times. (I learned the main idea from Karina Adcock, and used the TAKE function to make it work with a single range/table parameter).

EDIT: This function assumes that each product in the source data exists in the inventory (Repeat >= 1). For an alternative implementation that considers Repeat = 0 please see @Xlambda's RPTBYNR function below.

Excel Formula:
=LAMBDA(data,
    XLOOKUP(
        SEQUENCE(SUM(TAKE(data,,-1))),
        VSTACK(1, SCAN(1,TAKE(data,,-1),LAMBDA(a,b,a+b))),
        VSTACK(TAKE(data,,1),""),
        ,
        -1
    )
)
RepeatByNumber
ABCDEF
1ProductRepeatResult (range param)Result (table param)
2Apple5AppleApple
3Banana4AppleApple
4Cherry2AppleApple
5Strawberry4AppleApple
6AppleApple
7BananaBanana
8BananaBanana
9BananaBanana
10BananaBanana
11CherryCherry
12CherryCherry
13StrawberryStrawberry
14StrawberryStrawberry
15StrawberryStrawberry
16StrawberryStrawberry
Sheet4
Cell Formulas
RangeFormula
D2:D16D2=REPEATBYNUMBER(A2:B5)
F2:F16F2=REPEATBYNUMBER(ProductTable)
Dynamic array formulas.
 
Last edited:
Upvote 1
Thanks for posting the alternative function, @st001.

(Note: I edited your post to use XLS code tags to allow readers copy and paste the function easily)
 
RWSRPT(ar) Rows Repeat function, no need of SCAN, REDUCE, VSTACK, or XLOOKUP
Excel Formula:
=LAMBDA(ar,
    LET(
        a, DROP(ar, , -1),
        n, TAKE(ar, , -1),
        INDEX(
            a,
            TOCOL(IF(n >= SEQUENCE(, MAX(n)), SEQUENCE(ROWS(a)), NA()), 2),
            SEQUENCE(, COLUMNS(a))
        )
    )
)
repeat product names.xlsx
ABCDEFGHIJ
1
2ar=RWSRPT(B3:E6)
3aei3aei
4bfj5aei
5cgk4aei
6dhl2bfj
7bfj
8bfj
9bfj
10bfj
11cgk
12cgk
13cgk
14cgk
15dhl
16dhl
17
18
19
20ar=RWSRPT(B21:E24)
21aei3aei
22bfj5aei
23cgk0aei
24dhl2bfj
25bfj
26bfj
27bfj
28bfj
29dhl
30dhl
31
RWSRPT
Cell Formulas
RangeFormula
G2,G20G2=FORMULATEXT(G3)
G3:I16,G21:I30G3=RWSRPT(B3:E6)
Dynamic array formulas.
 
RWSRPT(ar) Rows Repeat function, no need of SCAN, REDUCE, VSTACK, or XLOOKUP
Excel Formula:
=LAMBDA(ar,
    LET(
        a, DROP(ar, , -1),
        n, TAKE(ar, , -1),
        INDEX(
            a,
            TOCOL(IF(n >= SEQUENCE(, MAX(n)), SEQUENCE(ROWS(a)), NA()), 2),
            SEQUENCE(, COLUMNS(a))
        )
    )
)
repeat product names.xlsx
ABCDEFGHIJ
1
2ar=RWSRPT(B3:E6)
3aei3aei
4bfj5aei
5cgk4aei
6dhl2bfj
7bfj
8bfj
9bfj
10bfj
11cgk
12cgk
13cgk
14cgk
15dhl
16dhl
17
18
19
20ar=RWSRPT(B21:E24)
21aei3aei
22bfj5aei
23cgk0aei
24dhl2bfj
25bfj
26bfj
27bfj
28bfj
29dhl
30dhl
31
RWSRPT
Cell Formulas
RangeFormula
G2,G20G2=FORMULATEXT(G3)
G3:I16,G21:I30G3=RWSRPT(B3:E6)
Dynamic array formulas.

Very impressive!

But the other one doesn't use TOCOL, MAX, SEQUENCE, TAKE, ROWS, COLUMNS, or INDEX... : )

Not sure which is faster.
 
But I actually prefer your new method. Its very elegant!
 
RWSRPT(ar) Rows Repeat function, no need of SCAN, REDUCE, VSTACK, or XLOOKUP
Excel Formula:
=LAMBDA(ar,
    LET(
        a, DROP(ar, , -1),
        n, TAKE(ar, , -1),
        INDEX(
            a,
            TOCOL(IF(n >= SEQUENCE(, MAX(n)), SEQUENCE(ROWS(a)), NA()), 2),
            SEQUENCE(, COLUMNS(a))
        )
    )
)
@Xlambda: Thanks for sharing.

Honestly, took me 15 minutes to understand it. After that, I can say this is beyond beautiful to me. Math, algorithm, and functions used in this short formula. Really good one!
 
But I actually prefer your new method. Its very elegant!
Thanks a lot!! 🙏✌️
I love iterations, recursive or with REDUCE, but I only use them when there is no other alternative.
So, I've tested both functions, neck to neck.
Here are the results:
repeat product names.xlsx
ABCDEFGHIJKLMNOP
1 =RANDARRAY(5000,4,1,5,1)calc time 35.36 seccalc time: instant
2above formula, c-p as values
3down to 5000 rows=AROWREPEAT(B4:E5003)=RWSRPT(B4:E5003)check rows
44514451451source ar
54234451451=ROWS(B4:E5003)
643134514515000
73313451451
81122423423result arrays
93344423423=ROWS(G4#)
10411142342315118
112213423423
123333431431=ROWS(K4#)
13442343143115118
144511431431
153221331331
163225331331
175224331331
184344112112
193414112112
202514334334
214341334334
225111334334
235541334334
245242411411
253522221221
262122221221
275425221221
284343333333
291452333333
302543333333
313111442442
321514442442
332455442442
341522451451
352415322322
365325322322
371431322322
381552322322
393452322322
402441322322
414115522522
424524522522
434541522522
441443522522
453352434434
465314434434
471423434434
483214434434
494512341341
501555341341
514424341341
525214341341
531555251251
545434251251
551231251251
563441251251
573543434434
581334511511
595153554554
604454524524
612422524524
625124352352
635235352352
641522212212
655254212212
661332542542
674421542542
684451542542
693415542542
705155542542
Sheet10
Cell Formulas
RangeFormula
G3,K3,O12,O9,O5G3=FORMULATEXT(G4)
G4:I15121G4=AROWREPEAT(B4:E5003)
K4:M15121K4=RWSRPT(B4:E5003)
O6O6=ROWS(B4:E5003)
O10O10=ROWS(G4#)
O13O13=ROWS(K4#)
Dynamic array formulas.
 
@Xlambda: Thanks for sharing.

Honestly, took me 15 minutes to understand it. After that, I can say this is beyond beautiful to me. Math, algorithm, and functions used in this short formula. Really good one!
Thank you!! Glad you liked it!!✌️🙏
 
Thanks Xlambda! And thanks for doing this testing!

Lesson learnt!
The simpler the better, and it's worth doing some tests to see what the most optimum is. Marco and Alberto do this a lot in the 'DAX' world.
I wonder if Microsoft plan on fixing the XLOOKUP bug, and giving us even more functions... : )
 
Very cool. I have LET and LAMBDA, but don't have VSTACK, TOCOL, TAKE, or DROP.

Was just curious as to how this all was working. Was able to make it work with the functions I have available, even though it would be an ugly LAMBDA function.

But, it was a good learning experience and very cool to see how you did it. I'm still trying to get in the habit of using XLOOKUP, and didn't realize how cool of a function it really is. The -1 for the match_mode parameter is sweet.

Anyway, this was how I made it work.

Book1 (version 1).xlsb
ABCD
1ProductRepeatApple
2Apple5Apple
3Banana4Apple
4Cherry2Apple
5Strawberry4Apple
6Banana
7Banana
8Banana
9Banana
10Cherry
11Cherry
12Strawberry
13Strawberry
14Strawberry
15Strawberry
Sheet2
Cell Formulas
RangeFormula
D1:D15D1=LET(s,SEQUENCE(SUM(B2:B5)),XLOOKUP(s,SCAN(1,B1:B5,LAMBDA(a,b,IF(ISNUMBER(b),a+b,1))),A2:A6,,-1))
Dynamic array formulas.
 
Actually, I thought Power Query will complete the series, but I think it won't be fair to Excel Custom Functions. This can be easily tested in ScriptLab.
(It is very easy to deal with arrays in JavaScript.)

JavaScript:
/**
 * @customfunction
 * @param {any[][]} rng
 * @return {any[][]}
 */
function ExploreCF(rng) {
  var result = [];
  rng.forEach((cols) => {
    for (var i = 1; i <= cols[1] ; i++) {
      result.push([cols[0]]);
    }
  });
  return result;
}
 

Forum statistics

Threads
1,223,604
Messages
6,173,312
Members
452,510
Latest member
RCan29

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