Producing an Array of Numbers

Written by Allen Wyatt (last updated November 9, 2024)
This tip applies to Excel 2019, Excel in Microsoft 365, and 2021


4

Sonny has found an array formula used in several Excel calendar templates that has him baffled. It returns an array of numbers from 0 to 41 arranged in 7 columns and 6 rows. The formula is this: ={0,1,2,3,4,5,6} + {0;1;2;3;4;5}*7. Sonny wonders how Excel evaluates this formula to produce that array.

This type of formula works because of the dynamic array capabilities inherent in the latest versions of Excel. There are three things to keep in mind in looking at this formula. First, Excel allows you to enter arrays in a formula by enclosing the array within curly braces. Second, column elements in an array are separated by commas. Third, row elements in an array are separated by semicolons.

You can most easily see how these three elements are utilized in Excel by entering the first portion of the formula in cell A1:

={0,1,2,3,4,5,6}

What you end up with is the values 0 through 6 in the range A1:G1—this is a single-row array, consisting of values in seven columns. The fact it is a single-row array is evidenced by there being no semicolons within the braces that define the array.

Now, do the same thing with the second array in the formula, but this time enter it into cell A4:

={0;1;2;3;4;5}

The result is the values 0 through 5 in the range A4:A9—this is a single-column array, consisting of values in six rows. The fact it is a single-column array is evidenced by there being no commas within the braces that define the array.

Now go ahead and combine the two arrays, using the plus sign in the original formula. This time enter the following formula into cell A12:

={0,1,2,3,4,5,6} + {0;1;2;3;4;5}

What you end up with is a two-dimensional array (or, if you prefer, a matrix) in the range A12:G17. (See Figure 1.)

Figure 1. An array created by a simple formula combining two arrays.

Each cell in the matrix is calculated by adding the elements in the two arrays. The sum of the elements defines the value that appears at the intersection of the columns and rows. So, cell A12 contains the result of 0+0, cell B12 the result of 1+0, cell C12 is 2+0, and so on. When the second row of the array is calculated, cell A13 is the result of 0+1, cell B13 is 1+1, cell C13 is 2+1, and so on. This process is repeated for each of the 7 columns and 6 rows.

Now you can add the final portion of Sonny's formula, and enter it into cell A20:

={0,1,2,3,4,5,6} + {0;1;2;3;4;5}*7

The only difference between this and the just-explained formula is the multiplication by 7. Since multiplication is performed before addition, each element in the row array {0;1;2;3;4;5} is multiplied by 7 before it is added to the column array. You could just as easily have done the multiplication explicitly by adjusting the row array in this manner:

={0,1,2,3,4,5,6} + {0;7;14;21;28;35}

Either way you end up with the same result—a 7x6 matrix that contains the values 0 through 41. The values are calculated by the sum of the elements in the arrays that make up the matrix.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11919) applies to Microsoft Excel 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing Workbooks in a Folder

This tip presents two techniques you can use to print multiple workbooks all at the same time. Both techniques involve ...

Discover More

Automatic Periods after a Caption

Word can automatically add captions to certain elements of your documents, such as figures or tables. You can control ...

Discover More

Resizing Very Large Tables

When pasting a table into your document, you might discover that it extends beyond the right margin of your page. Here ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Condensing Sequential Values to a Single Row

If you have a bunch of ZIP Codes or part numbers in a list, you may want to "condense" the list so that sequential series ...

Discover More

Grabbing the Second-to-Last Value in a Column

Need to get at the next-to-last value in a column, regardless of how many cells are used within that column? This tip ...

Discover More

How Many Times Does Each Name Appear in a List?

If you have a list of names in a column, and you want to know how many times those names appear in a larger list of data, ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2024-11-15 17:30:06

J. Woolley

My previous comment below describes Excel array constants and mentions a VBA Variant array. I don't believe there is a way to define an array constant in VBA, but you can assign an Excel array constant to a VBA Variant array. For example,
    Dim vArray as Variant
    vArray = [{1,2,3;4,5,6}]
Brackets like [...] are an alternate form of the Application.Evaluate method. This vArray will have dimensions (1 to 2, 1 to 3). Notice an Excel array constant is base-1 (first index is 1); so is an Excel dynamic array result.
For the constant row vector in my previous comment,
    vArray = [{0,1,2,3,4,5,6}]
vArray will be 1D with dimensions (1 to 7).
For the constant column vector in my previous comment,
    vArray = [{0;1;2;3;4;5}]
