Option Explicit Sub 融資資料() Dim URL As String Dim i, j, k, xlCount As Integer Application.ScreenUpdating = False With Workbooks(1).Worksheets(1) .UsedRange.ClearContents .Cells(1, 1) = "交易日期" .Cells(1, 2) = "融資餘額" .Cells(1, 3) = "融券餘額" .Cells(1, 4) = "融資金額" End With ClaerChartObjects xlCount = 2 For i = 2014 To Year(Date) For j = 1 To Month(Date) For k = 1 To Day(DateSerial(Format(i, "0000"), Format(j, "00") + 1, 1) - 1) 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.UsedRange.Copy _ Destination:=Workbooks(1).Worksheets(2).Cells(1, 1) With Workbooks(1) .Worksheets(1).Cells(xlCount, 1) = Format(i, "0000") & "/" & Format(j, "00") & "/" & Format(k, "00") .Worksheets(2).Range("F3:F5").Copy .Worksheets(1).Range("B" & xlCount & ":B" & xlCount).PasteSpecial Transpose:=True .Worksheets(2).UsedRange.ClearContents End With xlCount = xlCount + 1 End If .Close 0 End With Next k Next j Next i Worksheets(1).Cells.EntireColumn.AutoFit Worksheets(1).Cells.HorizontalAlignment = xlCenter 繪圖 Application.ScreenUpdating = True End Sub Sub 繪圖() Dim i, nRow As Integer ClaerChartObjects nRow = Worksheets(1).Range("A65536").End(xlUp).Row With ActiveSheet.ChartObjects.Add(Left:=301, Top:=33, Width:=701, Height:=445).Chart With .SeriesCollection.NewSeries .ChartType = xlLineMarkers '圖表類型 .Values = Range("B2:B" & nRow) '數值 .XValues = Range("A2:A" & nRow) 'X軸 .Name = "大盤融資" '圖表名稱 End With .Legend.Delete 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) 'Y軸設定 .HasTitle = True .AxisTitle.Text = Range("B1:B1") .AxisTitle.Font.Size = 12 .AxisTitle.Orientation = xlVertical '字體方向旋轉 .AxisTitle.VerticalAlignment = xlCenter .AxisTitle.HorizontalAlignment = xlCenter End With End With 'ColorIndex : 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
執行畫面