简体   繁体   中英

draw more than one plot using vba

I am trying to draw more than one plot using vba in excel I wrote this code which enable me to draw one drawing

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

What I need to do is to make the data range variable I tried to adjust the above code in the following manner

 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

but it doesn't work Any suggestion please?

Thanks in advance

Looking at the first series you're trying to add. In the first snippet you're trying to pull the data from the Range DP253:DP257 , the columnindex for column DP is 120 , not 32 Cells(253, 32) refers to cell AF253 and Cells(302, 32) refers to cell AF302 .

For the plot named "HBES"

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

Edit

For the plot named "NHBES"

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

For the plot named "NHBCS"

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

For the plot named "HBCS"

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

The code is built up as follows

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))

If you are referring to range A1:B5, you'd type Range(Cells(1, 1), Cells(5, 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM