最新消息

[公告2014/05/30] 如有需要將部落格中,任何一篇文章的程式碼使用在商業用途,請與我聯繫。

[公告2015/04/26] Line版的 iInfo程式與投資應用 群組已上線想加入的朋友們,請先查看 "入群須知" 再與我聯繫 Line : aminwhite5168,加入請告知身分與回答 "入群須知" 的問題。

[公告2018/04/22] 台北 Python + Excel VBA 金融資訊爬蟲課程,課程如網頁內容 金融資訊爬蟲班:台北班 Python 金融資訊爬蟲、EXCEL VBA 金融資訊爬蟲

[公告2019/01/08] 請注意:我再次重申,部落格文章的程式碼,是要提供各位參考與學習,一旦網頁改版請自行修改,別要求東要求西要我主動修改,你們用我寫東西賺錢了、交差了,請問有分我一杯羹嗎?既然賺錢沒分我,請問有什麼理由要求我修改,如果沒能力改,就花錢來找我上課。

[公告2019/12/01] 若各位有 Excel VBA 案子開發需求,歡迎與我聯繫,可接案處理。

[公告2020/05/22] 頁面載入速度慢,起因為部分JS來源(alexgorbatchev.com)失效導致頁面載入變慢,目前已做調整,請多見諒。

2014年3月8日 星期六

VBA抓大盤融資餘額

用VBA抓取證交所每日融資餘額,並且畫成曲線圖,有興趣研究,請參考如下程式碼。
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

執行畫面