VBA Code to Resize a Chart

sbbarnett

New Member
Joined
May 24, 2018
Messages
3
I'm working with data that can be filtered multiple ways resulting in a variable number of rows of data. I also have a chart driven off of the data.

I want to create a macro that resizes the cahrt based on how many meaningful rows of data are present

I created the VBA code below where S27 contains the number of relevant rows. I want to substitute the row 85 below with the variable 'Counter' and I can't figure out a syntax that works.

Sub Macro1()
'
' Macro1 Macro
'
Counter = Range("S27").Value

ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(1).Values = "='All Chart Data'!N27:N85"
ActiveChart.SeriesCollection(2).Values = "='All Chart Data'!O27:O85"
ActiveChart.SeriesCollection(3).Values = "='All Chart Data'!P27:P85"
ActiveChart.SeriesCollection(4).Values = "='All Chart Data'!Q27:Q85"
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Change
ActiveChart.SeriesCollection(1).Values = "='All Chart Data'!N27:N85"
to
ActiveChart.SeriesCollection(1).Values = "='All Chart Data'!N27:N" & range("S27").value

do the same for the other lines
 
Upvote 0
Change
ActiveChart.SeriesCollection(1).Values = "='All Chart Data'!N27:N85"
to
ActiveChart.SeriesCollection(1).Values = "='All Chart Data'!N27:N" & range("S27").value

do the same for the other lines

Thank you - I tried this but got: "Run Time Error '1004': Application-Defined or object-defines error" when it reached that step
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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