我正在尝试在VBA中向我的图表添加辅助垂直轴。我一直遇到这个错误,指出对象Axes
的方法_Chart
失败了。
我查找了解决方案并实施了一些说明,以确保辅助轴首先处于活动状态。我的代码如下。当我试图声明辅助轴具有标题时,错误发生在第三行到最后一行。任何帮助都非常感谢,因为我还是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
答案 0 :(得分:1)
即使不在此代码中,我也认为processRowBegin
和processRowEnd
在其他地方被定义为Long
,并且它们具有数值。
尝试下面的代码,它运行没有错误,我不确定你的最终目标是什么以及你的图表看起来如何,但我认为你可以轻松修改它以满足你的需求。
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
答案 1 :(得分:0)
我遇到了这个问题,并通过更改代码的顺序解决了该问题。这是有效的顺序:
我认为解决此问题的方法是将图表类型设置在开头附近。我在这里编写的顺序中肯定有一定的灵活性,但我没有对此进行探讨。