Last active
February 21, 2026 04:25
-
-
Save ncalm/a65c6169d14ffc1978d5b6126f199c95 to your computer and use it in GitHub Desktop.
REPEAT function for Excel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| REPEAT = LAMBDA(table,row_repeats,[has_header], | |
| LET( | |
| // if has_header is zero, FALSE or is omitted, then FALSE, otherwise TRUE | |
| // long-hand for clarity! | |
| _has_header,IF(OR(has_header=0,ISOMITTED(has_header),has_header=FALSE),FALSE,TRUE), | |
| _header,IF(_has_header,TAKE(table,1),NA()), // get the header if it's there | |
| _table,IF(_has_header,DROP(table,1),table), // redefine the table based on header presence | |
| _reps,NUMBERVALUE(row_repeats), | |
| _total,SUM(_reps), | |
| IF( | |
| OR( | |
| MIN(_reps)<0, // negatives not allowed | |
| _total=0, // all zero not allowed | |
| ROWS(_table)<>ROWS(_reps) // must be same size as table | |
| ), | |
| #VALUE!, // return an error if any of the above conditions are true | |
| LET( | |
| // create an index array to pull rows multiple times from the input table | |
| _data,INDEX( | |
| _table, | |
| XMATCH(SEQUENCE(_total),SCAN(,_reps,SUM),1), | |
| SEQUENCE(,COLUMNS(_table)) | |
| ), | |
| // stack the header on top... or not | |
| IF(_has_header,VSTACK(_header,_data),_data) | |
| ) | |
| ) | |
| ) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment