# Blog Archives

## Excel – Create a Dynamic 12 Month Rolling Chart

If like many people producing reports at work you report on a rolling yearly basis, you are probably manually changing the data range of your chart every month too either by changing the cell references or even worse, deleting the first month of data and adding the most recent data to the end of your table. All time consuming, and ultimately unnecessary.

It is possible to create a dynamic 12 month rolling chart that automatically displays the last 12 months of data (or any other time frame in fact). All you have to do is add data to the end of your data table and let Excel do the rest!

For this you will need to use the **OFFSET** function. If you are not familiar with this function, then go to my “Creating Dynamic Ranges” blog ( http://wp.me/p2EAVc-9Y ) to understand how this function works before tackling this…otherwise this will be a complete mystery and quite unfathomable.

Let’s start with some basic data – one year of sales figures.

To create a dynamic chart using this simple table we will need two named dynamic ranges – one for the data itself and one for the labels. Note that when working with charts you will need to create a separate dynamic range for each series as charts treat each series separately so you cannot create a single dynamic named range that includes all rows and columns.

Personally, I like to start by creating the dynamic range that handles the data.

Go to **NAME MANAGER** and select **NEW**, or go to **DEFINE NAME**. Give the range a name of some sort. In this example I will use **ChtData**. Don’t use the word Chart in your name, apparently it won’t work (Mr Excel).

In the refers to box enter the following formula;

We use **-12** in the **HEIGHT** argument as we always want to count back 12 rows from the last cell containing data.

Then we need a dynamic named range to pick out the correct labels (in this case dates) to match the dynamically selected data. In this example let’s call it ChtLabels.

Following the same steps as above enter the following formula;

Rather than work everything out from another reference cell, we can use our **ChtData** range as the reference point or in this case reference range. This will work out what to select based on the first dynamic range, and select values from one column to the left (**WIDTH** argument = **-1**).

Check that both ranges work properly by adding some new data at the bottom of the chart and click into the refers to box of the named range to see which cells it selects.

Now time to create our chart. Just follow the usual steps to create a chart…**INSERT**, **CHART** etc.

Once our “static” chart is set up, now comes the clever bit.

Make sure you have clicked on your chart and then click on **SELECT DATA**.

In the **SELECT DATA SOURCE** window, click on the series you want to turn into a dynamic one. If you have multiple series, then you will need to do the following steps for each one.

Click on **EDIT**.

Currently you should see the cell references relating to the series in the **SERIES VALUES** box. Remove any cell references but ** leave** the sheet name and exclamation mark. Replace the cell references with the dynamic range name for the data – in this example

**ChtData**. Click on

**OK**. Now do the same for the labels.

Click on any one of the labels under **HORIZONTAL (CATEGORY) AXIS LABELS** and then click on **EDIT**.

As before, remove any cell references from **AXIS LABEL RANGE**, leaving the sheet name and exclamation mark exactly as before. So in this example we should now have **Book1!ChtLabels**.

All ready to go! Now just add new data and watch your chart automatically update to always show the last 12 months of data.

## Excel – Creating Dynamic Named Ranges

If you use named ranges in Excel, you’ll know how useful they can be. If you are not familiar with them check my blog ( http://wp.me/p2EAVc-99 ) on setting up named ranges and how they can be used.

If you are currently using them you will also know that it can be a bit of pain having to update the range all the time as new records are added or removed from your data. If your data does change regularly, wouldn’t it be nice to have a named range that automatically adjusts to the correct number of rows or columns? Well…you can, by creating dynamic ranges.

At the heart of a dynamic range is the **OFFSET** function. Before we embark on creating ranges, let’s look at the syntax first as it is not that straightforward.

The official syntax you get from Microsoft when you enter the function is;

**=OFFSET(reference, rows, columns, [height], [width])**

…which doesn’t really tell you much if you have never used this function before.

**Reference**: every range has a starting point, even dynamic ones. Reference is like an anchor point from which the rest of the range is referenced.

**Rows**: is the number of rows away from the reference or anchor point. Positive numbers represent rows down, and negative numbers rows up.

**Columns**: is the number of columns away from the reference or anchor point. Positive numbers represent columns to the right, and negative numbers columns to the left.

=OFFSET($A$1,0,0 would mean the range starts from A1

=OFFSET($A$1,1,0 would mean the range starts from A2 (1 row below)

=OFFSET$A$1,0,1 would mean the range starts from B1 (1 column to the right)

…and so on and so forth.

**Height**: this is optional, but represents the number of rows you want to include in your range. So if we had =OFFSET($A$1,0,0,5 then the range would be 5 rows starting from A1.

**Width**: another optional argument. This sets the number of columns that make up your range. Continuing the formula from above, if we have =OFFSET($A$1,0,0,5,3) then we would set our range to 5 rows high and 3 columns wide starting at A1.

This of course gives us a fixed range, as we are specifically defining the number of rows and columns we want in the named range.

So how do we get it to be dynamic?

We need to build in functions that calculate the number of rows and/or columns within the **OFFSET** function. Generally speaking we do this within the **height** and **width** parts of the function, but you may need to alter the other arguments such as when you work with dynamic charts.

So let’s build a dynamic range;

First of all, you can’t create a dynamic named range in the same way as a normal named range i.e. select a bunch of cells and give them a name. We need to go to the **NAME MANAGER** and click on **NEW**, or go to **DEFINE NAME**.

In either case, give your range a name.

Decide whether the range is specific to the worksheet or can be referenced from anywhere in the workbook.

Add a comment…purely optional as a note to yourself or anyone else vaguely interested in the named range.

And then the important bit – **REFERS TO:** Rather than entering a basic range, this is where we have to use the **OFFSET** function.

Starting with a basic table, I want to create a dynamic range that automatically works out the number of rows in a fixed width table of 5 columns.

So our formula will look like this;

**=OFFSET(Sheet1!$A$1,1,0,COUNT(Sheet1!$A:$A),5)**

In plain English…starting 1 row below A1, count the number of cells that contain numbers in column A (to work out number of rows) and set the width to 5 columns.

In this example I am excluding the heading row, but you may well need to include it, if for example, you are creating a dynamic range to use in a Pivot table.

Below, we can see the correctly selected data.

If I add a few rows of data, the range is automatically recalculated to include them.

If I want the range to check the number of columns too then I need to modify the formula as follows;

**=OFFSET(Sheet1!$A$1,1,0,COUNT(Sheet1!$A:$A), COUNTA(Sheet1!$1:$1))**

Now, not only does the formula count the number of cells containing numbers in column A, it counts the number cells containing text (hence COUNTA)across the top of my data table to give me the number of columns that make up my table.

So now you can create either a one-way or two-way dynamic range depending on your personal requirements, and no need to manually update the **REFERS TO** cell references.

In another blog, I will show you how you can use the **OFFSET** function to create dynamic charts.