返回列表 上一主題 發帖

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

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

本帖最後由 FAlonso 於 2011-3-13 15:21 編輯

操作原意是在B2選擇開始日期,B3選擇結束日期,B4選擇數據列(例如最高值,最低值)等,再透過程式繪畫該數據列的圖表

以下程式成功運行
  1. Sub plotgraph()
  2. Dim myrange As Range, error As Boolean, x As Integer, y As Integer, mycolumn, mytitle
  3. With Sheet1
  4. .Activate
  5. .[a1].Activate
  6. End With

  7. error = False
  8. x = 0
  9. y = 0
  10. mytitle = [b4].Value

  11. If [b2] = "" Or [b3] = "" Then
  12. MsgBox "No date"
  13. Exit Sub
  14. End If

  15. If DateValue([a8]) > DateValue([b2]) Then
  16. MsgBox "Before starting period"
  17. error = True
  18. End If

  19. If DateValue([b3]) > DateValue([a41]) Then
  20. MsgBox "After ending period"
  21. error = True
  22. End If

  23. If error = True Then
  24. Exit Sub
  25. End If

  26. For Each myrange In Range([a8], [a65536].End(xlUp))
  27. If DateValue(myrange) >= DateValue([b2]) Then
  28. x = myrange.Row
  29. Exit For
  30. End If
  31. Next

  32. For Each myrange In Range([a8], [a65536].End(xlUp))
  33. If DateValue(myrange) > DateValue([b3]) Then
  34. y = myrange.Row - 1
  35. Exit For
  36. End If
  37. Next

  38. If mytitle = "開盤" Then
  39.     mycolumn = "B"
  40. ElseIf mytitle = "最高" Then
  41.     mycolumn = "C"
  42. ElseIf mytitle = "最低" Then
  43.     mycolumn = "D"
  44. ElseIf mytitle = "收盤" Then
  45.     mycolumn = "E"
  46. End If

  47. Sheet1.ChartObjects(1).Select
  48. ActiveChart.HasTitle = True
  49. ActiveChart.ChartTitle.Text = mytitle
  50. ActiveChart.HasLegend = False
  51. ActiveChart.SetSourceData Source:=Sheet1.Range("A" & x & ":" & "A" & y & "," & mycolumn & x & ":" & mycolumn & y), PlotBy:=xlColumns

  52. With Sheet1
  53. .Activate
  54. .[a1].Activate
  55. End With

  56. End Sub
複製代碼
80 字節以內
不支持自定義 Discuz! 代碼

本帖最後由 FAlonso 於 2011-3-13 15:22 編輯

請參考以上程式
80 字節以內
不支持自定義 Discuz! 代碼

TOP

        靜思自在 : 為人處世要小心細心,但不要「小心眼」。
返回列表 上一主題