问题
我正在尝试用线性和对数刻度的数据以编程方式填充图表。出于某种原因,Axis.ScaleType 属性出错了。
尝试修复
我尝试将图表类型强制为 xyScatter。枚举xlLinear = xlScaleLinear
和xlLogarithmic = xlScaleLogarithmic
. 放置Axis.ScaleType = xlScaleLogarithmic
有效果,但在 时失败SeriesCollection.Count = 0
。
有什么建议么?一些谷歌搜索表明这是一个相对常见的问题。
Option Explicit
Private Type xySeries
Name As String
X As Range
Y As Range
Markers As XlMarkerStyle
Axis As XlAxisGroup
Color As XlColorIndex
Weight As Single
End Type
Private Type xyAxis
NumberFormat As String
ScaleType As XlScaleType
Minimum As Double
Maximum As Double
End Type
'Pass this function the chartsheet name, and and array of data for the series of custom xySeries type
Private Sub ChartData(sCSName As String, asPlots() As xySeries, XAxis As xyAxis, YAxis() As xyAxis)
Dim cs As Chart, oSeries As Series, iPlot As Integer
Dim sPlot As String, sAlias As String, iAlias As Integer
'Dim rgTime As Range, rgValues As Range
Set cs = ThisWorkbook.Charts(sCSName)
For Each oSeries In cs.SeriesCollection
oSeries.Delete
Next
cs.PlotVisibleOnly = False
cs.ChartType = xlXYScatterLines
With ThisWorkbook.Sheets("LimeData")
For iPlot = 1 To UBound(asPlots, 1)
cs.SeriesCollection.NewSeries
cs.SeriesCollection(iPlot).ChartType = xlXYScatterLines
cs.SeriesCollection(iPlot).Name = asPlots(iPlot).Name
cs.SeriesCollection(iPlot).XValues = asPlots(iPlot).X
cs.SeriesCollection(iPlot).values = asPlots(iPlot).Y
cs.SeriesCollection(iPlot).MarkerStyle = asPlots(iPlot).Markers
cs.SeriesCollection(iPlot).AxisGroup = asPlots(iPlot).Axis
cs.SeriesCollection(iPlot).Format.Line.Weight = asPlots(iPlot).Weight
Next iPlot
End With
With cs
With .Axes(xlCategory)
.MinimumScale = RangeNominalExtreme(asPlots(1).X, 0)
.MaximumScale = RangeNominalExtreme(asPlots(1).X, 1)
.TickLabels.NumberFormat = XAxis.NumberFormat
'Error occurs on the next line
.ScaleType = XAxis.ScaleType
'Run-Time Error '-2147467259' (80004002):
'Method 'ScaleType' of 'Axis' failed
End With
.Axes(xlValue, xlPrimary).ScaleType = YAxis(1).ScaleType
.Axes(xlValue, xlSecondary).ScaleType = YAxis(2).ScaleType
End With
End Sub