Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 Axis Charts with VB in Excel 1

Status
Not open for further replies.

DreamerZ

Programmer
Jul 11, 2001
254
US
I have recorded the following VBA below. No errors in recording. Upon running the macro, however, the code errors on the bolded lines:
"Method Axes of Object_Chart failed" is the error
Code:
Sub view_MeasurementCharts()
    
    Charts.Add
    ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
        "Line - Column on 2 Axes"
    ActiveChart.SetSourceData Source:=Sheets("MeasurementData").Range( _
        "D1:E3,M1:M3"), PlotBy:=xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet5"
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "" '//Chart Title
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "" 'Left Axis
Title
        [b].Axes(xlCategory, xlSecondary).HasTitle = False
        .Axes(xlValue, xlSecondary).HasTitle = True
        .Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "" 'Right
Axis Title[/b]
    End With
    ActiveChart.HasLegend = True
    ActiveChart.Legend.Select
    Selection.Position = xlBottom
    [b]ActiveChart.Axes(xlValue, xlSecondary).Select
    With ActiveChart.Axes(xlValue, xlSecondary)[/b]
        .MinimumScale = 0
        .MaximumScale = 25
        .MinorUnitIsAuto = True
        .MajorUnit = 5
        .Crosses = xlCustom
        .CrossesAt = 0
        .ReversePlotOrder = False
        .ScaleType = xlLinear
        .DisplayUnit = xlNone
    End With
    Selection.TickLabels.NumberFormat = "0"
    ActiveChart.Axes(xlValue).Select
    With ActiveChart.Axes(xlValue)
        .MinimumScale = 175
        .MaximumScale = 210
        .MinorUnitIsAuto = True
        .MajorUnitIsAuto = True
        .Crosses = xlCustom
        .CrossesAt = 0
        .ReversePlotOrder = False
        .ScaleType = xlLinear
        .DisplayUnit = xlNone
    End With
End Sub
It's not setting up the secondary axis properly...basically, not at all even
though it recorded it. xlSecondary is a keyword for the Axes Method.
Something else I have to do to get the secondary axis working?

Thx,


DreamerZ
simplesolutions@prodigy.net
[ignore][/ignore]
 
D,

1. Can't start with a split range -- add it later in code
2. starts out as a Chart object and becomes a ChartObject object when it's location is made a sheet (BTW I changed sheet names so I could run)

check out this modified code. you will need to tweek...
Code:
Sub view_MeasurementCharts()
    Dim ch1 As ChartObject
    Charts.Add
    ActiveChart.ApplyCustomType ChartType:=xlBuiltIn, TypeName:= _
        "Line - Column on 2 Axes"
    ActiveChart.SetSourceData Source:=Sheet1.Range( _
        "D1:E3"), PlotBy:=xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet2"
    Set ch1 = ActiveSheet.ChartObjects(ActiveSheet.ChartObjects.Count)
    With ch1.Chart
        .HasTitle = True
        .ChartTitle.Characters.Text = "" '//Chart Title
        .Axes(xlCategory, xlPrimary).HasTitle = False
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "" 'Left Axis Title
        
        Sheet1.[M1:M3].Copy
        .SeriesCollection.Paste Rowcol:=xlColumns, SeriesLabels:=False, _
            CategoryLabels:=False, Replace:=False, NewSeries:=True
        .SeriesCollection(2).AxisGroup = 2
        .SeriesCollection(2).ChartType = xlLine
        
        .Axes(xlCategory, xlSecondary).HasTitle = False
        .Axes(xlValue, xlSecondary).HasTitle = True
        .Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "" 'Right Axis Title
    End With
    ActiveChart.HasLegend = True
    ActiveChart.Legend.Select
    Selection.Position = xlBottom
    ActiveChart.Axes(xlValue, xlSecondary).Select
    With ActiveChart.Axes(xlValue, xlSecondary)
        .MinimumScale = 0
        .MaximumScale = 25
        .MinorUnitIsAuto = True
        .MajorUnit = 5
        .Crosses = xlCustom
        .CrossesAt = 0
        .ReversePlotOrder = False
        .ScaleType = xlLinear
        .DisplayUnit = xlNone
    End With
    Selection.TickLabels.NumberFormat = "0"
    ActiveChart.Axes(xlValue).Select
    With ActiveChart.Axes(xlValue)
        .MinimumScale = 175
        .MaximumScale = 210
        .MinorUnitIsAuto = True
        .MajorUnitIsAuto = True
        .Crosses = xlCustom
        .CrossesAt = 0
        .ReversePlotOrder = False
        .ScaleType = xlLinear
        .DisplayUnit = xlNone
    End With
End Sub
:)


Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top