Excel VBA将Chartsheet Axis设置为Logarithmic

时间:2013-03-27 22:13:49

标签: excel-vba charts scale vba excel

问题

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

尝试修复

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

3 个答案:

答案 0 :(得分:1)

问题很可能是由于存在辅助Y轴(从您的行.Axes(xlValue, xlSecondary).ScaleType = YAxis(2).ScaleType猜到)。如果你可以放心,你可以继续。

请参阅Excel VBA chart axis error: "Method 'ScaleType' of object 'Axis' failed" when reading `.ScaleType`

答案 1 :(得分:0)

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

答案 2 :(得分:0)

.SeriesCollection.Count = 0时,唯一的图表元素是图表区域。没有绘图区域,没有系列,没有轴,没有。因此,在图表中有数据之前,您无法将轴格式应用于轴。