以往用VBA畫單一圖層是比較簡單,若想要將兩張圖疊加在一起比較時,真需要話點時間處理,這裡先做個兩張圖疊加在一起的簡單範例當作入門,有興趣的可以參考以下程式碼。
Option Explicit Dim xlStartYear As Integer Dim xlStartMonth As Integer Dim xlStartDay As Integer Sub 大盤與融資() Application.ScreenUpdating = False xlStartYear = 2014 xlStartMonth = 1 xlStartDay = 1 ClaerChartObjects With Workbooks(1).Worksheets(1) .UsedRange.ClearContents .Cells(1, 1) = "交易日期" .Cells(1, 2) = "大盤指數" .Cells(1, 3) = "融資餘額" .Cells(1, 4) = "融券餘額" .Cells(1, 5) = "融資金額" End With 大盤指數 融資資料 Worksheets(1).Cells.EntireColumn.AutoFit Worksheets(1).Cells.HorizontalAlignment = xlCenter 繪圖 Application.ScreenUpdating = True End Sub Sub 大盤指數() Dim url As String Dim i, j, k, xlCount, xlDay, xlDaysOfMonth As Integer xlDay = xlStartDay xlCount = 2 For i = xlStartYear To Year(Date) For j = xlStartMonth To Month(Date) xlDaysOfMonth = Day(DateSerial(Format(i, "0000"), Format(j, "00") + 1, 1) - 1) For k = xlDay To xlDaysOfMonth url = "http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX_print.php?genpage=genpage/Report" & Format(i, "0000") & Format(j, "00") & "/A112" & Format(i, "0000") & Format(j, "00") & Format(k, "00") & "MS.php&type=csv" With Workbooks.Open(url) .ActiveSheet.Range("B3:B3").Select If ActiveCell.Value <> Empty Then .ActiveSheet.Range("B3:B3").Copy _ Destination:=Workbooks(1).Worksheets(1).Range("B" & xlCount & ":B" & xlCount) Workbooks(1).Worksheets(1).Cells(xlCount, 1) = Format(i, "0000") & "/" & Format(j, "00") & "/" & Format(k, "00") xlCount = xlCount + 1 End If .Close 0 End With Next k If k >= xlDaysOfMonth Then xlDay = 1 End If Next j Next i End Sub Sub 融資資料() Dim url As String Dim i, j, k, xlCount, xlDay, xlDaysOfMonth As Integer xlDay = xlStartDay xlCount = 2 For i = xlStartYear To Year(Date) For j = xlStartMonth To Month(Date) xlDaysOfMonth = Day(DateSerial(Format(i, "0000"), Format(j, "00") + 1, 1) - 1) For k = xlDay To xlDaysOfMonth url = "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN_2.php?select2=MS&input_date=" & Format(i, "0000") & "/" & Format(j, "00") & "/" & Format(k, "00") & "&type=csv" With Workbooks.Open(url) .ActiveSheet.Cells(1, 1).Select If ActiveCell.Value <> Empty Then .ActiveSheet.Range("F3:F5").Copy Workbooks(1).Worksheets(1).Range("C" & xlCount & ":C" & xlCount).PasteSpecial Transpose:=True xlCount = xlCount + 1 End If .Close 0 End With Next k If k >= xlDaysOfMonth Then xlDay = 1 End If Next j Next i End Sub Sub 繪圖() Dim i, nRow As Integer ClaerChartObjects nRow = Worksheets(1).Range("A65536").End(xlUp).Row With ActiveSheet.ChartObjects.Add(Left:=358, Top:=33, Width:=701, Height:=445).Chart .HasTitle = True .ChartTitle.Text = "大盤與融資餘額" With .SeriesCollection.NewSeries .ChartType = xlLineMarkers '圖表類型 .Values = Range("B2:B" & nRow) '數值 .XValues = Range("A2:A" & nRow) 'X軸 .Name = "大盤" '圖表名稱 .AxisGroup = xlPrimary End With With .SeriesCollection.NewSeries .ChartType = xlLineMarkers '圖表類型 .Values = Range("C2:C" & nRow) '數值 .XValues = Range("A2:A" & nRow) 'X軸 .Name = "融資" '圖表名稱 .AxisGroup = xlSecondary End With '.Legend.Delete .Legend.Position = xlBottom 'xlLegendPositionBottom 或 xlBottom都可 For i = 1 To .SeriesCollection(1).Points.Count With .SeriesCollection(1).Points(i) '.MarkerStyle = xlMarkerStyleCircle '.MarkerForegroundColorIndex = 1 '設定數列前景色為黑色(ColorIndex=1) '.MarkerBackgroundColorIndex = 3 '設定數列背景色為紅色(ColorIndex=3) '.Border.ColorIndex = 3 '設定數列線條為紅色(ColorIndex=3) '.ApplyDataLabels xlDataLabelsShowValue '顯示數值 '.MarkerSize = 6 End With Next i With .Axes(xlCategory) 'X軸設定 .HasTitle = True .AxisTitle.Text = Range("A1:A1") .AxisTitle.Font.Size = 12 End With With .Axes(xlValue, xlPrimary) '左邊 Y軸設定 .HasTitle = True .AxisTitle.Text = Range("B1:B1") .AxisTitle.Font.Size = 12 .AxisTitle.Orientation = xlVertical '字體方向旋轉 .AxisTitle.VerticalAlignment = xlCenter .AxisTitle.HorizontalAlignment = xlCenter End With With .Axes(xlValue, xlSecondary) '右邊 Y軸設定 .HasTitle = True .AxisTitle.Text = Range("C1:C1") .AxisTitle.Font.Size = 12 .AxisTitle.Orientation = xlVertical '字體方向旋轉 .AxisTitle.VerticalAlignment = xlCenter .AxisTitle.HorizontalAlignment = xlCenter End With End With 'Color Index : http://msdn.microsoft.com/en-us/library/cc296089(office.12).aspx End Sub Sub ClaerChartObjects() Dim Chtobj As ChartObject For Each Chtobj In ActiveSheet.ChartObjects Chtobj.Delete Next End Sub
執行畫面