Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

6/22/2019 VBA Excel: Macro-created graph with dynamic range - Stack Overflow

VBA Excel: Macro-created 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.

0 Private Sub CommandButton2_Click()

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"

When I look in the code, it keeps referencing the last line.

ActiveChart.Location xlLocationAsObject

Any ideas? Thanks

FOLLOW UP

Here is the code referencing N

Dim N As Long
Private Sub CommandButton1_Click()

Dim x As Integer
Dim y As Integer
x = 0
y = 1

N = Application.InputBox(Prompt:="Enter value", Type:=1)


If N > Columns.Count Then
N = Columns.Count
Else
For i = 4 To 9999
Cells(1, i).ClearContents
Cells(3, i).ClearContents
Next i
End If

For i = 4 To N + 3
x = x + y
Cells(1, i) = x
Next i

End Sub

excel vba excel-vba

edited Feb 9 '15 at 22:46 asked Aug 14 '14 at 17:24


pnuts DeeWBee
50.1k 7 64 102 380 1 6 27

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

ActiveChart.Location xlLocationAsObject, Name

to embed the chart in Sheet1

See all available Options


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 1/2
6/22/2019 VBA Excel: Macro-created graph with dynamic range - Stack Overflow

answered Aug 14 '14 at 18:24


EvenPrime
3,349 3 31 54

oh jeez...I'm an idiot...thanks so much! – DeeWBee Aug 14 '14 at 18:26

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

You might also like