Problem setting chart source

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
308
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I am helping a friend update an Excel file made by someone else, and it uses charts. I don't have much experience with them, so am having a hard time figuring out how to use VBA to change the source data.

The existing chart data is using a fixed last row, meaning if there is ever any data that goes beyond that row, it is ignoring it. Here's a screenshot of the chart source information that currently exists.

Screenshot (327).png


You can see that the last row will always be 772. But sometimes the data may extend beyond that row. I know how to write code to get the last row with data in it. But I'm having trouble updating the chart data range.

Here is the code I've written to do it.

VBA Code:
Set Rng = ThisWorkbook.Sheets("Net Results").Range("$A$3:$A$" & LR & ",$D$3:$D$" & LR)
Charts("Drawdown").SetSourceData Source:=Rng, PlotBy:=xlColumns

I am getting Run-time error 9: Subscript out of range on the 2nd line of code. I can't figure out why. The chart name is correct. LR is the variable that holds the last row. By stepping through the code, I have confirmed that its value is correct. Any ideas what might be wrong?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
For charts with a data source that may grow over time, I used named ranges in my series, like so:
1706824982242.png
 
Upvote 0
For charts with a data source that may grow over time, I used named ranges in my series, like so:
View attachment 106183
I appreciate the reply, but I'd really love to know why the code isn't working. I need to learn about how to update charts via VBA, so understanding what I've done wrong in the code would be great.
 
Upvote 0
I was able to figure it out, finally. Instead of the two lines of code I listed in my original post, I used the below code and it worked.

VBA Code:
Set Rng = ThisWorkbook.Sheets("Net Results").Range("$A$3:$A$" & LR & ",$D$3:$D$" & LR)
ThisWorkbook.Sheets("SummaryCharts").ChartObjects("Drawdown").Activate
ActiveChart.SetSourceData Source:=Rng

I don't know if this is the best way to do it (I've heard you should avoid using "Activate" in VBA code) but since it worked, I'll go with it unless anyone has any other suggestions.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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