Vba Excel Macro Create Graph With Dynamic Range
Vba Excel Macro Create Graph With Dynamic Range
I'm trying to create a macro where a graph is created with a dynamic range for the columns. Here is what I have so far.
With Sheets("Sheet1")
Charts.Add
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=.Range("D2", Cells(2, N + 3))
ActiveChart.Location xlLocationAsObject
End With
End Sub
Right now however, I keep encountering a run time error with the statement-
"The specified dimension is not valid for the current chart type"
ActiveChart.Location xlLocationAsObject
FOLLOW UP
Dim N As Long
Private Sub CommandButton1_Click()
Dim x As Integer
Dim y As Integer
x = 0
y = 1
For i = 4 To N + 3
x = x + y
Cells(1, i) = x
Next i
End Sub
1 What is N? What range of data are you planning to plot? – EvenPrime Aug 14 '14 at 17:37
@Thinkingcap sorry totally spaced. Modified the question – DeeWBee Aug 14 '14 at 17:41
1 Answer
xlLocationAsObject requires where parameter ,specifying where you want the chart object to be embedded or placed
Replace
1
ActiveChart.Location xlLocationAsObject
with
https://stackoverflow.com/questions/25313983/vba-excel-macro-created-graph-with-dynamic-range 1/2
6/22/2019 VBA Excel: Macro-created graph with dynamic range - Stack Overflow
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
https://stackoverflow.com/questions/25313983/vba-excel-macro-created-graph-with-dynamic-range 2/2