Transcript of "An Introduction To Array Functions"
An Introduction to Array Functions, Offset Function, and Defined Formulas
With Applications to Pivot Table and Chart Maintenance
Barry Posterro – July 2009
Most functions we use take in multiple values and return one answer:
Ex: = SUM (A1:A5)
= A1 * A5
All return one value only and put that result in the cell where the function was written.
An array function takes in a range of values (like A1:A5) and returns a range of values.
1) Select the entire range where you want the results to appear (results range)
2) Enter your formula
3) Finalize your formula by pressing CTRL-SHIFT-ENTER
Ex: Simply reference the cells A1:A5 in cells A13:A17
The results are:
Things to notice:
1) The function has brackets around it.
These are added by Excel and tell us that we entered an array function.
2) The function is the same in each of the cells A13:A17.
This highlights the point that we only entered one formula, not five
formulas. The only formula actually being calculated is the one in cell A13.
Cells A14:A17 are just used for holding results, not calculating results. They
contain the formula only as an auditing tool to tell us what function created these
Array Function Security
One of the nice things about array functions is that they add a level of security
that we do not otherwise have if we’d entered in separate formulas.
Attempt to delete cell A16. You should get the following error:
You cannot break up an array formula after you have entered it. Excel enforces the
integrity of the formula. This error is not limited to a delete attempt. Trying to edit any
of the 5 cells will result in the same error.
Editing an Array Function
1) Re-highlight the entire results range
2) Press F2 to enter the function edit mode
3) Edit formula
Ex: Change the formula we already entered to reference column B1:B5
Notice that once you’ve edited the formula (but before CTRL-SHIFT-ENTER), the new
domain will be outlined even while the results in A14:A17 are unchanged. Pressing
CTRL-SHIFT-ENTER will cause the execution and those cells to update.
Deleting an Array Function
Select the entire results range (A13:A17) and press delete. To delete an array
function, you must delete all of the cells it uses.
Some Excel Array Functions – not so useful to us
Excel has some array functions of its own. A lot of these are associated with
statistics and data fitting.
LINEST - fits data to a line y = mx+b. Given y’s and x’s it returns m and b.
The results are 1 and 10, which make sense, since the columns differ by 10. It’s a good
idea to read the help files associated with any new array function you are using from
Excel’s library. This function returns its results in row form. If you had highlighted two
stacked cells like C13 and C14, you would have gotten the result 1 twice.
Like LINEST, there is also LOGEST, which estimates the fit y = b*m^x
TREND – uses the linear estimate of LINEST to produce what the fitted values of
the data would be.
Some Excel Array Functions – useful to us
Often we find ourselves in a situation where we receive data in one orientation (column)
but need it in the other orientation (row) for what we need to do. The array function
TRANSPOSE handles this:
Ex: Take Column B and make it a row.
We can also use this to transpose the entire table. This is our first use of a two
This is a very useful array function that the rest of this lecture will rely upon. It returns
an array of values in the shape that the user specifies.
It takes five inputs.
OFFSET ( cell_anchor , rows_down, columns_right, height, width)
The last two inputs are optional. They default to 1 if omitted.
This example returns an array formed by:
1) relative to A1
2) move down 2 rows
3) move over 1 column - now we’re in cell B3
4) return a 4 row, 5 column array with cell B3 as the corner
If you want the cell_anchor to be the corner of your results, set the rows_down to zero
and the columns_right to zero.
You can also enter negative numbers for your rows_down or columns_right parameters.
A height of 1 implies a row. A width of 1 implies a column.
As mentioned earlier. Leaving off the last two parameters results in a single cell as those
parameters each default to 1.
Notice that if we’re only returning one cell, we do not need to use CTRL-SHIFT-ENTER
and there are no brackets around the formula in the formula bar.
So far, we’ve just been returning data, but we can also use OFFSET as the input to any
function that takes in an array. For example, SUM or AVERAGE.
This method allows us to make formulas in which the number of points to average can
become one of the inputs to the formula. Let’s look at this with rolling averages.
For each period, we take the average of the last number of months given in column A.
OFFSET with TRANSPOSE
Keep in mind that the display area must be the same shape as the array created by the
offset function or you may get unexpected results. The following is what occurs if we try
to put a row created by offset into a column:
If a column is really what you want here, you need to invoke the transpose function.
While we employ using defined names to refer to cells in a workspace, the defined names
capability of Excel also extends to formulas and constants (constants being a very simple
Ex: Define the word “million” to be 1,000,000.
Now, typing “=million” in any cell will return that value.
And, we can use it in any formula like a variable.
Likewise, we can define and name a formula.
This formula sums up all of column B:
Putting this name in any cell will return the sum of column B.
- simple function that returns how many values are present in a rang of cells
Ex: First, let’s triple the size of the data.
This function is the last key to the payoff of the earlier work we did.
Self-Updating Pivot Table
The argument of a pivot table’s range property is usually a set area of a
spreadsheet. For example:
As we know, if the dimensions of this area change, we need to reset the range argument
of the pivot table. That is, if more rows or columns are added, we need to tell that to the
Using OFFSET, COUNTA and a defined name, we will make the argument of the pivot
table’s domain a defined name that will never need to be updated.
Step 1. Create an array that uses variable arguments for its size.
In an easier to see view:
Pressing ADD, and then clicking into the formula bar, Excel will highlight the area that
you are referencing with this formula.
Step 2. Assign the name PT_data (or whatever you called this) to the range argument of
the pivot table.
Step 3. Click Finish and you are done.
Now, going forward, as long as the data you want begins in the same corner as the data
you are referencing with your defined name, PT_data, you never need to go through the
Pivot Table options again to reset the range as more rows or columns are added.
Let’s see that. Add another column of random data call “Garbage” and fill it in. Also,
add another row called “Test.”
Now, simply refresh your pivot table.
The new field, Garbage, shows up now, and if you drop Dates into the row field, you’ll
see Test now shows up as well.
Beware of rogue data corrupting your defined name PT_data. Because the
COUNTA is counting all values in the row and column, having stray values in those rows
or columns will corrupt your dimensions. Also, make sure that you pick a column for
COUNTA that will never be empty or your PT_data will not have as many rows as you
need. The correct column to pick is not always the far left column.
Also, while we call these “self-updating,” that only refers to the range. If data
within your table changes, you still need to refresh your pivot table as you normally
Self-Updating and/or Interactive Charts
Self Updating Charts
- we can apply very similar techniques to those of the self-updating pivot table
to create self-updating charts and interactive charts
Step 1. Using offset and counta, create a defined name for the column “Random” that
brings in all of its elements.
In an easier to see view:
Looking at each argument:
anchor = G1 I am anchoring off of the label of the data – this is my style, but it works
out well. I’ll show you why later.
rows_down = 1 I do not want the label to be part of my data, just the values
columns_over = 0 I am staying in this column
height = # values – 1 I am counting all the values in column G, but I don’t want
to count the label
width = 1 My data is only 1 column wide
Go back to the defined name dialog and click in your formula for random_graph. It
should highlight the data in that column, leaving off the label “Random.”
Step 2. Build a chart. On the series tab, we use the defined name (and the name of the
workbook) as the source of the data.
Click Finish and we’re all set.
Testing: Now, go back to Column G and add and take away values and see what
happens. The graph will grow and shrink automatically according to the size of the data.
Note that Excel requires that you put in the name of the workbook and an exclamation
point before the name of the data.
- while it is nice to have the chart automatically show all data, that may not be the
desired behavior. We can make the graph interactive by playing around with the
parameters of the defined name “random_graph.”
Let’s recall those parameters:
anchor = G1
rows_down = 1
columns_over = 0
height = counta(Sheet1!$G:$G)-1
width = 1
By making rows_down and height dependent on cells we can manipulate, we gain
tremendous control over the graph.
Add the following two defined cells in K1 and K2:
Now edit the defined name “random_graph” to reference these.
In an easier to see view:
Now, playing with changing the values in those cells will immediately change the look of
the graph. These values control the start and stop points of the data. Notice that there is
no additional logic beyond that which we provided. Changing graph_rows to 100 will
leave you with a lot of empty spots in your graph.
To finish this graph off, we need to add the labels for the x-axis.
To do this, we simply take the formula we wrote for “random_graph” and re-anchor it in
column A. I always simply name mine “x_axis.”
The easiest way to do this is to:
1) bring up the defined name dialog box
2) click on “random_graph”
3) rename it “x_axis”
4) change the anchor parameter to A1 from G1
5) click Add
6) Click OK
Include this name in the graph as the X-axis labels and now the X-axis will move in
conjunction with your data.
The final product:
You should practice this with this by trying to include another series of data.
For example, try to add the “Ones” data to this graph as well, so that you’re graphing two
series of data, both controlled by cells K1 and K2.
While the set up of a graph like this is more involved, the payoffs come in how quickly it
can be updated or changed without having to go through the chart dialog boxes. A graph
like this is also a lot more secure.
We have graphs like this that use 10 data series and it is a lot easier for us to simply
change the number of rows or the starting point than it would be to go through and edit
10 series formulas, hoping not to miss one. Multiply that by the 20 spreadsheets we
update weekly, and the savings is well worth the couple of extra set up minutes.
I encourage you to also get creative with your input cells. With a MATCH function and a
drop down you can make the graph_start input a lot clearer by using actual dates. I will
send out an example of such a graph later.