SEQUENCES

SEQUENCES(counts,[start],[breaks],[steps],[stack],[sort])
counts
Required. Specifies the total number(s) of sequences as well as how many number(s) needed for each sequence and can be one or more integers.
start
Optional. Specifies the "start" of the first specified sequence and can be any single real number.
breaks
Optional. Specifies the numerical difference(s) between the "start" of the sequences specified in "counts" and can be one or more real numbers.
steps
Optional. Specifies the "steps" for the specified sequence(s) and can be one or more real numbers.
stack
Optional. Specifies the output format of the function and takes two general arguments: 0 or omitted or FALSE, for linear output; or 1 or TRUE or any number other than 0, for stacked output.
sort
Optional. Specifies whether the function output is sorted or not as well as the direction of sort and takes two arguments: 1, for ascending sort; or -1, for descending sort.

SEQUENCES is a powerful and versatile multi-SEQUENCE solution providing full control over "start" of each sequence as well as each of the "counts" and "steps" and having other useful features such as stacking and sorting

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
578
Office Version
  1. 365
Platform
  1. Windows
SEQUENCES is a powerful and versatile multi-SEQUENCE solution providing full control over "start" of each sequence as well as each of the "counts" and "steps" and having other useful features such as stacking and sorting

The overall function parameters are similar to the native SEQUENCE function, with the following differences:
1) "rows" and "columns" have been replaced with "counts"
2) "start" and "breaks" allow for full control over the start of each sequence
3) "step" has been enhanced to "steps" to allow full control for the step of each sequence
4) "stack" allows for changing the output from linear to stacked
5) "sort" allows for sorting even in the stacked format

Thus, SEQUENCES allows for creating multiple sequences at once with the ability to control all of their properties independently of one another. SEQUENCES also allows for incorporating blanks wherever needed as well as filtering out any zero counts (see description below)

(The code is modular (as all my other functions) allowing for easy swapping/optimizing over time. See all the modules below, after the description)


SEQUENCES takes six parameters, one required and five optional, as follows:

A) The first parameter, required, specifies the total number(s) of sequences as well as how many number(s) needed for each sequence and can be one or more integers

Note that if more than one number needs to be entered, they should be included in array brackets "{}" per native syntax

Note that the total counts of numbers specifies the number of needed sequences (e.g. 3 or {1} indicates that one sequence is needed while {4,1} indicates two sequences)

Note that each of the numbers indicates the total count of numbers for each sequence (e.g. {5,3} means that the first sequence has five numbers and the second has three)

Note that any zero(s) in this parameter are filtered out implying that the sequence(s) corresponding to the position(s) of the zero(s) will be eliminated from the output; note, however, that {4,5} will not necessarily result in the same two sequences as {4,0,5} due to possible differences between "breaks" and/or "steps" (see items C and D below)

Note that including negative number(s) in this parameter will result in sequence(s) of blanks with count(s) corresponding to the absolute value(s) of the negative number(s)

Note that entering numbers in column format (i.e. with semicolons) (e.g. {3;6;2} will result in an output which is equal to the transposed version of row format (i.e. counts with commas)

Note that the numbers entered in this parameter can be either one-dimensional arrays (either in row format or column format) or two-dimensional as the function linearizes the "counts"

Note that if anything other than numbers is entered in this parameter, the function will crash to an #N/A

B) The second parameter, optional, specifies the "start" of the first specified sequence and can be any single real number

Note that if this parameter is left omitted, the first sequence will have the default start of 1 and any subsequent sequence will start 1 higher (as long as the "breaks" parameter is left omitted; see item C below)

Note that if more than one number is entered, the function will crash to an #N/A

Note that if anything other than a number is entered, the function will crash to an #N/A

C) The third parameter, optional, specifies the numerical difference(s) between the start of the sequences specified in the "counts" parameter and can be one or more real numbers

Note that if more than one number needs to be entered, they should be included in array brackets "{}" per native syntax

Note that if this parameter is left omitted, the default break of one will be automatically applied to all sequences following the first one (i.e. the start of each sequence will be 1 higher than the previous)

Note that if a single number is entered, that break will be applied to all sequences following the first one

Note, that if more than one number is entered, they will be applied to the numbers in "counts" respectively; if the numbers of "breaks" is fewer than that of "counts", the default break of 1 will be automatically assigned to the remaining numbers of "counts"

Note that if there are any additional numbers in "breaks", past the last point of correspondence to "counts", they will be ignored by the function; thus, if more numbers are later added to "counts", the additional numbers in "breaks" will be automatically applied to those added numbers

Note that if anything other than numbers is entered in this parameter, the function will crash to an #N/A

D) The fourth parameter, optional, specifies the "steps" for the specified sequence(s) and can be one or more real numbers

