J
John Bigelow
Hi all. I am relatively new to using VBA to program Excel and have just
started to tinker with charts. I'm just at the stage where I'm tinkering,
trying to figure out how things work before trying anything serious, and in
the process I have come accross a very confusing situation.
I set up a very simple worksheet with the following dummy data in A1:C7
X Y Z
1 10 6
2 20 5
3 30 4
4 40 3
5 50 2
6 60 1
Then, I wrote two subroutines in VBA to try my hand at creating charts:
Public Sub Step1()
ThisWorkbook.Activate
Dim MyChart1 As Chart
Set MyChart1 = ThisWorkbook.Charts.Add
MyChart1.Name = "Chart1"
With MyChart1
.ChartType = xlLine
.SetSourceData _
Source:=Sheets("Sheet1").Range("A1:C7"), _
PlotBy:=xlColumns
.HasTitle = True
.ChartTitle.Text = "From Step 1"
End With
End Sub
Public Sub Step2()
ThisWorkbook.Activate
Dim MyChart2 As Chart
Set MyChart2 = ThisWorkbook.Charts.Add
MyChart2.Name = "Chart2"
With MyChart2
.ChartType = xlBubble
.SetSourceData _
Source:=Sheets("Sheet1").Range("A1:C7"), _
PlotBy:=xlColumns
.HasTitle = True
.ChartTitle.Text = "From Step 2"
End With
End Sub
If I run Step2() by itself, I get a run-time error 1004
"Method `ChartType' of object `_Chart' failed."
But, if I run Step1() first and then run Step(2), both subroutines run with
no problem. Step1 produces a line chart and Step2 produces a bubble chart.
In fact, both run with no problem if I run
Public Sub Combo()
Call Step1
Call Step2
End Sub
So my questions are: 1) What is wrong with Step2 that is not wrong with
Step1? They look pretty parallel to me. 2) Why does running Step1 first
make it possible for Step2 to run without an error? 3) How should I code
the creation of a chart to get consistently succesful results?
I would certainly be grateful for helpful suggestions on any or all of these
questions.
John
started to tinker with charts. I'm just at the stage where I'm tinkering,
trying to figure out how things work before trying anything serious, and in
the process I have come accross a very confusing situation.
I set up a very simple worksheet with the following dummy data in A1:C7
X Y Z
1 10 6
2 20 5
3 30 4
4 40 3
5 50 2
6 60 1
Then, I wrote two subroutines in VBA to try my hand at creating charts:
Public Sub Step1()
ThisWorkbook.Activate
Dim MyChart1 As Chart
Set MyChart1 = ThisWorkbook.Charts.Add
MyChart1.Name = "Chart1"
With MyChart1
.ChartType = xlLine
.SetSourceData _
Source:=Sheets("Sheet1").Range("A1:C7"), _
PlotBy:=xlColumns
.HasTitle = True
.ChartTitle.Text = "From Step 1"
End With
End Sub
Public Sub Step2()
ThisWorkbook.Activate
Dim MyChart2 As Chart
Set MyChart2 = ThisWorkbook.Charts.Add
MyChart2.Name = "Chart2"
With MyChart2
.ChartType = xlBubble
.SetSourceData _
Source:=Sheets("Sheet1").Range("A1:C7"), _
PlotBy:=xlColumns
.HasTitle = True
.ChartTitle.Text = "From Step 2"
End With
End Sub
If I run Step2() by itself, I get a run-time error 1004
"Method `ChartType' of object `_Chart' failed."
But, if I run Step1() first and then run Step(2), both subroutines run with
no problem. Step1 produces a line chart and Step2 produces a bubble chart.
In fact, both run with no problem if I run
Public Sub Combo()
Call Step1
Call Step2
End Sub
So my questions are: 1) What is wrong with Step2 that is not wrong with
Step1? They look pretty parallel to me. 2) Why does running Step1 first
make it possible for Step2 to run without an error? 3) How should I code
the creation of a chart to get consistently succesful results?
I would certainly be grateful for helpful suggestions on any or all of these
questions.
John