2017-04-15 38 views
0

這是我的代碼:ploting與宏多個數據系列在Excel

Dim i As Integer 
Dim names1 As String 
Dim names2 As String 
Dim names3 As String 


ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmooth).Select 
For i = 1 To 1 

    names1 = Cells(2, i + 1).Address 
    names2 = Range(Cells(3, i), Cells(85, i)).Address 
    names3 = Range(Cells(3, i + 1), Cells(85, i + 1)).Address 

    ActiveChart.SeriesCollection.NewSeries 
    ActiveChart.FullSeriesCollection(1).Name = "=Sheet3!" & names1 
    ActiveChart.FullSeriesCollection(1).XValues = "=Sheet3!" & names2 
    ActiveChart.FullSeriesCollection(1).Values = "=Sheet3!" & names3 

Next i 

X值列「I」時,Y值列「我+ 1」 現在我想在每一個循環它刪除現有的繪圖並繪製新的繪圖。所以我應該添加什麼來堆積彼此的情節?

+0

你'For'循環是從'1要1' ??? –

+0

我想確保它繪製第一組數據並將其刪除,以便將其更改爲「1至1」,並忘記在此處複製正確的版本。 – mojijoon

+0

看到我的答案和代碼 –

回答

2

嘗試(代碼註釋中說明)下面的代碼:

Option Explicit 

Sub PlotMultiSeries() 

Dim i   As Long 
Dim names1  As String 
Dim names2  As String 
Dim names3  As String 
Dim MyCht  As Shape 
Dim Ser   As Series 

' axccording to your code, you are using "Sheet3" , fully qualify all your Ranges and Cells 
With Worksheets("Sheet3") 

    ' set the created Chart to a Shape variable 
    Set MyCht = .Shapes.AddChart2(Style:=240, XlChartType:=xlXYScatterSmooth) 

    For i = 1 To 1 ' your i loop is from 1 To 1 ??? 
     names1 = .Cells(2, i + 1).Address(False, False, xlA1, True) 
     names2 = .Range(.Cells(3, i), .Cells(85, i)).Address(False, False, xlA1, True) 
     names3 = .Range(.Cells(3, i + 1), .Cells(85, i + 1)).Address(False, False, xlA1, True) 

     With MyCht 
      ' add a new series 
      Set Ser = .Chart.SeriesCollection.NewSeries 
      ' define new Series properties 
      With Ser 
       .Name = "=" & names1 
       .XValues = "=" & names2 
       .Values = "=" & names3 
      End With 
     End With 
    Next i 
End With 
+0

我改變了一下如下 對於i = 2到5 names1 = Cells(2,2 * i - 1).Address names2 = Range(Cells(3,2 * i - 1),Cells(85,2 * i-1))。Address names3 = Range(Cells(3,2 * i),Cells(85,2 * i))。Address for i = 1 it sweep through all (它應該只繪製第一組,然後去下一個我),但之後,它工作正常 – mojijoon

+0

@mojijoon很高興我可以幫助:)不要忘記標記爲「答案」 –

+0

第一次迭代怎麼樣。我怎樣才能改變它,所以它只繪製第一組數據? – mojijoon