返回列表 上一主題 發帖

[發問] 請問該如何用VBA自動化出股票K線圖呢?

回復 4# j1221


    k線圖資料必需依序排列
你若直接用所有欄位直接製圖會以預設的直調圖呈現
若直接轉換類型,EXCEL也會出現警告視窗,要求變更資料範圍
學海無涯_不恥下問

TOP

感激Hsieh板主,受教了....

但可否請問您,為何要「先改變資料數列再設定圖表類型」才對呢?

再次感謝
我愛麻辣家族討論區!!

TOP

回復 1# j1221
先改變資料數列再設定圖表類型
  1. Sub Macro1()
  2. '
  3. ' Macro1 Macro
  4. ' Shih-Hao 在 2011/1/16 錄製的巨集
  5. '

  6. '
  7.     Charts.Add
  8.     'ActiveChart.ChartType = xlStockOHLC  '取消這行

  9.     ActiveChart.SetSourceData Source:=Sheets("76C").Range("A1:N22"), PlotBy:= _
  10.         xlColumns
  11.     ActiveChart.SeriesCollection(5).Delete
  12.     ActiveChart.SeriesCollection(5).Delete
  13.     ActiveChart.SeriesCollection(5).Delete
  14.     ActiveChart.SeriesCollection(5).Delete
  15.     ActiveChart.SeriesCollection(5).Delete
  16.     ActiveChart.SeriesCollection(1).XValues = "='76C'!R2C1:R22C1"
  17.     ActiveChart.SeriesCollection(2).XValues = "='76C'!R2C1:R22C1"
  18.     ActiveChart.SeriesCollection(3).XValues = "='76C'!R2C1:R22C1"
  19.     ActiveChart.SeriesCollection(4).XValues = "='76C'!R2C1:R22C1"
  20.     ActiveChart.Location Where:=xlLocationAsObject, Name:="76C"
  21.     With ActiveChart
  22.         .HasTitle = False
  23.         .Axes(xlCategory, xlPrimary).HasTitle = False
  24.         .Axes(xlValue, xlPrimary).HasTitle = False
  25.         .ChartType = xlStockOHLC   '這裡才改變類型
  26.     End With
  27.     ActiveChart.HasLegend = False
  28.     Windows("test1.xls").SmallScroll Down:=7
  29.     ActiveSheet.Shapes(1).IncrementLeft -168.75
  30.     ActiveSheet.Shapes(1).IncrementTop 269.25
  31.     Windows("test1.xls").SmallScroll Down:=6
  32.     ActiveSheet.Shapes(1).ScaleWidth 1.53, msoFalse, msoScaleFromTopLeft
  33.     ActiveSheet.Shapes(1).ScaleHeight 1.36, msoFalse, msoScaleFromTopLeft
  34.     ActiveChart.PlotArea.Select
  35.     With Selection.Border
  36.         .ColorIndex = 16
  37.         .Weight = xlThin
  38.         .LineStyle = xlContinuous
  39.     End With
  40.     With Selection.Interior
  41.         .ColorIndex = 2
  42.         .PatternColorIndex = 1
  43.         .Pattern = xlSolid
  44.     End With
  45.     With Selection.Border
  46.         .ColorIndex = 16
  47.         .Weight = xlThin
  48.         .LineStyle = xlContinuous
  49.     End With
  50.     With Selection.Interior
  51.         .ColorIndex = 40
  52.         .PatternColorIndex = 1
  53.         .Pattern = xlSolid
  54.     End With
  55.     ActiveChart.Axes(xlCategory).Select
  56.     Selection.TickLabels.NumberFormatLocal = "yyyy-mm-dd"

  57. End Sub
複製代碼
學海無涯_不恥下問

TOP

Hello,

如果是在活頁簿裡的資料, 建議直接使用圖表的方式繪製就可以了.
這樣子比較簡單快速.

TOP

        靜思自在 : 【時日莫空過】一個人在世間做了多少事,就等於壽命有多長。因此必須與時間競爭,切莫使時日空過。
返回列表 上一主題