Note that if more than one number needs to be entered, they should be included in array brackets "{}" per native syntax

Note that if this parameter is left omitted, the default step of 1 will be applied to all sequences

Note that if a single number is entered, this number will be applied as step to all sequences

Note, that if more than one number is entered, they will be applied to the numbers in "counts" respectively; if the numbers of "steps" is fewer than that of "counts", the default step of 1 will be automatically assigned to the remaining numbers of "counts"

Note that if there are any additional numbers in "steps", past the last point of correspondence to "counts", they will be ignored by the function; thus, if more numbers are later added to "counts", the additional numbers in "steps" will be automatically applied to those added numbers

Note that if anything other than numbers is entered in this parameter, the function will crash to an #N/A

E) The fifth parameter, optional, specifies the output format of the function and takes two general arguments:
0 or omitted or FALSE, for linear output
1 or TRUE or any number other than 0, for stacked output

Note that besides being omitted, only one real number or TRUE/FALSE can be entered in this parameter; entering more than one number or logical will cause the function to crash to an #N/A

Note that if anything other than the specified allowed arguments is entered, the function will crash to an #N/A

F) The sixth parameter, optional, specifies whether the function output is sorted or not as well as the direction of sort and takes the same two arguments of the native SORT function:
1, for ascending sort
-1, for descending sort

Note that if the function output is linear, the entire elements of the output will be subjected to the specified sort, while if the function output is stacked, each column (or row, in case of column "counts") will be subjected to the specified sort independently


This function is mostly based on the following two threads:

Special thanks to Stephen (@StephenCrump) and @djclements for making critical elements of this function possible

(You can download my template from the link in my signature below, which makes it easier to obtain/test the functions compared to manually pasting from the text/XL2BB)


Note that in the function code, the last IF is not strictly needed for the core functionality; it is there just to ensure the correct arguments are entered in all parameters. Thus, the function will work perfectly fine with the last variable being "h".

SEQUENCES
Excel Formula:
=LAMBDA(counts,[start],[breaks],[steps],[stack],[sort],
   LET(
      v,TOROW(counts),
      b,ABS(v),
      c,TOROW(IF(IO(breaks),1,breaks)),
      a,COUNT(b),
      j,COUNT(c),
      u,TOROW(steps),
      e,COUNT(u),
      g,IF(OR(IO(steps),e>1),1,INDEX(u,1)),
      q,ABS(IF(g,g,1)),
      o,(
         IF(v<0,"",IF(ISERROR(b+u),1,u))-1)*
         SEQUENCE(MAX(b),,0,IF(e>1,1,0)
      ),
      l,IF(IO(stack),0,INDEX(stack,1)),
      n,IF(IO(sort),0,INDEX(sort,1)),
      m,SEQUENCE(MAX(b),,,q),
      k,IFS(
         m<=(b*q),
         IF(g<=0,SEQUENCE(MAX(b),,,g),m)+
            IF(
               j=1,
               SEQUENCE(,COUNT(b),0,INDEX(c,1)),
               LET(
                  r,SCAN(0,c,LAMBDA(y,x,y+x)),
                  t,a-COUNT(r),
                  HSTACK(0,r,SEQUENCE(,IF(t>0,t,1),MAX(r)+1,1))
               )
            )
      ),
      p,CHOOSECOLS(k+o,FILTER(SEQUENCE(,a),b<>0)),
      d,IF(
         l,
         p,
         TOROW(IF(ISERROR(p),IF(ERROR.TYPE(p)=3,"",p),p),2,1)
      ),
      i,d+(IF(IO(start),0,start-1)),
      w,IF(
         n,
         IF(
            l,
            IFNA(
               WRAPCOLS(
                  SORTBY(TOCOL(i),
                     TOCOL(IF({1},SEQUENCE(,COLUMNS(i)),i)),1,
                     TOCOL(ISNA(i)),1,
                     TOCOL(i),n
                  ),
                  ROWS(i)
               ),
               ""
            ),
            IF(
               PRODUCT((counts>=0)*1),
               SORT(i,,n,1),
               IFNA(
                  TOROW(LET(
                     za,TRANSPOSE(i),
                     zb,ROWS(za),
                     zc,SEQUENCE(zb),
                     zd,ISERROR(za),
                     ze,NOT(zd),
                     zf,TOCOL(za,2,1),
                     zg,TOCOL(IFS(ze,1),2,1),
                     zh,SORTBY(EXPAND(SORTBY(zf,zg,1,zf,n),zb*1),
                        VSTACK(zg,TOCOL(IFS(zd,1),2,1)),1,
                        VSTACK(TOCOL(IFS(ze,zc),2,1),TOCOL(IFS(zd,zc),2,1)),1
                     ),
                     WRAPCOLS(zh,zb))
                  ),
                  ""
               )
            )
         ),
         IFNA(i,"")
      ),
      f,IF(ISERROR(w),IF(ERROR.TYPE(w)=3,"",w),w),
      h,IF(COLUMNS(counts)>=ROWS(counts),f,TRANSPOSE(f)),
      IF(
         OR(IO(breaks),ISNUMBERS(breaks))*
            (COUNT(start)<=1)*
            OR(ISNUMBER(start),IO(start))*
            ISNUMBERS(steps,TRUE)*
            (COUNT(l)<=1)*
            ISNUMBER(l)*
            ISNUMBERS(counts,TRUE)*
            ISNUMBERS(sort,TRUE)*
            OR(IO(sort),sort=1,sort=-1)*
            OR(IO(stack),COUNT(stack)=1),
         h,
         NA()
      )
   )
)

