返回列表 上一主題 發帖

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

回復 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

回復 4# j1221


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

TOP

回復 6# j1221


    Sheets(c & op).Cells(i, 1).PasteSpecial
學海無涯_不恥下問

TOP

回復 8# j1221


    ActiveChart.SeriesCollection(1).XValues = "='" & c & op & "'!R2C1:R22C1"
學海無涯_不恥下問

TOP

回復 10# j1221
應該是SELECT的問題
  1.     With Sheets("data")
  2.                 .Cells(1, 1).EntireRow.Copy
  3.                 Sheets.Add After:=Sheets("data")
  4.                 ActiveSheet.Cells(1, 1).PasteSpecial
  5.                 ThisWorkbook.ActiveSheet.Name = c & op
  6.    End With
複製代碼
學海無涯_不恥下問

TOP

回復 13# j1221


    Sheets("data").Cells(i, 3).Value = Val(f)
因為inputbox位指定變數型態則會以字串形態默認
所以f是一個數字組合的字串
但是,你工作表的年月是通用格式,所以被認為是數値
所以判斷式數直是不可能跟字串相等
故此將f使用VAL函數轉為數值即可
學海無涯_不恥下問

TOP

        靜思自在 : 我們最大的敵人不是別人.可能是自己。
返回列表 上一主題