LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Novice's Question re Chart Programming

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


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming question Kelly Excel Programming 1 March 13th 06 07:03 PM
Programming Question leafsfan1967 Excel Programming 7 June 17th 05 08:56 PM
VBA and F2 programming question nl_fan[_3_] Excel Programming 0 October 1st 04 05:00 PM
VBA and F2 programming question nl_fan[_2_] Excel Programming 0 September 29th 04 04:53 PM
Programming Question Mac Lingo[_2_] Excel Programming 3 August 7th 04 06:56 PM


All times are GMT +1. The time now is 11:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"