woensdag 23 oktober 2013

Excel: Offset Function With Relative or Mixed Cell Referencing

Normally when you use the OFFSET function to create dynamic names, it will use absolute cell referencing. So, you will get examples like this:

=OFFSET(Sheet2!$A$1;0;0;COUNTA(Sheet2!$A:$A);1)

So, what if we used relative cell referencing instead? The result would depend on the cell our cursor is in when we create our formula and the cell our cursor is in when we are looking at the name again.
  • Fill the range A1:A4 with the figures 12 to 15.
  • Put your cursor in A1 again.
  • Create a name RANGE based on the formula with relative cell referencing:
=OFFSET(Sheet1!A1;0;0;COUNTA(Sheet1!A:A);1)
  • Now put your pointer in B15.
  • Look at the name again:

As you can see, we get a different formula now based on the position of our cursor. So for what purpose could we use relative cell referencing?

On a sheet with the name relativeoffset,I created a table in A1:J18 like this:

Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9
Salesman 1 12 29 46 63 16 17 18 19 23
Salesman 2 13 30 47 64 17 23 45 67
Salesman 3 14 31 48 65 18
Salesman 4 15 32 49 66 19 19 19 56
Salesman 5 16 33 50 67 20 20 34
Salesman 6 17 34 51 68 21 21 45 45
Salesman 7 18 35 52 69 22 22 56
Salesman 8 19 36 53 70
Salesman 9 20 37 54 71 23 23 23 23
Salesman 10 21 38 55 72 24 24
Salesman 11 22 39 56 73 25 25 25
Salesman 12 23 40 57 74 26 26 26 26
Salesman 13 24 41 58 75 27
Salesman 14 25 42 59 76 28 28 28 28
Salesman 15 26 43 60 77 29 29 29
Salesman 16 27 44 61 78 30 30 30 30
Salesman 17 28 45 62

As you can see, not every row is completely filled in. Now I created three names:

With the cursor in B2:

BLOCK =OFFSET(relativeoffset!$B2;0;0;1;COUNTA(relativeoffset!$B2:B2))

SALESMAN =OFFSET(relativeoffset!$A1;0;0;1;1)

WEEK =OFFSET(relativeoffset!$B$1;0;0;1;COUNTA(relativeoffset!$B2:B2))

As you can see, I actually used mixed cell referencing.

Now I created a graph with just one series:

Series name: =relativeoffset!Salesman
Series value: =relativeoffset!Block
Axis label names: =relativeoffset!Week

To get the graph really going (the formula needs recalculating every time I move the cursor) I had to add a bit of VBA to the worksheet relativeoffset:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.Calculate
End Sub

And then the content of the graph moves along with your cursor:


In poistion F10 I get Salesman 9 until Week 5 and so on.

You can also download the file graphexperiment.zip through this link:

https://drive.google.com/folderview?id=0B7HgkOwFZtdZVmhRQUZFM28yc1U&usp=sharing
Een reactie plaatsen