如何使用Rows而不是Series来绘制图形?

时间:2015-10-23 03:31:07

标签: excel vba excel-vba graph charts

我正在做一个用于绘制某些数据的VBA Excel代码,但即使指定按行绘制,225个数据系列的限制仍会显示,如何修复?

Y=1197
Set DChart = Charts.Add
   With DChart
      .ChartType = xlXYScatterSmoothNoMarkers
      .SetSourceData Source:=Sheets("Sheet1").Range("A2:" & "B" & Y), PlotBy:=xlRows
   End With

1 个答案:

答案 0 :(得分:0)

问题是您正在绘制此PlotBy:=xlRows,而您应该使用PlotBy:=xlColumns

因为 PlotBy:=xlRows,你有1196系列,即使它有效,它也不会与任何事情相关。使用 PlotBy:=xlColumns,您只有1个系列包含所有数据点。

我通常把它作为基础,我为你调整了第一个块,剩下的就是信息:

Sub Graph()

Dim Gr As Chart, _
    Sr As Series, _
    Src_Name As String

Src_Name = "Sheet1"
Set Gr = ActiveWorkbook.Charts.Add

With Gr
    '----Source Data Definition
    .SetSourceData Source:=Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(1197, 2)), PlotBy:=xlColumns
    '----Graph Type
    .ChartType = xlXYScatterSmoothNoMarkers
    '----Location/Placement
    .Location Where:=xlLocationAsNewSheet, Name:="NewSheetName"
    '----Title
    .HasTitle = True
    .ChartTitle.Characters.Text = "Chart Title"


    '----Data Series 1
    Set Sr = .SeriesCollection.NewSeries
    With Sr
        .Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5))
        .XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1))
        .AxisGroup = 1
        .Name = "MTTF"
    End With
    '----Data Series 2
    Set Sr = .SeriesCollection.NewSeries
    With Sr
        .Values = Range(Sheets(Src_Name).Cells(2, 2), Sheets(Src_Name).Cells(20, 5))
        .XValues = Range(Sheets(Src_Name).Cells(2, 1), Sheets(Src_Name).Cells(20, 1))
        .Name = "MTTR"
        '----Placing a Serie on Second axis
        .AxisGroup = 2
    End With
    '----Series' formats
    '.SeriesCollection(i).Delete
    '----For a line type chart
    '.SeriesCollection(i).Format.Line.Weight = 1
    '.SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer)
    '----For an area type chart
    '.SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(int1 as integer, int1 as integer, int3 as integer)

    '----Axis parameters
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Age"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Hours"
    .PlotArea.Interior.ColorIndex = 2
    .Axes(xlValue).MajorGridlines.Border.LineStyle = xlDot
    .ChartArea.Font.Size = 14
    .Deselect

    '----Legend positioning
    With .Legend
        .Left = 350
        .Top = 75
    End With
    '----Drawing area positiong
    With .PlotArea
        .Width = 550
        .Height = 350
    End With
End With

'----Free memory
Set Gr = Nothing
Set Sr = Nothing

End Sub