3

Currently, I have a table that will automatically update when new data is added to a spreadsheet. I am able to easily create a macro that will graph a set range, but how do I get it to automatically update the range so that it graphs all the right data? My goal is to be able to create a button that I can press at any time that will run a macro on this table and graph the results.

Right now, my code is:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 6/24/2010 by Nicole
'

''
    Range("R1:S12").Select
    Range("S12").Activate
    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Sheets("Intakes").Range("R1:S12"),PlotBy _
        :=xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Intakes"
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "# Cases that day"
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = False
    End With
End Sub

Thanks,

Nicole

3
  • explain what you mean by 'a table that will automatically update when new data is added to the spreadsheet'. Exactly what is your 'table'? Can't you trigger off of it? Jun 24, 2010 at 23:58
  • basically, the code in the cells is =IF(ISBLANK(OFFSET($A$2,SUM($S$2:$S12),0))=TRUE," ",OFFSET($A$2,SUM($S$2:$S12),0)) So, when the cell switches from being blank to having data, I want to add it to the range that should be graphed.
    – Nicole
    Jun 25, 2010 at 13:34
  • Nicole, when responding to comments adding an ampersand in front will send a notification to the user you're replying to, like @Lance. I'll look at this issue. Jul 1, 2010 at 15:45

3 Answers 3

2

All you need to do differently (though it slightly depends on how the original cell gets its value updated) is to put your Macro in a Worksheet Change Event, then compare the Target to the Range of Interest:

Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Target, Me.Range("R1:S12")) Is Nothing Then
    Range("R1:S12").Select
    Range("S12").Activate
    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Sheets("Intakes").Range("R1:S12"),PlotBy _
        :=xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Intakes"
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "# Cases that day"
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = False
    End With
End If  

End Sub
1

FYI, found another solution for this...

I was struggling with the same problem:

I had already created a dynamic name that selects my desired graph range [the whole range], but after I inserted this name into the graph range, it immediately converted back to a static range [cell reference] and no longer graphed my dynamic range name.

I created a macro: select graph, change data selection, overwrite static cell range with my created dynamic name, enter.

I assigned this macro to a "Update Graph" button, but noticed the macro had taken the name's range as static, not dynamic.

SO HERE'S THE SOLUTION: open macro menu, modify created macro... in the macro, look for the fixed cell range, overwrite it with 'sheetname'!dynamicname

done. button should now update graph with dynamic range data.

hope this helps someone out there. cheers.

0

A reasonably simple solution is to make that range a named range, using the input box that is located to the right of the formula input section. That way, instead of referring to the range as "R1:S12", you can access it by name, Range("MyOwnRange"). If you insert rows or columns inside the named range, it will resize automatically to include the new rows.

2
  • This doesn't work because the rows are automatically added at the end of the range, so this row is not included in "MyOwnRange". I basically have code filled in 1000 consecutive cells in two columns. The cell appears blank if there is not yet data to populate the cell (here I am going by dates, so the cells will appear blank until we have reached that date). Basically, I want it to include all cells that have text in them (not code), which does not work when naming the range.
    – Nicole
    Jun 24, 2010 at 18:08
  • @Nicole Seems the same problem that the one discussed in this thread mrexcel.com/archive/Chart/1911.html . The solution at the end of the thread. Jun 26, 2010 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.