VBA - 重复图表的选择

时间:2018-02-20 15:16:45

标签: excel vba charts repeat

我正在重复下面的过程,并想知道如何将其转换为一个简单的宏...(可能使用Dim i As Long : For i = 10 To 42)或类似的东西?

Sub Graph4()        
    ActiveSheet.ChartObjects("Chart 1").Activate        
    ActiveChart.SeriesCollection.NewSeries      
    ActiveChart.FullSeriesCollection(8).Name = "=""Pipe 8"""        
    ActiveChart.FullSeriesCollection(8).XValues = "=Sheet2!$G$9:$I$9        
    ActiveChart.FullSeriesCollection(8).Values = "=Sheet2!$C$9:$E$9"        
    ActiveChart.SeriesCollection.NewSeries      
    ActiveChart.FullSeriesCollection(9).Name = "=""Pipe 9"""
    ActiveChart.FullSeriesCollection(9).XValues = "=Sheet2!$G$10:$I$10"     
    ActiveChart.FullSeriesCollection(9).Values = "=Sheet2!$C$10:$E$10"      
    ActiveChart.SeriesCollection.NewSeries      
    ActiveChart.FullSeriesCollection(10).Name = "=""Pipe 10"""      
    ActiveChart.FullSeriesCollection(10).XValues = "=Sheet2!$G$11:$I$11"        
    ActiveChart.FullSeriesCollection(10).Values = "=Sheet2!$C$11:$E$11"
End Sub

我试过这个但是没有用......

Sub Graph2()
    Dim i As Long
    For i = 10 To 42        
        ActiveSheet.ChartObjects("Chart 1").Activate        
        ActiveChart.SeriesCollection.NewSeries      
        ActiveChart.FullSeriesCollection(i).Name = i - 1        
        ActiveChart.FullSeriesCollection(i).XValues = ActiveSheet.Range(Cells(i, 7), Cells(i, 8), Cells(i, 9)).Select       
        ActiveChart.FullSeriesCollection(i).Values = ActiveSheet.Range(Cells(i, 3), Cells(i, 4), Cells(i, 5)).Select
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

我相信你需要这样的东西。

For i = 10 To 42
  ActiveSheet.ChartObjects("Chart 1").Activate
  ActiveChart.SeriesCollection.Add _
   Source:=ActiveSheet.Range(Cells(10 + 1, 3), Cells(10 + 1, 5)) "adds the new series
  ActiveChart.SeriesCollection(i).xvalues = ActiveSheet.Range(Cells(10 + 1, 7), Cells(10+1, 9))  
  ActiveChart.SeriesCollection(i).Name = "Pipe " & i   'names the series
Next i