返回列表 上一主題 發帖

VBA 畫圖方式請益?????

VBA 畫圖方式請益?????

請教各位大大:
我有5個以上的sheets資料,想要自動抓取其中column的資料並畫成散布圖(資料sheet不一定,有時超個10個sheets .....請問以下程式會不會太常有可以簡短點嗎?
PS:如果我要畫第二張圖在同一張sheet內,應該怎麼寫(資料也是根據之前的sheet 但不同column)
以下是第一張圖的code:
  1. Sub ALL_PLOT()

  2. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. '                                                                             '
  4. '                               Plot Create                                  '
  5. '                                                                             '
  6. '                                                                             '
  7. ' ''''''''''''''''''''''CHART(1) Ave.Ave. G.R. '''''''''''''''''''''''''''''''

  8. ''''''''''''''''''''''''''Chart(1) Ave. G.R. / HSP ADD''''''''''''''''''''''''
  9.   Sheets("R2R_analysis").Select
  10.   ActiveSheet.Shapes.AddChart.Select
  11.     ActiveChart.ChartType = xlXYScatter

  12.    Dim aSh As Worksheet
  13.     Application.ScreenUpdating = False
  14.     For Each aSh In Worksheets
  15.     If aSh.Name = "工作表1 (2)" Then
  16.     Call polt_1
  17.     ElseIf aSh.Name = "工作表1 (3)" Then
  18.     Call polt_2
  19.     ElseIf aSh.Name = "工作表1 (4)" Then
  20.     Call polt_3
  21.     ElseIf aSh.Name = "工作表1 (5)" Then
  22.     Call polt_4
  23.     ElseIf aSh.Name = "工作表1 (6)" Then
  24.     Call polt_5
  25.    
  26.      ElseIf aSh.Name = "工作表1 (7)" Then
  27.     Call polt_6
  28.    
  29.      ElseIf aSh.Name = "工作表1 (8)" Then
  30.     Call polt_7
  31.    
  32.      ElseIf aSh.Name = "工作表1 (9)" Then
  33.     Call polt_8
  34.      ElseIf aSh.Name = "工作表1 (10)" Then
  35.     Call polt_9
  36.    
  37.    
  38.     ElseIf aSh.Name = "工作表1 (11)" Then
  39.     Call polt_10
  40.     ElseIf aSh.Name = "工作表1 (12)" Then
  41.       Call polt_11
  42.       
  43.    End If
  44.         
  45.         
  46.   Next
  47.     Application.ScreenUpdating = True
  48.       
  49. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  50.   ActiveChart.ApplyLayout (4)
  51.    
  52. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  53.    
  54. Dim xRg As Range
  55.      Dim xChart As ChartObject
  56.     Set xRg = Range("A20:J50")
  57.   Set xChart = ActiveSheet.ChartObjects(1)
  58.    With xChart
  59.   .Top = xRg(1).Top
  60. .Left = xRg(1).Left
  61. .Width = xRg.Width
  62. .Height = xRg.Height
  63.    End With
  64. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  65.    
  66.    ActiveChart.SetElement (msoElementChartTitleAboveChart)
  67.     Selection.Caption = "R2R_Ave.G.R."
  68.     ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
  69.     Selection.Caption = "G.R.(mm/hr)"
  70.     ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
  71.     Selection.Caption = "Length(mm)"
  72.    
  73.     ActiveSheet.ChartObjects(1).Activate
  74.     ActiveChart.Axes(xlCategory).Select
  75.     ActiveChart.Axes(xlCategory).MinimumScale = 0
  76.     ActiveChart.Axes(xlCategory).MaximumScale = 1100
  77.     ActiveChart.Axes(xlCategory).MajorUnit = 100
  78.     ActiveChart.Axes(xlCategory).MinorUnit = 50
  79.     ActiveChart.Axes(xlCategory).CrossesAt = 0
  80.     ActiveChart.Axes(xlValue).CrossesAt = 0

  81.     ActiveChart.SetElement (msoElementPrimaryValueGridLinesMajor)
  82.     ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
  83.   

  84.     Application.ScreenUpdating = True
  85.   
  86.   
  87.   

  88.   
  89. End Sub
  90. Sub polt_1()
  91.    
  92.     ActiveChart.SeriesCollection.NewSeries
  93.     ActiveChart.SeriesCollection(1).Name = Sheets("工作表1 (2)").Range("U1")
  94.     ActiveChart.SeriesCollection(1).XValues = "='工作表1 (2)'!$A$2:$A$20000"
  95.     ActiveChart.SeriesCollection(1).Values = "='工作表1 (2)'!$D$2:$D$20000"
  96.    
  97. End Sub
  98. Sub polt_2()
  99.     ActiveChart.SeriesCollection.NewSeries
  100.     ActiveChart.SeriesCollection(2).Name = Sheets("工作表1 (3)").Range("U1")
  101.     ActiveChart.SeriesCollection(2).XValues = "='工作表1 (3)'!$A$2:$A$20000"
  102.     ActiveChart.SeriesCollection(2).Values = "='工作表1 (3)'!$D$2:$D$20000"
  103.    
  104.   End Sub

  105. Sub polt_3()
  106.     ActiveChart.SeriesCollection.NewSeries
  107.     ActiveChart.SeriesCollection(3).Name = Sheets("工作表1 (4)").Range("U1")
  108.     ActiveChart.SeriesCollection(3).XValues = "='工作表1 (4)'!$A$2:$A$20000"
  109.     ActiveChart.SeriesCollection(3).Values = "='工作表1 (4)'!$D$2:$D$20000"
  110.    
  111.   End Sub

  112. Sub polt_4()
  113.     ActiveChart.SeriesCollection.NewSeries
  114.     ActiveChart.SeriesCollection(4).Name = Sheets("工作表1 (5)").Range("U1")
  115.     ActiveChart.SeriesCollection(4).XValues = "='工作表1 (5)'!$A$2:$A$20000"
  116.     ActiveChart.SeriesCollection(4).Values = "='工作表1 (5)'!$D$2:$D$20000"
  117.    
  118.   End Sub

  119. Sub polt_5()
  120.     ActiveChart.SeriesCollection.NewSeries
  121.     ActiveChart.SeriesCollection(5).Name = Sheets("工作表1 (6)").Range("U1")
  122.     ActiveChart.SeriesCollection(5).XValues = "='工作表1 (6)'!$A$2:$A$20000"
  123.     ActiveChart.SeriesCollection(5).Values = "='工作表1 (6)'!$D$2:$D$20000"
  124.    
  125.   End Sub


  126. Sub polt_6()
  127.     ActiveChart.SeriesCollection.NewSeries
  128.     ActiveChart.SeriesCollection(6).Name = Sheets("工作表1 (7)").Range("U1")
  129.     ActiveChart.SeriesCollection(6).XValues = "='工作表1 (7)'!$A$2:$A$20000"
  130.     ActiveChart.SeriesCollection(6).Values = "='工作表1 (7)'!$D$2:$D$20000"
  131.    
  132.   End Sub

  133. Sub polt_7()
  134.     ActiveChart.SeriesCollection.NewSeries
  135.     ActiveChart.SeriesCollection(7).Name = Sheets("工作表1 (8)").Range("U1")
  136.     ActiveChart.SeriesCollection(7).XValues = "='工作表1 (8)'!$A$2:$A$20000"
  137.     ActiveChart.SeriesCollection(7).Values = "='工作表1 (8)'!$D$2:$D$20000"
  138.    
  139.   End Sub
  140.   Sub polt_8()
  141.     ActiveChart.SeriesCollection.NewSeries
  142.     ActiveChart.SeriesCollection(8).Name = Sheets("工作表1 (9)").Range("U1")
  143.     ActiveChart.SeriesCollection(8).XValues = "='工作表1 (9)'!$A$2:$A$20000"
  144.     ActiveChart.SeriesCollection(8).Values = "='工作表1 (9)'!$D$2:$D$20000"
  145.    
  146.   End Sub
  147.    Sub polt_9()
  148.     ActiveChart.SeriesCollection.NewSeries
  149.     ActiveChart.SeriesCollection(9).Name = Sheets("工作表1 (10)").Range("U1")
  150.     ActiveChart.SeriesCollection(9).XValues = "='工作表1 (10)'!$A$2:$A$20000"
  151.     ActiveChart.SeriesCollection(9).Values = "='工作表1 (10)'!$D$2:$D$20000"
  152.    
  153.   End Sub
  154.    Sub polt_10()
  155.     ActiveChart.SeriesCollection.NewSeries
  156.     ActiveChart.SeriesCollection(10).Name = Sheets("工作表1 (11)").Range("U1")
  157.     ActiveChart.SeriesCollection(10).XValues = "='工作表1 (11)'!$A$2:$A$20000"
  158.     ActiveChart.SeriesCollection(10).Values = "='工作表1 (11)'!$D$2:$D$20000"
  159.     End Sub
  160. Sub polt_11()
  161. ActiveChart.SeriesCollection.NewSeries
  162.     ActiveChart.SeriesCollection(11).Name = Sheets("工作表1 (12)").Range("U1")
  163.     ActiveChart.SeriesCollection(11).XValues = "='工作表1 (12)'!$A$2:$A$20000"
  164.     ActiveChart.SeriesCollection(11).Values = "='工作表1 (12)'!$D$2:$D$20000"
  165. End Sub
複製代碼
Tks
Ian

回復 2# jcchiang

感謝大大的回覆
但在跑的時候出現錯誤????????
再麻煩指教!

謝謝
  1. Sub ALL_PLOT_TEST()
  2. Dim x As Integer
  3. Sheets("R2R_analysis").Select
  4. ActiveSheet.Shapes.AddChart.Select
  5. ActiveChart.ChartType = xlXYScatter
  6. Application.ScreenUpdating = False
  7. For x = 2 To Worksheets.Count
  8.    ActiveChart.SeriesCollection.NewSeries
  9.    ActiveChart.SeriesCollection(x - 1).Name = Sheets("工作表1 (" & x & ")").Range("U1")   .....................................................(請問這段出錯誤(陣列索引 超出範圍) 要如何修正
  10.    ActiveChart.SeriesCollection(x - 1).XValues = Sheets("工作表1 (" & x & ")").Range("A2:A20000")
  11.    ActiveChart.SeriesCollection(x - 1).Values = Sheets("工作表1 (" & x & ")").Range("D2:D20000")
  12. Next
  13. Application.ScreenUpdating = True
複製代碼
Ian

TOP

感謝大大的指教
想在請問一下
以下的code
1.可以改成圖表放置的位置是依我只是的位置固定嗎(ex:Plot1 在 A20:J50  '  plot2在L20:U50........以此類推)
2.還有坐標軸可自己先寫在code中嗎? 因為每一張圖的座標軸明成不一樣

感謝
  1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  2. Dim xRg As Range
  3. Dim xChart As ChartObject
  4. Set xRg = Range("A20:J50")..........................................................................................(問題1)
  5. Set xChart = ActiveSheet.ChartObjects(z)
  6. With xChart
  7.    .Top = xRg(z).Top
  8.    .Left = xRg(z).Left
  9.    .Width = xRg.Width
  10.    .Height = xRg.Height
  11. End With

  12. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  13. ActiveChart.SetElement (msoElementChartTitleAboveChart)
  14. Selection.Caption = "R2R_Ave.G.R." & z...........................................................................................(問題2)
  15. ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
  16. Selection.Caption = "G.R.(mm/hr)"..........................................................................................(問題2)
  17. ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
  18. Selection.Caption = "Length(mm)"
複製代碼
回復 3# jcchiang
Ian

TOP

回復 5# jcchiang

好喔!  晚點傳
(上班中 工作電腦傳不出去)   

感謝
Ian

TOP

回復 9# jcchiang


    感謝大大的回覆 ,我再試一下,感應不盡


謝謝
Ian

TOP

回復 9# jcchiang

感謝喔, 可以使用喔(可照我要的修改)


謝謝
Ian

TOP

        靜思自在 : 甘願做、歡喜受。
返回列表 上一主題