返回列表 上一主題 發帖

ActiveChart.SetSourceData 來源資料問題 (己解決)

回復 2# FAlonso
[會出現型態不符合的警號,該如何避免?]
你的錯誤在於原本的程式在執行時
ActiveSheet.Selection 是 .ChartObjects(1) 而不是 RANGE

修改你的程式如下
b2 b3 請直接輸入日期 即可

   
  1. Sub plotgraph()
  2. Dim myrange As Range, error As Boolean, x As Integer, y As Integer
  3. Dim Rng As Range
  4. error = False
  5. x = 0
  6. y = 0
  7. With Sheet1
  8.     .Activate
  9.     .[a1].Activate   
  10.      '[會出現型態不符合的警號,該如何避免?]
  11.     '你的錯誤在於原本的程式在執行時
  12.     'ActiveSheet.Selection 是 .ChartObjects(1) 而不是 RANGE
  13.     Set Rng = .Range(.[a8], .[a65536].End(xlUp))
  14.     If .[b2] = "" Or .[b3] = "" Then
  15.         MsgBox "No date"
  16.         error = True
  17.     ElseIf .[b2] > .[b3] Then
  18.         MsgBox .[b2] & ">" & .[b3]
  19.         error = True
  20.     ElseIf Rng(1) > .[b2] Then
  21.         MsgBox "Before starting period"
  22.         error = True
  23.     ElseIf .[b3] > Rng(Rng.Count) Then
  24.         MsgBox "After ending period"
  25.         error = True
  26.     End If
  27.     If error = True Then Exit Sub
  28.     For Each myrange In Rng
  29.         If myrange >= .[b2] Then
  30.             x = myrange.Row - 1
  31.             Exit For
  32.         End If
  33.     Next
  34.     For Each myrange In Rng
  35.         If .[b3] <= myrange Then
  36.             y = myrange.Row - 1
  37.             Exit For
  38.         End If
  39.     Next
  40.     If Sheet1.ChartObjects.Count = 0 Then Charts.Add
  41.     .ChartObjects(1).Select
  42.     ActiveChart.ChartType = xlLineMarkers
  43.     ActiveChart.Location xlLocationAsObject, Name:="Sheet1"
  44.     ActiveChart.HasDataTable = False
  45.     .Activate
  46.     If [b4] = "開盤" Then
  47.         ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",B" & x & ":B" & y), PlotBy:=xlColumns
  48.     ElseIf .[b4] = "最高" Then
  49.         ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",C" & x & ":C" & y), PlotBy:=xlColumns
  50.     ElseIf .[b4].Value = "最低" Then
  51.         ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",D" & x & ":D" & y), PlotBy:=xlColumns
  52.     ElseIf .[b4].Value = "收盤" Then
  53.         ActiveChart.SetSourceData Source:=.Range("A" & x & ":A" & y & ",E" & x & ":E" & y), PlotBy:=xlColumns
  54.     End If
  55. End With
  56. End Sub
複製代碼

TOP

        靜思自在 : 並非有錢魷是快樂,問心無愧心最安。
返回列表 上一主題