r/excelevator • Excel • Feb 07 '19

UDF - SEQUENCER ( range/columns , OPTIONAL[ rows , start , step , vertical ]) - generate a sequence including with range match and vertical switch

A sequencer UDF - an upgrade to Microsofts SEQUENCE function

SEQUENCER ( range/columns [, rows , start , step , vertical] )

SEQUENCER allows for quick and easy creation of a sequence within an array. The size of the array can be dynamic through reference to a Table or Named range to match the size, or chosen by the user using a constant value or dynamically via a formula.

SEQUENCER has a "v" switch for vertical population of the array value sequence, whereby horizontal population is the result. The "v" switch can be put in place of any argument after the first one, or at the end in its own place. The horizontal switch forces the sequence to be populated vertically rather than horizontally in the array. This is not the same as transposing the array. The array can be transposed by wrapping in the TRANSPOSE function.

To create a grid of a sequence of values, select that range and enter the formula in the active cell and enter with ctrl+shift+enter. If you select a range larger than the array parameters cater for, those array elements will be populated with #N/A

An interesting way to see the formula in action is to select a large range for the function and use 5 reference cells for the arguments, populating those values you will see the array generated dynamically in your selected region.

See here for example .gif

Scroll down to the UDF Code after the examples


So many options available, only your imagination is the limit.


4 rows 3 columns - sequence 1 thru 12

=SEQUENCER (4,3)

ColAColBColCColD
1234
5678
9101112

4 rows 3 columns, start at 10 thru 21

=SEQUENCER(4,3,10)

ColAColBColCColD
10111213
14151617
18192021

4 rows 3 columns, start at 100, step by 15 to 265

=SEQUENCER(4,3,100,15)

ColAColBColCColD
100115130145
160175190205
220235250265

4 rows 3 columns, step back by -15

=SEQUENCER(4,3,0,-15)

ColAColBColCColD
0-15-30-45
-60-75-90-105
-120-135-150-165

Change the direction of the values for a vertical sequence, 4 rows 3 columns start at 10 step 10

=SEQUENCER(4,3,10,10,"v")

ColAColBColCColD
104070100
205080110
306090120

Use a range to set the row column values, a Table is a dynamic range and so the array will match those dimensions dynamically

=SEQUENCER(Table1)

ColAColBColCColD
1234
5678
9101112

Vertical sequence of dynamic range

=SEQUENCER(Table1,"v")

ColAColBColCColD
14710
25811
36912

Vertical sequence of dynamic range, start at 10 step 10, vertical values step

=SEQUENCER(Table1,10,10,"v")

ColAColBColCColD
104070100
205080110
306090120

A vertical Table of Pi incremented by Pi

=SEQUENCER(Table1,PI(),PI(),"v")

ColAColBColCColD
3.14159312.5663721.9911531.41593
6.28318515.7079625.1327434.55752
9.42477818.8495628.2743337.69911

A Table of single values

=SEQUENCER(Table1,10,0)

ColAColBColCColD
10101010
10101010
10101010

A Table of the alphabet

=CHAR(SEQUENCER(Table1)+64)

ColAColBColCColD
ABCD
EFGH
IJKL

So many uses, this does not even scratch the surface!



Paste the following code into a worksheet module for it to be available for use.


Function SEQUENCER(vxAxis As Variant, Optional arg1 As Variant, Optional arg2 As Variant, Optional arg3 As Variant, Optional arg4 As Variant) As Variant
'SEQUENCER ( range           , [start] , [step] , [vertical] ) v1.3
'SEQUENCER ( xCount , yCount , [start] , [step] , [vertical] )
    'https://www.reddit.com/u/excelevator
    'https://old.reddit.com/r/excelevator
    'https://www.reddit.com/r/excel - for all your Spreadsheet questions!
Const vert As String = "v" ' vertical array value path flag
Dim arrayVal() As Variant
Dim xAxis As Double, yAxis As Double
Dim nStart As Double, nStep As Double
Dim uB As Integer, i As Double, ii As Double, iv As Double, isRng As Boolean, orientVert As Boolean
Dim oLoop As Double, iLoop As Double, arRow As Integer, arCol As Integer
If IsMissing(arg1) Then arg1 = ""
If IsMissing(arg2) Then arg2 = ""
If IsMissing(arg3) Then arg3 = ""
If IsMissing(arg4) Then arg4 = ""
Dim goVert As Boolean: goVert = InStr(LCase(arg1 & arg2 & arg3 & arg4), vert)
If TypeName(vxAxis) = "Range" Then
        Dim rc As Double: rc = vxAxis.Rows.Count
        Dim cc As Double: cc = vxAxis.Columns.Count
        If rc * cc > 1 Then isRng = True
End If
If isRng Then
    xAxis = rc
    yAxis = cc
    If (arg1 = "" Or arg1 = LCase(vert)) Then nStart = 1 Else nStart = arg1
    If (arg2 = "" Or arg2 = LCase(vert)) Then nStep = 1 Else nStep = arg2
    If (arg3 = "" Or arg3 = LCase(vert)) Then arg2 = 1 Else nStep = arg2
Else
    xAxis = IIf(arg1 = "" Or arg1 = LCase(vert), 1, arg1)
    yAxis = vxAxis
    If (arg2 = "" Or arg2 = LCase(vert)) Then nStart = 1 Else nStart = arg2
    If (arg3 = "" Or arg3 = LCase(vert)) Then nStep = 1 Else nStep = arg3
End If
ReDim arrayVal(xAxis - 1, yAxis - 1)
oLoop = IIf(goVert, yAxis - 1, xAxis - 1)
iLoop = IIf(goVert, xAxis - 1, yAxis - 1)
For i = 0 To oLoop
iv = 0
    For ii = 0 To iLoop
        If goVert Then
            arrayVal(iv, i) = nStart
        Else
            arrayVal(i, ii) = nStart
        End If
        nStart = nStart + nStep
        iv = iv + 1
    Next
Next
SEQUENCER = arrayVal
End Function


Let me know of any issues.

Error log:

20190211 - corrected column row count reverse when not table reference


See all related Excel 365 functions and some similar

MINIFS

MAXIFS

TEXTJOIN

CONCAT

IFS

SWITCH


UNIQUE

SPLITIT

PERCENTAGEIFS

STDEVIFS

TEXTIFS

FUNCIFS

IFEQUAL

ISVISIBLE


See a whole bundle of other custom functions at r/Excelevator

2 Upvotes

17 comments sorted by

View all comments

Show parent comment

1

u/excelevator Excel Feb 08 '19

So I bottled it

probably best option

1

u/finickyone Feb 08 '19

Trademark really :)