ISNUMBERS (Returns TRUE if ALL elements in the reference are numbers and FALSE otherwise)
Excel Formula:
=LAMBDA(reference,[if_omitted],
   IF(
      IO(reference),
      IF(IO(if_omitted),FALSE,if_omitted),
      IF(PRODUCT(ISNUMBER(reference)*1),TRUE,FALSE)
   )
)

IO (shortened form of ISOMITTED)
Excel Formula:
=LAMBDA(parameter,
   ISOMITTED(parameter)
)


SEQUENCES.xlsx
ABCDEFGHIJKLMNOPQRSTUV
1
2#CALC!
3
412234545678
5
61234512234344567
7234612344567
845712344567
95876544321
1012344567
1111276544321
1222345
132345
1434
1545678
165
173
184
195
204
215
226
237
248
25
26start:omittedstart:2.2start:-3.5
271234562.23.24.25.26.27.2-3.5-2.5-1.5-0.50.51.5
282345673.24.25.26.27.28.2-2.5-1.5-0.50.51.52.5
29346784.25.27.28.29.2-1.5-0.51.52.53.5
305796.28.210.20.52.54.5
316107.211.21.55.5
3278.22.5
3389.23.5
34
35breaks:1.73breaks:{1.73,-2}breaks:{1.73,-2,0,4}
3612.734.466.197.929.6512.730.733.734.735.7312.730.730.734.735.73
3723.735.467.198.9210.6523.731.734.735.736.7323.731.731.735.736.73
3834.738.199.9211.6534.735.736.737.7334.732.736.737.73
395.739.1912.655.736.738.735.733.738.73
406.7313.656.739.736.739.73
417.737.737.73
428.738.738.73
43
44steps:0.75steps:{0.75,2.33,1.5}steps:{0.75,2.33,1.5,1,-2.5,0,3,4}
45123456123456123456
461.752.754.755.756.751.754.334.55671.754.334.552.56
473.55.57.52.56.666782.56.66606
484.258.258.99798.9976
49511.321011.326
505.7513.6513.65
5115.9815.98
52
53sort:-1counts:{3,-7,2,4,3,5}counts:{3,0,2,4,3,5}
5438477101345613456
552736692456724567
561655836783678
575477979
58461010
593
602
61
62
63#CALC!#CALC! 3 2 
6443
65
66
67
684.51.53.55534.57123
6976.50.55471.56.511.516.521.5
7011.5-2.553113.50.5-2.5-5.5
7116.5-5.55155555
7221.519543
7337111519
74
75 3 312
76
77
7816171819
791920212223
80222324252627
81
SEQUENCES
Cell Formulas
RangeFormula
B2B2=LAMBDA(counts,[start],[breaks],[steps],[stack],[sort],LET(v,TOROW(counts),b,ABS(v),c,TOROW(IF(IO(breaks),1,breaks)),a,COUNT(b),j,COUNT(c),u,TOROW(steps),e,COUNT(u),g,IF(OR(IO(steps),e>1),1,INDEX(u,1)),q,ABS(IF(g,g,1)),o,(IF(v<0,"",IF(ISERROR(b+u),1,u))-1)*SEQUENCE(MAX(b),,0,IF(e>1,1,0)),l,IF(IO(stack),0,INDEX(stack,1)),n,IF(IO(sort),0,INDEX(sort,1)),m,SEQUENCE(MAX(b),,,q),k,IFS(m<=(b*q),IF(g<=0,SEQUENCE(MAX(b),,,g),m)+IF(j=1,SEQUENCE(,COUNT(b),0,INDEX(c,1)),LET(r,SCAN(0,c,LAMBDA(y,x,y+x)),t,a-COUNT(r),HSTACK(0,r,SEQUENCE(,IF(t>0,t,1),MAX(r)+1,1))))),p,CHOOSECOLS(k+o,FILTER(SEQUENCE(,a),b<>0)),d,IF(l,p,TOROW(IF(ISERROR(p),IF(ERROR.TYPE(p)=3,"",p),p),2,1)),i,d+(IF(IO(start),0,start-1)),w,IF(n,IF(l,IFNA(WRAPCOLS(SORTBY(TOCOL(i),TOCOL(IF({1},SEQUENCE(,COLUMNS(i)),i)),1,TOCOL(ISNA(i)),1,TOCOL(i),n),ROWS(i)),""),IF(PRODUCT((counts>=0)*1),SORT(i,,n,1),IFNA(TOROW(LET(za,TRANSPOSE(i),zb,ROWS(za),zc,SEQUENCE(zb),zd,ISERROR(za),ze,NOT(zd),zf,TOCOL(za,2,1),zg,TOCOL(IFS(ze,1),2,1),zh,SORTBY(EXPAND(SORTBY(zf,zg,1,zf,n),zb*1),VSTACK(zg,TOCOL(IFS(zd,1),2,1)),1,VSTACK(TOCOL(IFS(ze,zc),2,1),TOCOL(IFS(zd,zc),2,1)),1),WRAPCOLS(zh,zb))),""))),IFNA(i,"")),f,IF(ISERROR(w),IF(ERROR.TYPE(w)=3,"",w),w),h,IF(COLUMNS(counts)>=ROWS(counts),f,TRANSPOSE(f)),IF(OR(IO(breaks),ISNUMBERS(breaks))*(COUNT(start)<=1)*OR(ISNUMBER(start),IO(start))*ISNUMBERS(steps,TRUE)*(COUNT(l)<=1)*ISNUMBER(l)*ISNUMBERS(counts,TRUE)*ISNUMBERS(sort,TRUE)*OR(IO(sort),sort=1,sort=-1)*OR(IO(stack),COUNT(stack)=1),h,NA())))
B4:O4B4=SEQUENCES({2,4,-3,1,4})
B6:F9B6=SEQUENCES({2,4,3,1,4},,,,1)
B11:B24B11=SEQUENCES({2;4;3;1;4})
D11:G15D11=SEQUENCES({2;4;3;1;4},,,,1)
I6:S6I6=SEQUENCES({2,3,2,4})
I7:S7I7=SEQUENCES({2,-3,2,4})
I8:S8I8=SEQUENCES({2,-3,2,4},,,,,1)
I9:S9I9=SEQUENCES({2,-3,2,4},,,,,-1)
I10:P10I10=SEQUENCES({2,0,2,4},,,,,1)
I11:P11I11=SEQUENCES({2,0,2,4},,,,,-1)
B27:G33B27=SEQUENCES({3,7,2,4,3,5},,,,1)
I27:N33I27=SEQUENCES({3,7,2,4,3,5},2.2,,,1)
P27:U33P27=SEQUENCES({3,7,2,4,3,5},-3.5,,,1)
B36:G42B36=SEQUENCES({3,7,2,4,3,5},,1.73,,1)
I36:N42I36=SEQUENCES({3,7,2,4,3,5},,{1.73,-2},,1)
P36:U42P36=SEQUENCES({3,7,2,4,3,5},,{1.73,-2,0,4},,1)
B45:G51B45=SEQUENCES({3,7,2,4,3,5},,,0.75,1)
I45:N51I45=SEQUENCES({3,7,2,4,3,5},,,{0.75,2.33,1.5},1)
P45:U51P45=SEQUENCES({3,7,2,4,3,5},,,{0.75,2.33,1.5,1,-2.5,0,3,4},1)
B54:G60B54=SEQUENCES({3,7,2,4,3,5},,,,1,-1)
I54:N60I54=SEQUENCES({3,-7,2,4,3,5},,,,1)
P54:T58P54=SEQUENCES({3,0,2,4,3,5},,,,1)
B63B63=SEQUENCES({0},,,,1)
E63E63=SEQUENCES({0,0},,,,1)
H63:I65H63=SEQUENCES({0,-3,2},,,,1)
L63:M65L63=SEQUENCES({-3,2},,,,1)
P63:R65P63=SEQUENCES({-3,-1,-2},,,,1)
B68:G72B68=SEQUENCES({2,5,4,4,3,5},4.5,{-3,2,1.5,0,-2},{2.5,5,-3,0,-1,4},1)
I68:M73I68=SEQUENCES({2;5;4;4;3;5},4.5,{-3,2,1.5,0,-2},{2.5,5,-3,0,-1,4},1)
O68O68=SEQUENCES({1})
Q68Q68=SEQUENCES({0,1})
S68S68=SEQUENCES({0,0,1})
B75:G75B75=SEQUENCES({0,-5,1})
I75:J79I75=SEQUENCES({0,-5,1},,,,1)
L75:Q80L75=SEQUENCES({2;0;-5;0;-5;4;5;6},,3,,1)
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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