XY散点图 - 使用VBA格式化4个轴 - #ERROR

时间:2017-02-23 08:21:05

标签: excel-vba charts excel-charts vba excel

我正在编写一个代码来绘制一个带有4个轴的xy散点图。当我使用VBA对图表的辅助x轴'顺序进行反转以及将交叉点设置为最小值时,我总是会出错。

我可以手动或使用录制的宏来完成,但不能使用vba代码。

Sub WashabilityChart()

Dim SFChart As Object
Dim NumRows As Integer
Dim SGmin, SGmax As Double
Dim i As Integer
Dim n As Long
Dim NGWt() As Double
Dim NGValues() As Double

Sheets("Data Table").Select

NumRows = Sheets("Data Table").Range("D4", Range("D4").End(xlDown)).Rows.Count
SGmin = Application.WorksheetFunction.Min(Range("D4", "D" & 4 + NumRows))
SGmax = Application.WorksheetFunction.Max(Range("D4", "D" & 4 + NumRows))

ReDim NGWt(NumRows, 0)
ReDim NGValues(NumRows, 0)

For i = 1 To NumRows - 2
    NGWt(i, 0) = Sayfa1.Range("L" & i + 4)
    NGValues(i, 0) = 100 - NGWt(i, 0)
Next i

Set SFChart = Charts.Add

With SFChart
    .ChartType = xlXYScatterSmooth
    .PlotArea.Interior.ColorIndex = 2
    .HasAxis(xlCategory, xlPrimary) = True
    .HasAxis(xlCategory, xlSecondary) = True
    .HasAxis(xlValue, xlPrimary) = True
    .HasAxis(xlValue, xlSecondary) = True

    With .Axes(xlCategory, xlPrimary)
        .HasTitle = True
        .AxisTitle.Characters.Text = "Ash %"
        .MaximumScale = 100
        .MinimumScale = 0
        .MajorUnit = 10
        .MinorUnit = 5
        .HasMajorGridlines = True
        .HasMinorGridlines = True
    End With

    With .Axes(xlValue, xlPrimary)
        .ReversePlotOrder = True
        .Crosses = xlMaximum
        .MaximumScale = 100
        .MinimumScale = 0
        .MajorUnit = 10
        .MinorUnit = 5
        .HasMajorGridlines = True
        .HasMinorGridlines = True
    End With

    For n = .SeriesCollection.Count To 1 Step -1
        .SeriesCollection(n).Delete
    Next n

    .SeriesCollection.NewSeries
    With .SeriesCollection(1)
        .AxisGroup = xlPrimary
        .XValues = Sayfa1.Range("H4", "H" & 4 + NumRows)
        .Values = Sayfa1.Range("G4", "G" & 4 + NumRows)
    End With

    .SeriesCollection.NewSeries
    With .SeriesCollection(2)
        .AxisGroup = xlPrimary
        .XValues = Sayfa1.Range("J4", "J" & 4 + NumRows)
        .Values = Sayfa1.Range("G4", "G" & 4 + NumRows)
    End With

    .SeriesCollection.NewSeries
    With .SeriesCollection(3)
        .AxisGroup = xlPrimary
        .XValues = Sayfa1.Range("N4", "N" & 4 + NumRows)
        .Values = Sayfa1.Range("M4", "M" & 4 + NumRows)
    End With

    .SeriesCollection.NewSeries
    With .SeriesCollection(4)
        .AxisGroup = xlSecondary
        .XValues = Sayfa1.Range("D4", "D" & 4 + NumRows)
        .Values = Sayfa1.Range("I4", "I" & 4 + NumRows)
    End With

    .SeriesCollection.NewSeries
    With .SeriesCollection(5)
        .AxisGroup = xlSecondary
        .XValues = Sayfa1.Range("I5", "I" & 3 + NumRows)
        .Values = NGValues
    End With

    With .Axes(xlValue, xlSecondary)
        .MaximumScale = 100
        .MinimumScale = 0
        .MajorUnit = 10
        .MinorUnit = 5
    End With

    With .Axes(xlCategory, xlSecondary)
        If SGmin >= 1.5 Then
            .MinimumScale = 1.5
            .MaximumScale = 2.5
        Else
            .MinimumScale = 1
            .MaximumScale = 2
        End If
        .MajorUnit = 0.1
        .MinorUnit = 0.05
        .HasMajorGridlines = True
        .HasMinorGridlines = True
    End With

    With .Axes(xlCategory, xlSecondary)
        .**ReversePlotOrder = True**
    End With
    With .Axes(xlCategory, xlSecondary)
        **.Crosses = xlMinimum**
    End With
    With .Axes(xlValue, xlSecondary)
        .Crosses = xlMaximum
    End With
End With

End Sub

0 个答案:

没有答案