2

问题

我正在尝试用线性和对数刻度的数据以编程方式填充图表。出于某种原因,Axis.ScaleType 属性出错了。

尝试修复

我尝试将图表类型强制为 xyScatter。枚举xlLinear = xlScaleLinearxlLogarithmic = 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
4

4 回答 4

1

查看:

SetElement(msoElementPrimaryCategoryAxisLogScale)

http://msdn.microsoft.com/en-us/library/office/ff864118.aspx

于 2013-12-16T17:32:54.860 回答
1

问题很可能是由于存在辅助 Y 轴(从您的线猜测.Axes(xlValue, xlSecondary).ScaleType = YAxis(2).ScaleType)。如果你能放过它,你可能会继续前进。

阅读 `.ScaleType` 时,请参阅Excel VBA 图表轴错误:“对象 'Axis' 的方法 'ScaleType' 失败”

于 2014-11-26T02:49:07.373 回答
0

我在ScaleType属性帮助中看到的是它“仅适用于值轴。”。但是您将轴设置为xlCategory而不是xlValue. 如果没有您拥有的数据,我无法检查它,但这可能是您问题的答案。

于 2013-03-27T23:02:24.223 回答
0

当 时.SeriesCollection.Count = 0,唯一的图表元素是图表区域。没有情节区域,没有系列,没有轴,什么都没有。因此,在图表中有数据之前,您不能将轴格式应用于轴。

于 2013-04-01T14:28:29.887 回答