Excel VBA:由不同系列

时间:2017-04-20 11:57:36

标签: vba excel-vba histogram excel

我正在尝试在VBA中完成一个简单的任务:获取由不同系列组成的直方图。 作为" XValues"我有字符串。 作为"价值观"我有漂浮物。

问题是,当我向xlColumnClustered图表的SeriesCollection添加新系列时,它不考虑" XValues"并从左上角自动添加数据(列),并将不同系列的数据(列)混合。这不是在前一系列的数据之后添加新系列的数据。

我使用的代码如下:

Dim MyEmbeddedChart As Chart
Set MyEmbeddedChart = ActiveSheet.Shapes.AddChart.Chart
With MyEmbeddedChart

    'Data
    .ChartType = xlColumnClustered

    .SeriesCollection.NewSeries
    .SeriesCollection(1).Name = "PTV = CTV + 5mm"
    .SeriesCollection(1).XValues = "=" & ActiveSheet.Name & "!" & PAi & ":" & PAf
    .SeriesCollection(1).Values = "=" & ActiveSheet.Name & "!" & Ai & ":" & Af

    .SeriesCollection.NewSeries
    .SeriesCollection(2).Name = "PTV = CTV + 7mm"
    .SeriesCollection(2).XValues = "=" & ActiveSheet.Name & "!" & PBi & ":" & PBf
    .SeriesCollection(2).Values = "=" & ActiveSheet.Name & "!" & Bi & ":" & Bf

    .SeriesCollection.NewSeries
    .SeriesCollection(3).Name = "PTV = CTV + ?"
    .SeriesCollection(3).XValues = "=" & ActiveSheet.Name & "!" & PCi & ":" & PCf
    .SeriesCollection(3).Values = "=" & ActiveSheet.Name & "!" & Ci & ":" & Cf

    .SeriesCollection.NewSeries
    .SeriesCollection(4).Name = "PTV of pre-treated patients"
    .SeriesCollection(4).XValues = "=" & ActiveSheet.Name & "!" & PDi & ":" & PDf
    .SeriesCollection(4).Values = "=" & ActiveSheet.Name & "!" & Di & ":" & Df

    'Titles
    .HasTitle = True
    .ChartTitle.Characters.Text = "V95% of CS-PTV(%) (Patients)"
    .ChartTitle.Characters(Start:=2, Length:=3).Font.Subscript = True
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Patients"
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "V95%(%) of CS-PTV(%)"
    .Axes(xlValue, xlPrimary).AxisTitle.Characters(Start:=2, Length:=3).Font.Subscript = True
    .Axes(xlCategory).HasMajorGridlines = True

    'Formatting
    .Axes(xlCategory).HasMinorGridlines = False
    .Axes(xlValue).HasMajorGridlines = True
    .Axes(xlValue).HasMinorGridlines = True
    .HasLegend = True

    'Position
    .ChartArea.Left = 725
    .ChartArea.Top = 70
    .ChartArea.Height = 500
    .ChartArea.Width = 750

End With

在运行时间内不同系列定义中涉及的局部变量等于:

enter image description here

我正在使用的工作表和我得到的直方图如下所示:

enter image description here

正如您所看到的,我不希望属于不同类别的列混合,而是出现在另一个类别之后。

有人知道我怎么能得到我想要的东西。 我提前非常感谢你。

0 个答案:

没有答案