vArray will be 2D with dimensions (1 to 6, 1 to 1).
In general, an Excel range can be assigned to a VBA Variant array and vice-versa. Here are several equivalent ways to assign values for the range illustrated in Figure 1 of my previous comment (A1# references the dynamic array):
    vArray = [A1#]
    vArray = [A1:G6]
    vArray = Range("A1#")
    vArray = Range("A1:G6")
    vArray = Range("A1#").Value
    vArray = Range("A1:G6").Value
After a range has been assigned to an array it can be processed efficiently. The result can be assigned to the original range or to another range with the same dimensions as vArray. For example,
    vArray = Range("A1:G6")
    ...  'process vArray
    Range("A1:G6") = vArray
That example requires a Sub procedure (macro). Here is a Function procedure (UDF) that accepts a scalar, row vector, column vector, or matrix Source constant or range and returns each element with numbers doubled:

Function Doubler(Source As Variant) As Variant
    Dim vArray As Variant, b1D As Boolean
    Dim nR As Long, nC As Long, nCL As Long, nCU As Long
    vArray = Source
    If IsArray(vArray) Then
        On Error Resume Next
            nCL = LBound(vArray, 2)
            nCU = UBound(vArray, 2)
            b1D = (Err <> 0)
        On Error GoTo 0
        If b1D Then
            For nC = LBound(vArray, 1) To UBound(vArray, 1)
                If WorksheetFunction.IsNumber(vArray(nC)) Then
                    vArray(nC) = 2 * vArray(nC)
                End If
            Next nC
        Else
            For nR = LBound(vArray, 1) To UBound(vArray, 1)
                For nC = nCL To nCU
                    If WorksheetFunction.IsNumber(vArray(nR, nC)) Then
                        vArray(nR, nC) = 2 * vArray(nR, nC)
                    End If
                Next nC
            Next nR
        End If
    Else
        If WorksheetFunction.IsNumber(vArray) Then vArray = 2 * vArray
    End If
    Doubler = vArray
End Function

Notes:
1. VBA's IsNumeric function returns True for text like "123" but Excel's ISNUMBER does not.
2. If Source is a range, empty cells will be returned as zero values; blank text like "" will not.
3. VBA's For Each...Next statement can step through each element of an array but any change made to an element will be ignored by the array.


2024-11-11 15:29:59

J. Woolley

In Excel nomenclature, braces like {...} define an array constant in terms of numeric, text, and/or logical constants; for example,
    {-6, 3.14; "hello", TRUE}
Commas separate column elements; semicolons separate row elements. An array constant must not include cell references or expressions, so A1, 2*7, PI(), etc., are not allowed. An array constant can be considered an expression, so an array constant cannot include an array constant.
In Excel (as in VBA), arrays are expressed as rows containing columns. So the 2x3 array constant {1,2,3;4,5,6} has 2 rows and each row has 3 columns. This cannot be expressed as 3 columns with 2 rows each like {1;4,2;5,3;6}. A 2D array must be rectangular, so each row must have the same number of columns.
A 2D array can be called a matrix as noted in the Tip. And a 1D array can be called a vector. So {0,1,2,3,4,5,6} is a constant row vector with 7 columns
and {0;1;2;3;4;5} is a constant column vector with 6 rows. In modern versions of Excel that support dynamic arrays, the Tip's formula
    ={0,1,2,3,4,5,6} + {0;1;2;3;4;5}*7
returns a 6x7 matrix (6 rows and 7 columns). If that formula is in cell A1, the resulting array (see Figure 1 below) can be referenced by A1#. The Nth row is given by INDEX(A1#, N, 0), so this formula returns the 1st row:
    =INDEX(A1#, 1, 0) -- equivalent to {0,1,2,3,4,5,6}
The Nth column is given by INDEX(A1#, 0, N), so this formula returns the 1st column:
    =INDEX(A1#, 0, 1) -- equivalent to {0;7;14;21;28;35}
The element at row R column C is given by INDEX(A1#, R, C), so this formula returns the last element:
    =INDEX(A1#, 6, 7) -- equal to 41
OFFSET(A1#, I, J, K, L) can be used to extract a KxL subset of A1# beginning at row I+1 and column J+1, so this formula returns the 2x2 bottom-right corner:
    =OFFSET(A1#, 4, 5, 2, 2) -- equivalent to {33,34;40,41}
In VBA, the expression
    Evaluate("A1#")(6, 7)
returns the last element (41), but curiously
    [A1#](6, 7)
returns an error. On the other hand,
    Dim v as Variant
    v = [A1#]
    MsgBox v(6, 7)
displays 41 as expected.
By the way, the Tip's last paragraph refers to a 7x6 matrix. This is not incorrect, but Excel prefers to call it 6x7.

Figure 1. 


2024-11-10 08:12:24

Tom Bates

I've pretty much ignored array formulas up to now. Thanks for the clear and concise example that I can fit in my mental toolbox! It will come in hndy, for sure!


2024-11-09 08:12:43

Miguel Antonio Suárez

Hi, Allen.

ExcelTips features a section named FORMULAS and others that describe FUNCTIONS capabilities. However, there is no a FUNCTIONS section with ExcelTips concerning those Excel tools specially.

So, I want to suggest you consider the possibility to add a FUNCTIONS section to ExcelTips so that ExcelTips users can encounter tips and stuff about FUNCTIONS.

Reading today's ExcelTips Ribbon newsletter, Help Wanted section, 'Performing Integer Divisions' question was asked for some FUNCTIONS capabilities. So, I think this is the opportunity to create such FUNCTIONS section for ExcelTips Ribbon.

Thanks a lot for your attention and I hope you encounter this suggestion useful for your ExcelTips newsletter.

Please, have a nice day, weekend...


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.