PC Review


Reply
Thread Tools Rate Thread

Chart Error when using Chart Line - Column on 2 Axes in vba code

 
 
=?Utf-8?B?Sm9obg==?=
Guest
Posts: n/a
 
      30th Jul 2007
I am trying to use the custom chart, Line - column on 2 axes in vba code. I
couldn't get it to work so I tried using the Macro recoder. The following is
a macro that was recorded. However when I run it after it has been recorded I
get the following Error message, "RunTime Error 1004, Methods " 'Axes of
object - chart failed".

Is there some way that I can create this chart type on the fly using VBA? I
have a routine that creates a chart, "Chart Type, Column" with no problems.

'Macro recorded routine
Charts.Add
ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
"Lines on 2 Axes"
ActiveChart.SetSourceData Source:=Sheets("Data").Range("B3761"),
PlotBy:= _
xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Data"
With ActiveChart
.HasTitle = False
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
.Axes(xlCategory, xlSecondary).HasTitle = False
.Axes(xlValue, xlSecondary).HasTitle = False
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom
End Sub

 
Reply With Quote
 
 
 
 
Jon Peltier
Guest
Posts: n/a
 
      3rd Aug 2007
I answered your later post, but for the archives I'll give a simple answer
here.

The macro recorder sometimes puts statements in the wrong order. You should
apply the chart type after the chart has been populated with data. In the
corrected macro below, note the new position of the ApplyCustomType comand.

Sub DoChart()
Charts.Add
ActiveChart.SetSourceData _
Source:=Sheets("Data").Range("B3761"), PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:="Data"

ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
"Line - Column on 2 Axes"

With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = False
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom
With Selection.Interior
.ColorIndex = 36
.Pattern = 1
End With
End Sub

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



"John" <(E-Mail Removed)> wrote in message
news:14CC2D0E-6C4E-4A67-8E88-(E-Mail Removed)...
>I am trying to use the custom chart, Line - column on 2 axes in vba code. I
> couldn't get it to work so I tried using the Macro recoder. The following
> is
> a macro that was recorded. However when I run it after it has been
> recorded I
> get the following Error message, "RunTime Error 1004, Methods " 'Axes of
> object - chart failed".
>
> Is there some way that I can create this chart type on the fly using VBA?
> I
> have a routine that creates a chart, "Chart Type, Column" with no
> problems.
>
> 'Macro recorded routine
> Charts.Add
> ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
> "Lines on 2 Axes"
> ActiveChart.SetSourceData Source:=Sheets("Data").Range("B3761"),
> PlotBy:= _
> xlColumns
> ActiveChart.Location Where:=xlLocationAsObject, Name:="Data"
> With ActiveChart
> .HasTitle = False
> .Axes(xlCategory, xlPrimary).HasTitle = False
> .Axes(xlValue, xlPrimary).HasTitle = False
> .Axes(xlCategory, xlSecondary).HasTitle = False
> .Axes(xlValue, xlSecondary).HasTitle = False
> End With
> With ActiveChart.Axes(xlCategory)
> .HasMajorGridlines = False
> .HasMinorGridlines = False
> End With
> With ActiveChart.Axes(xlValue)
> .HasMajorGridlines = True
> .HasMinorGridlines = False
> End With
> ActiveChart.HasLegend = True
> ActiveChart.Legend.Select
> Selection.Position = xlBottom
> End Sub
>



 
Reply With Quote
 
=?Utf-8?B?Sm9oblRSZWVk?=
Guest
Posts: n/a
 
      3rd Aug 2007
Thank you for your responce I will try it out but your answer does make sense.

"Jon Peltier" wrote:

> I answered your later post, but for the archives I'll give a simple answer
> here.
>
> The macro recorder sometimes puts statements in the wrong order. You should
> apply the chart type after the chart has been populated with data. In the
> corrected macro below, note the new position of the ApplyCustomType comand.
>
> Sub DoChart()
> Charts.Add
> ActiveChart.SetSourceData _
> Source:=Sheets("Data").Range("B3761"), PlotBy:=xlColumns
> ActiveChart.Location Where:=xlLocationAsObject, Name:="Data"
>
> ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
> "Line - Column on 2 Axes"
>
> With ActiveChart.Axes(xlCategory)
> .HasMajorGridlines = False
> .HasMinorGridlines = False
> End With
> With ActiveChart.Axes(xlValue)
> .HasMajorGridlines = True
> .HasMinorGridlines = False
> End With
> ActiveChart.HasLegend = True
> ActiveChart.Legend.Select
> Selection.Position = xlBottom
> With Selection.Interior
> .ColorIndex = 36
> .Pattern = 1
> End With
> End Sub
>
> - Jon
> -------
> Jon Peltier, Microsoft Excel MVP
> Tutorials and Custom Solutions
> Peltier Technical Services, Inc. - http://PeltierTech.com
> _______
>
>
>
> "John" <(E-Mail Removed)> wrote in message
> news:14CC2D0E-6C4E-4A67-8E88-(E-Mail Removed)...
> >I am trying to use the custom chart, Line - column on 2 axes in vba code. I
> > couldn't get it to work so I tried using the Macro recoder. The following
> > is
> > a macro that was recorded. However when I run it after it has been
> > recorded I
> > get the following Error message, "RunTime Error 1004, Methods " 'Axes of
> > object - chart failed".
> >
> > Is there some way that I can create this chart type on the fly using VBA?
> > I
> > have a routine that creates a chart, "Chart Type, Column" with no
> > problems.
> >
> > 'Macro recorded routine
> > Charts.Add
> > ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
> > "Lines on 2 Axes"
> > ActiveChart.SetSourceData Source:=Sheets("Data").Range("B3761"),
> > PlotBy:= _
> > xlColumns
> > ActiveChart.Location Where:=xlLocationAsObject, Name:="Data"
> > With ActiveChart
> > .HasTitle = False
> > .Axes(xlCategory, xlPrimary).HasTitle = False
> > .Axes(xlValue, xlPrimary).HasTitle = False
> > .Axes(xlCategory, xlSecondary).HasTitle = False
> > .Axes(xlValue, xlSecondary).HasTitle = False
> > End With
> > With ActiveChart.Axes(xlCategory)
> > .HasMajorGridlines = False
> > .HasMinorGridlines = False
> > End With
> > With ActiveChart.Axes(xlValue)
> > .HasMajorGridlines = True
> > .HasMinorGridlines = False
> > End With
> > ActiveChart.HasLegend = True
> > ActiveChart.Legend.Select
> > Selection.Position = xlBottom
> > End Sub
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Column-line chart on two axes Mary Ann Microsoft Excel Charting 2 6th Oct 2008 07:18 PM
Combination chart - Line - Column on 2 Axes =?Utf-8?B?RGViYmll?= Microsoft Excel Charting 2 16th Nov 2006 09:04 PM
chart: line-column with 2 Y axes =?Utf-8?B?R3JpZmY=?= Microsoft Access Reports 0 19th Aug 2006 01:51 PM
Line - Column 2 Axes chart question =?Utf-8?B?bWt0Z0B2enc=?= Microsoft Excel Charting 1 14th Mar 2006 09:22 PM
Line - Column Chart on 2 Axes Valerie Microsoft Powerpoint 1 17th Sep 2004 11:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:02 PM.