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.
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
