返回列表 上一主題 發帖

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

回復 1# iverson105

濃縮這樣,試試看
Sub ALL_PLOT()
Dim x As Integer
Sheets("R2R_analysis").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
Application.ScreenUpdating = False
For x = 2 To Worksheets.Count
   ActiveChart.SeriesCollection.NewSeries
   ActiveChart.SeriesCollection(x - 1).Name = Sheets("工作表1 (" & x & ")").Range("U1")
   ActiveChart.SeriesCollection(x - 1).XValues = Sheets("工作表1 (" & x & ")").Range("A2:A20000")
   ActiveChart.SeriesCollection(x - 1).Values = Sheets("工作表1 (" & x & ")").Range("D2:D20000")
Next
Application.ScreenUpdating = True

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveChart.ApplyLayout (4)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim xRg As Range
Dim xChart As ChartObject
Set xRg = Range("A20:J50")
Set xChart = ActiveSheet.ChartObjects(1)
With xChart
   .Top = xRg(1).Top
   .Left = xRg(1).Left
   .Width = xRg.Width
   .Height = xRg.Height
End With

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveChart.SetElement (msoElementChartTitleAboveChart)
Selection.Caption = "R2R_Ave.G.R."
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
Selection.Caption = "G.R.(mm/hr)"
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = "Length(mm)"
ActiveSheet.ChartObjects(1).Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 0
ActiveChart.Axes(xlCategory).MaximumScale = 1100
ActiveChart.Axes(xlCategory).MajorUnit = 100
ActiveChart.Axes(xlCategory).MinorUnit = 50
ActiveChart.Axes(xlCategory).CrossesAt = 0
ActiveChart.Axes(xlValue).CrossesAt = 0
ActiveChart.SetElement (msoElementPrimaryValueGridLinesMajor)
ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
Application.ScreenUpdating = True
End Sub

TOP

回復 2# jcchiang
第二個問題:如果我要畫第二張圖在同一張sheet內,應該怎麼寫(資料也是根據之前的sheet 但不同column)
因為不確定會畫幾張圖,所以程式會先詢問要畫幾次(如果只會畫2張就將y = Application.InputBox("畫圖次數", "")移除,For z = 1 To y改為For z = 1 To 2)
另外第二張以後的圖Column不知道會在哪裡,先當作再第一張圖Column往右擺放,如果位置差異較多就要再修改

Sub ALL_PLOT1()
Dim x, y, z As Integer
y = Application.InputBox("畫圖次數", "")
For z = 1 To y
Sheets("R2R_analysis").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
Application.ScreenUpdating = False
For x = 2 To Worksheets.Count
   ActiveChart.SeriesCollection.NewSeries
   ActiveChart.SeriesCollection(x - 1).Name = Sheets("工作表1 (" & x & ")").Range("U1").Offset(0, (z - 1))
   ActiveChart.SeriesCollection(x - 1).XValues = Sheets("工作表1 (" & x & ")").Range("A2:A20000").Offset(0, (z - 1))
   ActiveChart.SeriesCollection(x - 1).Values = Sheets("工作表1 (" & x & ")").Range("D2:D20000").Offset(0, (z - 1))
Next
Application.ScreenUpdating = True

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveChart.ApplyLayout (4)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim xRg As Range
Dim xChart As ChartObject
Set xRg = Range("A20:J50")
Set xChart = ActiveSheet.ChartObjects(z)
With xChart
   .Top = xRg(z).Top
   .Left = xRg(z).Left
   .Width = xRg.Width
   .Height = xRg.Height
End With

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ActiveChart.SetElement (msoElementChartTitleAboveChart)
Selection.Caption = "R2R_Ave.G.R." & z
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleRotated)
Selection.Caption = "G.R.(mm/hr)"
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
Selection.Caption = "Length(mm)"
ActiveSheet.ChartObjects(z).Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).MinimumScale = 0
ActiveChart.Axes(xlCategory).MaximumScale = 1100
ActiveChart.Axes(xlCategory).MajorUnit = 100
ActiveChart.Axes(xlCategory).MinorUnit = 50
ActiveChart.Axes(xlCategory).CrossesAt = 0
ActiveChart.Axes(xlValue).CrossesAt = 0
ActiveChart.SetElement (msoElementPrimaryValueGridLinesMajor)
ActiveChart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
Application.ScreenUpdating = True
Next
End Sub

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題