Updating chart sourcedata range in VBA

tpmcavoy

New Member
Joined
Aug 27, 2009
Messages
2
I am relatively new to VBA and I am having trouble updating the sourecdata for a chart in VBA. Recording a macro for it yeilded:

Sheets("Rating Chart").Select
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.SetSourceData Source:=Sheets("Summary").Range("AA2:AB61"), _
PlotBy:=xlColumns

I want to dynamically determine how many rows of data I have and update the source range accordinly.

I locate the last row of data using:
FinalRow = Sheets("Summary").Cells(Rows.Count, 27).End(xlUp).Row

I then try to update the source range using the following:

ActiveChart.SetSourceData _
Source:=Sheets("Summary").Range(Cells(2, 27), Cells(FinalRow, 28)), _
PlotBy:=xlColumns

Using the Cells in the Range seems to be my problem but I don't know how to vary the range any other way. Any help would be greatly appreciated. Thank you.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Perhaps:
Code:
FinalRow = Sheets("Summary").Cells(Sheets("Summary").UsedRange.Rows.Count, 27).End(xlUp).Row

ActiveChart.SetSourceData Source:=Sheets("Summary").Range("AA2:AB" & FinalRow), PlotBy:=xlColumns
 
Upvote 0
For instance,

Code:
ActiveChart.SetSourceData _
Source:=Sheets("Summary").Range("AA2:AB" & FinalRow), _
PlotBy:=xlColumns
 
Upvote 0
FWIW, the reason this didn't work

Code:
ActiveChart.SetSourceData _
    Source:=Sheets("Summary").Range(Cells(2, 27), Cells(FinalRow, 28)), _
    PlotBy:=xlColumns

is that VBA doesn't know what the Cells inside the parentheses refer to. This should work:


Code:
ActiveChart.SetSourceData _
    Source:=Sheets("Summary").Range(Sheets("Summary").Cells(2, 27), Sheets("Summary").Cells(FinalRow, 28)), _
    PlotBy:=xlColumns
 
Upvote 0
Hi All,

Sorry, I know this is a old post. Just wanted to say that i have used this to update charts automatically.

Thank you.

I am also using application.ontime to schedule an update for a dashboard.

But I don't want the dashboard displayed all the time (or to pop up while I am working).

Is there anyway to automatically update the chart without Activating it?

Thanks.

PR1ASD
 
Upvote 0
Yes, you can refer to it using Charts("chart name") if it's a chart sheet, or sheets("sheet name").Chartobjects("chart name").Chart if it's embedded on a worksheet.
 
Upvote 0
Thank you for the quick response, Rory.

worked like a charm.

thank you
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top