2015-06-21 106 views
2

我正在研究我的工作,我必須爲每個工作表生成超過91個圖表,我想用宏來實現這一點。Excel中的多個圖表

我還是新的宏,但我試圖寫這一個,但它不工作。我將非常感謝您在這個問題上的幫助!

該組數據,我有看起來像這樣

> A1   B1  C1 D1 E1  F1 G1  H1 I1 
> 
> 
> Period Ratio  Period Ratio  Period ratio 
> 2000Q1 1.23  2000Q1 0.78  2000Q1 1.07  
> 2000Q2 1.43  2000Q2 1.12  2000Q2 0.76  2000Q3 1.8   
> 2000Q3 1.09  2000Q3 1.21 

(下列甲& BI具有周期和比) - 然後列C是空的 - 然後(下欄d & EI具有周期和比率) 等等。

我用空列分隔數據集。

請注意,其他行(我有一個更新按鈕,我每次點擊一個新的行(週期比率)將被添加爲所有列) - 也是第一行的值開始於行3

我想創建一個圖表爲每個數據集(這裏3圖表)

我寫的宏是如下:

Sub loopChart() 
Dim mychart As Chart 
Dim c As Integer 
Sheets("analysis").Select 

c = 1 
While c <> 0 #I put this condition so that the code will know that I have no more data set 

    Set mychart = Charts.Add 
    mychart.SetSourceData Source:=Range(cells(3, c)).CurrentRegion, PlotBy:=xlColumns 
    c = c + 3 
Wend 

For Each mychart In Sheets("class").ChartObjects 
    mychart.ChartType = xlLineMarkers 
Next mychart 

End Sub 

我不是太相信我在做什麼是正確的,但我面臨着一個範圍的麻煩。我也知道這個宏將創建一個新的圖表。

如何在值旁邊的「分析」工作表上創建所有圖表?

我將不勝感激任何人的幫助!

+0

'它不工作'是什麼意思?請添加輸出圖片或描述錯誤的內容。根據這一點,您可能需要查看[此方法](http://stackoverflow.com/a/29720112/4288101)以通過VBA創建圖表。 'SetSourceData'往往會讓你置身於Excel解釋你想要繪製的'Range'的範圍內。直接定義'Series'使得構建你想要的'Chart'變得更容易。 –

回答

1

我的朋友。工作簿可以直接具有圖表,也可以將圖表集成到工作表中。這取決於你想要什麼。 Excel是一個面向對象的程序。 Range.CurrentRegion應該可以工作,但如果周圍有任何已填滿的單元格,則可能會導致問題。

試試這個:

Sub loopChart() 

Dim mychart As Chart 
Dim myRange As range 
Dim c As Integer 
c = 1 

While c <= 7 '1=dataSource1, 4=dataSource2, 7=dataSource3 
    'set data source for the next chart 
    With Worksheets("class") 
     Set myRange = .range(.Cells(3, c), .Cells(6, c + 1)) 
    End With 

    'create chart 
    Sheets("analysis").Select 
     ActiveSheet.Shapes.AddChart.Select 

     With ActiveChart 
      .ChartType = xlLineMarkers 'xlLine 
      .SetSourceData Source:=myRange, PlotBy:=xlColumns 'sets source data for graph including labels 
      .SetElement (msoElementLegendRight) 'including legend 
      .HasTitle = True 
      'dimentions & location: 
      .Parent.Top = 244 'defines the coordinates of the top of the chart 
      .Parent.Left = c * 100 'defines the coordinates for the left side of the chart 
      .Parent.Height = 200 
      .Parent.Width = 300 
      .ChartTitle.Text = "Title here" 
     End With 

    c = c + 3 
Wend 

End Sub 

結果: Screenshot

有關進一步的情況下,您可以訪問Microsoft Office開發中心:
Creating Charts in Excel 2003 Using Visual Basic for Applications Code

0

如果你希望所有的圖表analysisWorksheet,您可以在創建圖表時更改Location。我還在第二個循環中更改了工作表名稱以匹配工作表名稱。你可以將這一行代碼添加到第一個循環中;第二個循環沒有必要。如果你這樣做,一定要在位置線上設置一個新的參考mychart

Sub loopChart() 
Dim mychart As Chart 
Dim c As Integer 
Sheets("analysis").Select 

c = 1 
While c <> 0 #I put this condition so that the code will know that I have no more data set 

    Set mychart = Charts.Add 
    mychart.SetSourceData Source:=Range(cells(3, c)).CurrentRegion, PlotBy:=xlColumns 
    'change location to sheet 
    mychart.Location xlLocationAsObject, "analysis" 
    c = c + 3 
Wend 

For Each mychart In Sheets("analysis").ChartObjects 
    mychart.ChartType = xlLineMarkers 
Next mychart 

End Sub