Dynamic chart Range... Help!!

Shawn_archer

New Member
Joined
Mar 19, 2011
Messages
5
My first post here….I have recorded a macro that could generate a simple trendline chart and I want it make generic so that it can generate charts for a dynamic set of data in a single sheet.
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""Time"""
ActiveChart.SeriesCollection(1).Values = "=Data!$A$2:$A$16"
ActiveChart.SeriesCollection(1).XValues = "=Data!$B$2:$B$16"
Sample data:
Col A ColB
A 10
B 14
C 26
D 27
E 17
F 24
1<sup>st</sup> chart should generate Trendline for A,B,C and 2<sup>nd</sup> chart should generate D,E,F. My Data type & Columns will remain the same, only that i require that charts ranges are to be dynamic.
Your assistance on this would be a great help for me .. Thanks!!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Maybe like this

Code:
LR = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""Time"""
ActiveChart.SeriesCollection(1).Values = "=Data!$A$2:$A$" & LR
ActiveChart.SeriesCollection(1).XValues = "=Data!$B$2:$B$" & LR
 
Upvote 0
Hey Thanks for the quick reply.. It create a single chart for the entire data. what i required is N number of chart for N set of data. eg. 2 separate chart for the sample data i have provided earlier..
Is that doable!!
 
Upvote 0
Possibly

Code:
LR = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR - 2
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).Name = "=""Time"""
    ActiveChart.SeriesCollection(1).Values = "=Data!$A$" & i & ":$A$" & i + 2
    ActiveChart.SeriesCollection(1).XValues = "=Data!$B$" & i & ":$B$" & i + 2
Next i
 
Upvote 0
Hey thanks! I'm able to get N number of charts. using the code but it runs every time when i go to that page.. I mean with out running code if i go to the page it runs automatically.. how to stop that
 
Upvote 0
i think when i click on that tab it refreshes the chart that i mistook as it is creating charts again and again sorry.., however but i'm getting more no.of charts than i expect.
I have 31 data rows and 1 label low(i mean column headers) but the code generates 20 carts.

i want 10 data lines per chart so it should give me 3 charts out of the 31 row data.
i.e. 2-11 --- Chart 1
12- 21 ---- chart 2
21 - 31----- chart 3

Here is the code i use...
Sub smpl()

LR = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR - 10
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""Time"""
ActiveChart.SeriesCollection(1).Values = "=Data!$D$" & i & ":$D$" & i + 10
ActiveChart.SeriesCollection(1).XValues = "=Data!$F$" & i & ":$F$" & i + 10
Next i
End Sub
 
Upvote 0
Sorry, that's my error. It should be

Rich (BB code):
For i = 2 To LR - 9 Step 10
 
Upvote 0
Kudos!!! Friend.. Its working perfect.. Thanks for all the support!!!.. I'm Just a beginner to macros, it would be really helpful if you direct me to get some basic notes on it...
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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