views:

4297

answers:

6

Hi,

I've got a spreadsheet with plenty of graphs in it and one sheet with loads of data feeding those graphs.

I've plotted the data on each graph using

=Sheet1!$C5:$C$3000

This basically just plots the values in C5 to C3000 on a graph.

Regularly though I just want to look at a subset of the data i.e. I might just want to look at the first 1000 rows for example. Currently to do this I have to modify the formula in each of my graphs which takes time.

Would you know a way to simplify this? Ideally if I could just have a cell on single sheet that it reads in the row number from and plots all the graphs from C5 to C 'row number' would be best.

Any help would be much appreciated.

+1  A: 

You can set the range for a chart dynamically in Excel. You can use something like the following VBA code to do it:

Private Sub Worksheet_Change(ByVal Target as Range)
    Select Case Target 
    Case Cells(14, 2)
        Sheet1.ChartObjects(1).Chart.SetSourceData Range("$C5:$C$" & Cells(14,2))
    ...
    End Select
End Sub

In this case, the cell containing the number of the last row to include is B14 (remember row first when referring to the Cells object). You could also use a variable instead of the Cells reference if you wanted to do this entirely in code. (This works in both 2007 and 2003.) You can assign this procedure to a button and click it to refresh your chart once you update the cell containing the last row.

However, this may not be precisely what you want to do ... I am not aware of a way to use a formula directly within a chart to specify source data.

Edit: And as PConroy points out in a comment, you could put this code in the Change event for that worksheet, so that neither a button nor a key combination is necessary to run the code. You can also add code so that it updates each chart only when the matching cell is edited.

I've updated the example above to reflect this.

Dave DuPlantis
You could also trigger the above via Worksheet_Change so that it gets done automatically when the cell gets updated, rather than having to click a button
ConroyP
+1  A: 

You could look at dynamic ranges. If you use the OFFSET function, you can specify a starting cell and the number of rows and columns to select. This site has some useful information about assigning a name to an OFFSET range.

Jason Z
+3  A: 

OK, I had to do a little more research, here's how to make it work, completely within the spreadsheet (without VBA):

Using A1 as the end of your desired range, and the chart being on the same sheet as the data:

Name the first cell of the data (C5) as a named range, say TESTRANGE.
Created a named range MYDATA as the following formula:

=OFFSET(TESTRANGE, 0, 0, Sheet1!$A$1, 1)

Now, go to the SERIES tab of the chart SOURCE DATA dialog, and change your VALUES statement to:

=Sheet1!MYDATA

Now everytime you change the A1 cell value, it'll change the chart.

Thanks to Robert Mearns for catching the flaws in my previous answer.

Lance Roberts
I'm confused about what to do with A1 and B1.
Scottie T
A1 will be the row number to start with (relative to the full range) so for the question example: 1. B1 will be the row number to go to, so for the question example 1000. You could just put a 1 in the offset formula, and use a single cell for the end row number needed.
Lance Roberts
Some code is required to set the named range as the Data Source. When a named range is used as Data Source for a chart, Excel automatically converts it to a static range.
Robert Mearns
I just read your post on that, I'll have to do some research to figure out how to do this without code, which is what it appears the questioner wants.
Lance Roberts
Your updated solution works beautifully.
Robert Mearns
+1  A: 

+1s for the name solution.

Note that names don't really really reference ranges, they reference formulae. That's why you can set a name to something like "=OFFSET(...)" or "=COUNT(...)". You can create named constants, just make the name reference something like "=42".

Named formulae and array formulae are the two worksheet techniques that I find myself applying to not-quite-power-user worksheets over and over again.

Mike Woodhouse
good info on the reality of names
Lance Roberts
+1  A: 

This can be achieved in two steps:

  • Create a dynamic named range
  • Add some VBA code to update the charts data source to the named range

Create a dynamic named Range

Enter the number of rows in your data range into a cell on your data sheet.

Create a named range on your data sheet (Insert - Name - Define) called MyRange that has a formula similar this:

=OFFSET(Sheet1!$A$1,0,0,Sheet1!$D$1,3)

Update the formula to match your layout

  • Sheet1!$A$1 set this to the top left hand side of your data range
  • Sheet1!$D$1 set this to the cell containing the number of rows
  • 3 set this value to the number of columns

Test that the named range is working:

Select the dropdown menus Edit - Go To, type MyRange into the reference field. Your data area for the chart should be selected.

Add some VBA code

Open the VBA IDE (Alt-F11)

Select Sheet1 in the VBAProject window and insert this code

Private Sub Worksheet_Change(ByVal Target As Range)

        If Target.Address <> "$D$1" Then Exit Sub
    'Change $D$1 to the cell where you have entered the number of rows
    'When the sheet changes, code checks to see if the cell $D$1 has changed

       ThisWorkbook.Sheets("Sheet1").ChartObjects(1).Chart.SetSourceData _
         Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")
    '  ThisWorkbook.Sheets("Chart1").SetSourceData _
         Source:=ThisWorkbook.Sheets("Sheet1").Range("MyRange")
    'The first line of code assumes that chart is embedded into Sheet1
    'The second line assumes that the chart is in its own chart sheet
    'Uncomment and change as required

    'Add more code here to update all the other charts

End Sub

Things to watch for

Do not directly use the named range as the data source for the chart. If you enter the named range "MyRange" as the Source Data - Data Range for the chart, Excel will automatically convert the named range into an actual range. Any future changes to your named range will therefore not update your chart.

Performance might be impacted by the approaches listed above.

The OFFSET function in the named range is "volatile" which means that it recalculates whenever any cell in the workbook calculates. If performance is an issue, replace it with the INDEX formula.

=Sheet1!$A$1:INDEX(Sheet1!$1:$65536,Sheet1!$D$1,2)

The code fires everytime data is changed on Sheet1. If performance is an issue, change the code to run only when requested (i.e. via a button or menu).

Robert Mearns
Good detailed answer!
ConroyP
Thanks for pointing out the problem, I've fixed my answer to work completely from a cell now.
Lance Roberts
A: 

An easy way to do this is to just hide the rows/columns you don't want included - when you go to the graph it automatically excludes the hidden rows/columns