使用vba绘制多个绘图

时间:2016-01-16 20:58:48

标签: excel-vba draw vba excel

我试图在excel中使用vba绘制多个绘图我写了这个代码,这使我能够绘制一个绘图

Sub trial()
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.HasLegend = True
    ActiveChart.FullSeriesCollection(1).Name = "=""HBES"""
    ActiveChart.FullSeriesCollection(1).XValues = "=EN!$G$253:$G$257"
    ActiveChart.FullSeriesCollection(1).Values = "=EN!$dp$253:$dp$257"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(2).Name = "=""NHBES"""
    ActiveChart.FullSeriesCollection(2).XValues = "=EN!$G$253:$G$257"
    ActiveChart.FullSeriesCollection(2).Values = "='EN1'!$do$253:$do$257"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(3).Name = "=""NHBCS"""
    ActiveChart.FullSeriesCollection(3).XValues = "=EN!$G$253:$G$257"
    ActiveChart.FullSeriesCollection(3).Values = "=EN1c!$do$253:$do$257"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.FullSeriesCollection(4).Name = "=""HBCS"""
    ActiveChart.FullSeriesCollection(4).XValues = "=EN!$G$253:$G$257"
    ActiveChart.FullSeriesCollection(4).Values = "=ENC!$dp$253:$dp$257"
    With ActiveChart
     'chart name
    .HasTitle = True
    .ChartTitle.Characters.Text = "Expected Number of goods for Group Twenty in Condition State Five"
     'X axis name
    .Axes(xlCategory, xlPrimary).HasTitle = True
    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Years)"
     'y-axis name
    .Axes(xlValue, xlPrimary).HasTitle = True
    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Expected Number of goods"
End With
End Sub

我需要做的是使数据范围变量我尝试以下列方式调整上述代码

 Sub trial()
    ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.HasLegend = True
        ActiveChart.FullSeriesCollection(1).Name = "=""HBES"""
        ActiveChart.FullSeriesCollection(1).XValues = "=EN!$G$253:$G$257"
        ActiveChart.FullSeriesCollection(1).Values = Sheets("Sheets12").Range(Cells(253,32),Cells(302,32))
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(2).Name = "=""NHBES"""
        ActiveChart.FullSeriesCollection(2).XValues = "=EN!$G$253:$G$257"
        ActiveChart.FullSeriesCollection(2).Values = Sheets("Sheets13").Range(Cells(253,32),Cells(302,32))
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(3).Name = "=""NHBCS"""
        ActiveChart.FullSeriesCollection(3).XValues = "=EN!$G$253:$G$257"
        ActiveChart.FullSeriesCollection(3).Values = Sheets("Sheets14").Range(Cells(253,32),Cells(302,32))
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.FullSeriesCollection(4).Name = "=""HBCS"""
        ActiveChart.FullSeriesCollection(4).XValues = "=EN!$G$253:$G$257"
        ActiveChart.FullSeriesCollection(4).Values = Sheets("Sheets15").Range(Cells(253,32),Cells(302,32))
        With ActiveChart
         'chart name
        .HasTitle = True
        .ChartTitle.Characters.Text = "Expected Number of goods for Group Twenty in Condition State Five"
         'X axis name
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Years)"
         'y-axis name
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Expected Number of goods"
    End With
    End Sub

但它没有任何建议吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

查看您尝试添加的第一个系列。在您尝试从范围DP253:DP257中提取数据的第一个代码段中,列DP的列索引为120,而不是32 Cells(253, 32)指的是单元格AF253Cells(302, 32)指的是单元格AF302

对于名为“HBES”的情节

.Values = Sheets("EN").Range(Cells(253, 120), Cells(257, 120))

修改

对于名为“NHBES”的情节

.Values = Sheets("'EN1'").Range(Cells(253, 119), Cells(257, 119))

对于名为“NHBCS”的情节

.Values = Sheets("EN1c").Range(Cells(253, 119), Cells(257, 119))

对于名为“HBCS”的情节

.Values = Sheets("ENC").Range(Cells(253, 120), Cells(257, 120))

代码构建如下

Sheets("NAME OF THE SHEET AS IT APPEARS ON THE TAB IN YOUR WORKBOOK").Range(Cells(ROWNUMBER of the top (left) cell in the range, COLUMNNumber of the top (left) cell in the range), Cells(ROWNUMBER of the bottom (right) cell in the range, COLUMNUMBER of the bottom right cell in the range))

如果您指的是范围A1:B5,则键入Range(Cells(1, 1), Cells(5, 2)