Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Thief_
 
Posts: n/a
Default Creating a new chart- why Set NewChart = Charts.Add doesn't work?

I've got the following code:

Charts.Add
Set NewChart = Charts.Add
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

The code fails at the line ".HasTitle = True". Viewing the NewChart object
in the Watch window show the object has NO properties, but I don't
understand why since it is dimmed as a chart object. The only way to make
this code work is to change it to:

Charts.Add
Set NewChart = ActiveChart
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

Can someone explain why this is so?


--
|
+-- Thief_
|


  #2   Report Post  
Thief_
 
Posts: n/a
Default

Sorry, it seems that the line:

NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"

which moves the chart to a worksheet as an object, kills all the chart's
properties- this is so confusing!

--
|
+-- Thief_
|

"Thief_" wrote in message
...
I've got the following code:

Charts.Add
Set NewChart = Charts.Add
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

The code fails at the line ".HasTitle = True". Viewing the NewChart object
in the Watch window show the object has NO properties, but I don't
understand why since it is dimmed as a chart object. The only way to make
this code work is to change it to:

Charts.Add
Set NewChart = ActiveChart
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

Can someone explain why this is so?


--
|
+-- Thief_
|




  #3   Report Post  
Tushar Mehta
 
Posts: n/a
Default

No, it doesn't 'kill' the chart's properties. What happens is that
when the chart is moved to a worksheet, the object to which NewChart
points no longer exists. The correct way to relocate the chart and
continue to have a reference to it is to reestablish the reference to
the new object. Use

Set NewChart=NewChart.Location ( _
Whe=xlLocationAsObject, Name:="GraphResults")

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Sorry, it seems that the line:

NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"

which moves the chart to a worksheet as an object, kills all the chart's
properties- this is so confusing!


  #4   Report Post  
Jon Peltier
 
Posts: n/a
Default

Or, as I try to encourage people to do, create it as a chart object to
begin with.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______


Tushar Mehta wrote:
No, it doesn't 'kill' the chart's properties. What happens is that
when the chart is moved to a worksheet, the object to which NewChart
points no longer exists. The correct way to relocate the chart and
continue to have a reference to it is to reestablish the reference to
the new object. Use

Set NewChart=NewChart.Location ( _
Whe=xlLocationAsObject, Name:="GraphResults")

  #5   Report Post  
Tushar Mehta
 
Posts: n/a
Default

Yeah, I know you like to start with a chartobject. I don't. Except in
some specific circumstances, I prefer to let XL decide on the dimensions
of the chartobject based on the current window size.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2005

In article ,
says...
Or, as I try to encourage people to do, create it as a chart object to
begin with.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______


Tushar Mehta wrote:
No, it doesn't 'kill' the chart's properties. What happens is that
when the chart is moved to a worksheet, the object to which NewChart
points no longer exists. The correct way to relocate the chart and
continue to have a reference to it is to reestablish the reference to
the new object. Use

Set NewChart=NewChart.Location ( _
Whe=xlLocationAsObject, Name:="GraphResults")




  #6   Report Post  
Jon Peltier
 
Posts: n/a
Default

Hi Tushar -

That's fine if it's going to be an on-screen thing, but if it's intended for a
printed report, it's better to size it yourself. Besides, my retentive nature likes
charts that line up with the cell boundaries.

You can approximate the default Excel size if you make the chart half as wide and
tall as the active pane's usable size (subject to certain minimum values, of
course), centered within the range appearing in that pane.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

Tushar Mehta wrote:

Yeah, I know you like to start with a chartobject. I don't. Except in
some specific circumstances, I prefer to let XL decide on the dimensions
of the chartobject based on the current window size.


  #7   Report Post  
Andy Pope
 
Posts: n/a
Default

Hi,

I think it's that the NewChart object is a Chart when it's a chart sheet
but moving to the worksheet means its now a ChartObject, which has a
Chart reference. The variable does not automatically adjust itself.

Either format the chart and then move it or re reference NewChart after
you move it.

Set NewChart = Charts.Add
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
Set NewChart = ActiveChart
With NewChart
.ChartType = xlLineMarkers
.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

Cheers
Andy

Thief_ wrote:
I've got the following code:

Charts.Add
Set NewChart = Charts.Add
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

The code fails at the line ".HasTitle = True". Viewing the NewChart object
in the Watch window show the object has NO properties, but I don't
understand why since it is dimmed as a chart object. The only way to make
this code work is to change it to:

Charts.Add
Set NewChart = ActiveChart
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

Can someone explain why this is so?



--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
  #8   Report Post  
Jon Peltier
 
Posts: n/a
Default

Here's a simpler way. Make the chart object directly in the worksheet, without the
chart sheet as an intermediate step:

Set NewChart = ActiveSheet.ChartObjects.Add(100, 100, 300, 250).Chart

where the numbers in parentheses are the left, top, width, and height of the chart
object, in points.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services
Tutorials and Custom Solutions
http://PeltierTech.com/
_______

Thief_ wrote:

I've got the following code:

Charts.Add
Set NewChart = Charts.Add
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

The code fails at the line ".HasTitle = True". Viewing the NewChart object
in the Watch window show the object has NO properties, but I don't
understand why since it is dimmed as a chart object. The only way to make
this code work is to change it to:

Charts.Add
Set NewChart = ActiveChart
NewChart.ChartType = xlLineMarkers
NewChart.SetSourceData Source:=rng2Plot, PlotBy:=xlRows
NewChart.Location Whe=xlLocationAsObject, Name:="GraphResults"
With NewChart
.HasTitle = True
.ChartTitle.Characters.Text = Title
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Num Errors"
End With

Can someone explain why this is so?



Reply
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
Skip some columns when creating a chart MikeJ Charts and Charting in Excel 3 April 15th 05 12:15 AM
Creating a pie chart Val Charts and Charting in Excel 1 January 20th 05 07:12 PM
creating an x,y chart smintey Charts and Charting in Excel 2 December 16th 04 11:11 PM
Creating a dynamic chart Fysh Charts and Charting in Excel 9 December 15th 04 02:52 AM
Creating an x,y coordinate chart smintey Charts and Charting in Excel 2 December 9th 04 04:01 PM


All times are GMT +1. The time now is 03:32 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"