aquarules
aquarules

Reputation: 15

VBA chart error Method 'Axes' of Object '_Chart' failed for secondary axis title

I am trying to add a secondary vertical axis to my chart in VBA. I keep running into this error that says that the Method Axes of the Object _Chart has failed.

I have looked up solutions and implemented a few that stated to make sure that the secondary axis is first active. My code is below. The error occurs in the third to last line when I am trying to state that the secondary axis has a title. Any help is much appreciated since I am still a beginner in VBA.

Public Sub CreateChartForColumnsOneThreeandFive()

    Dim myChartColumnsOneThreeandFive As ChartObject

    Set myChartColumnsOneThreeandFive = ActiveSheet.ChartObjects.Add(Left:=150, Top:=150, Width:=500, Height:=400)

    myChartColumnsOneThreeandFive.Chart.HasTitle = True

    myChartColumnsOneThreeandFive.Chart.ChartTitle.Text = "Phase Detector Readback vs Substrate Forward Power"

    myChartColumnsOneThreeandFive.Chart.Type = xlLine

    myChartColumnsOneThreeandFive.Chart.SetSourceData Source:=ActiveWorkbook.Sheets("Sheet2").Range("C" & processRowBegin & ":C" & processRowEnd)

    myChartColumnsOneThreeandFive.Chart.SetSourceData Source:=ActiveWorkbook.Sheets("Sheet2").Range("E" & processRowBegin & ":E" & processRowEnd)

    myChartColumnsOneThreeandFive.Chart.SeriesCollection(1).Name = Range("C1")

    myChartColumnsOneThreeandFive.Chart.SeriesCollection(1).Name = Range("E1")

    myChartColumnsOneThreeandFive.Chart.SeriesCollection(1).Select

    myChartColumnsOneThreeandFive.Chart.SeriesCollection(1).AxisGroup = 2

    myChartColumnsOneThreeandFive.Chart.HasTitle = True

    myChartColumnsOneThreeandFive.Chart.ChartTitle.Text = "Substrate Forward Power vs Phase Detector Readback"

    myChartColumnsOneThreeandFive.Chart.Axes(xlCategory).HasTitle = True

    myChartColumnsOneThreeandFive.Chart.Axes(xlCategory).AxisTitle.Caption = "Time"

    myChartColumnsOneThreeandFive.Chart.Axes(xlValue, xlPrimary).HasTitle = True

    myChartColumnsOneThreeandFive.Chart.Axes(xlValue, xlPrimary).AxisTitle.Caption = "Substrate Forward Power"

      myChartColumnsOneThreeandFive.Chart.SeriesCollection(1).AxisGroup = xlSecondary

     myChartColumnsOneThreeandFive.Chart.HasAxis(xlValue, xlSecondary) = True

    myChartColumnsOneThreeandFive.Chart.Axes(xlValue, xlSecondary).HasTitle = True

    myChartColumnsOneThreeandFive.Chart.Axes(xlValue, xlSecondary).AxisTitle.Select

    myChartColumnsOneThreeandFive.Chart.Axes(xlValue, xlSecondary).AxisTitle.Text = _
        "Phase Detector Readback"

End Sub

Upvotes: 1

Views: 4026

Answers (2)

Patrick Mirek
Patrick Mirek

Reputation: 13

I ran into this issue and solved it by changing the order of my code. Here is the order that worked:

  1. Delete previous chart series (optional)
  2. Set chart type
  3. Add primary y-axis series
  4. Add secondary y-axis series
  5. Format secondary y-axis
  6. Format x axis
  7. Format primary y-axis

I think what solved it was setting the chart type right near the beginning. There's definetly some flexibility in the order I've written here, but I haven't explored it.

Upvotes: 0

Shai Rado
Shai Rado

Reputation: 33672

Even though not in this code, I assume processRowBegin and processRowEnd are defined somewhere else as Long and they have a numeric value.

Try the code below, it runs without errors, I am not sure what is your final goal and how your chart suppose to look like, but I think you can modify it easily to fit your needs.

Option Explicit

Public Sub CreateChartForColumnsOneThreeandFive()

Dim myChartColumnsOneThreeandFive As ChartObject 

Set myChartColumnsOneThreeandFive = ActiveSheet.ChartObjects.Add(Left:=150, Top:=150, Width:=500, Height:=400)

With myChartColumnsOneThreeandFive.Chart
    .HasTitle = True
    .ChartTitle.Text = "Phase Detector Readback vs Substrate Forward Power"
    .Type = xlLine

    ' create series 1, set values and name
    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = Range("C1")
    .SeriesCollection(1).Values = ActiveWorkbook.Sheets("Sheet2").Range("C" & processRowBegin & ":C" & processRowEnd)

   ' create series 2, set values and name
    .SeriesCollection.NewSeries
    .SeriesCollection(2).Name = Range("E1")
    .SeriesCollection(2).Values = ActiveWorkbook.Sheets("Sheet2").Range("E" & processRowBegin & ":E" & processRowEnd)

    .SeriesCollection(1).AxisGroup = 2
    .HasTitle = True
    .ChartTitle.Text = "Substrate Forward Power vs Phase Detector Readback"

    ' set X-axis
    .Axes(xlCategory).HasTitle = True
    .Axes(xlCategory).AxisTitle.Caption = "Time"

    ' set Y-axis
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Caption = "Substrate Forward Power"

    .SeriesCollection(1).AxisGroup = xlSecondary

    ' add a secondary Y-axis ans set it
    .HasAxis(xlValue, xlSecondary) = True
    .Axes(xlValue, xlSecondary).HasTitle = True
    .Axes(xlValue, xlSecondary).AxisTitle.Select
    .Axes(xlValue, xlSecondary).AxisTitle.Text = "Phase Detector Readback"
End With

End Sub

Upvotes: 1

Related Questions