最新消息

[公告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)失效導致頁面載入變慢,目前已做調整,請多見諒。

2013年5月15日 星期三

Excel VBA上市公司漲跌家數

想要判別目前大盤的風向球嗎?
做一個屬於自己專屬的風向球指標
版本一
Option Explicit
Sub 漲跌家數_Click()
    Dim URL, xlWorkbookName As String
    Dim xlYear, xlMonth, xlDay As String
    Dim i, j, k, l, Days As Integer
    
    xlWorkbookName = "漲跌家數.xlsm"
    With Workbooks(xlWorkbookName).Sheets(1)
        .UsedRange.ClearContents
        .Cells(1, 1) = "交易日期"
        .Cells(1, 2) = "上漲(漲停)"
        .Cells(1, 3) = "下跌(跌停)"
        .Cells(1, 4) = "持平"
        .Cells(1, 5) = "未成交"
        .Cells(1, 6) = "無比價"
        .Cells(1, 7) = "總數"
    End With
    
    l = 2
    For i = 2013 To Year(Date)
        For j = 1 To Month(Date)
            'Days = DateSerial(i, j + 1, 1) - DateSerial(i, j, 1)
            Days = Day(DateSerial(i, j + 1, 1) - 1)
            For k = 1 To Days
                xlYear = Format(i, "0000")
                xlMonth = Format(j, "00")
                xlDay = Format(k, "00")
                If DateDiff("d", xlYear & "/" & xlMonth & "/" & xlDay, Date) < 0 Then Exit Sub
                
                Workbooks(xlWorkbookName).Sheets(2).UsedRange.ClearContents
                URL = "http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX_print.php?genpage=genpage/Report" & xlYear & xlMonth & "/A112" & xlYear & xlMonth & xlDay & "MS.php&type=csv"
                With Workbooks.Open(URL)
                    If Not .ActiveSheet.Cells(104, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(105, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(106, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(107, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(108, 3).Value = Empty Then
                        .ActiveSheet.Range("C104:C108").Copy Destination:=Workbooks(xlWorkbookName).Sheets(2).Range("A1")
                    End If
                    .Close 0
                End With
                
                With Workbooks(xlWorkbookName)
                    If Not .Sheets(2).Cells(1, 1).Value = Empty Then
                        .Sheets(1).Cells(l, 1).Value = xlYear & xlMonth & xlDay
                        .Sheets(2).Cells(1, 1) = Split(.Sheets(2).Cells(1, 1), "(")(0)
                        .Sheets(2).Cells(2, 1) = Split(.Sheets(2).Cells(2, 1), "(")(0)
                        .Sheets(2).UsedRange.Copy
                        .Sheets(1).Cells(l, 2).PasteSpecial Transpose:=True
                        .Sheets(1).Cells(l, 7) = Application.Sum(Range(Cells(l, 2), Cells(l, 6)))
                        l = l + 1
                    End If
                End With
            Next k
        Next j
    Next i
End Sub

執行結果

版本二
Option Explicit
Sub 漲跌家數_Click()
    Dim URL, xlWorkbookName As String
    Dim xlYear, xlMonth, xlDay As String
    Dim i, j, k, l, Days As Integer
    
    xlWorkbookName = "漲跌家數.xlsm"
    With Workbooks(xlWorkbookName).Sheets(1)
        .UsedRange.ClearContents
        .Cells(1, 1) = "交易日期"
        .Cells(1, 2) = "上漲(漲停)"
        .Cells(1, 3) = "下跌(跌停)"
        .Cells(1, 4) = "持平"
        .Cells(1, 5) = "未成交"
        .Cells(1, 6) = "無比價"
        .Cells(1, 7) = "淨家數"
        '.Cells(1, 8) = "總數"
    End With
    
    Application.ScreenUpdating = False
    
    l = 2
    For i = 2013 To Year(Date)
        For j = 1 To Month(Date)
            'Days = DateSerial(i, j + 1, 1) - DateSerial(i, j, 1)
            Days = Day(DateSerial(i, j + 1, 1) - 1)
            For k = 1 To Days
                xlYear = Format(i, "0000")
                xlMonth = Format(j, "00")
                xlDay = Format(k, "00")
                If DateDiff("d", xlYear & "/" & xlMonth & "/" & xlDay, Date) < 0 Then Exit Sub
                
                Workbooks(xlWorkbookName).Sheets(2).UsedRange.ClearContents
                URL = "http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX_print.php?genpage=genpage/Report" & xlYear & xlMonth & "/A112" & xlYear & xlMonth & xlDay & "MS.php&type=csv"
                With Workbooks.Open(URL)
                    If Not .ActiveSheet.Cells(104, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(105, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(106, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(107, 3).Value = Empty Or _
                       Not .ActiveSheet.Cells(108, 3).Value = Empty Then
                        Application.StatusBar = xlYear & "/" & xlMonth & "/" & xlDay & " 資料載入中......"
                        Workbooks(xlWorkbookName).Sheets(1).Cells(l, 2) = Split(.ActiveSheet.Cells(104, 3), "(")(0)
                        Workbooks(xlWorkbookName).Sheets(1).Cells(l, 3) = Split(.ActiveSheet.Cells(105, 3), "(")(0)
                        .ActiveSheet.Range("C106:C108").Copy
                        Workbooks(xlWorkbookName).Sheets(1).Cells(l, 4).PasteSpecial Transpose:=True
                        With Workbooks(xlWorkbookName).Sheets(1)
                            .Cells(l, 1).Value = xlYear & "/" & xlMonth & "/" & xlDay
                            .Cells(l, 7) = .Cells(l, 2) - .Cells(l, 3)
                            '.Cells(l, 8) = Application.Sum(Range(.Cells(l, 2), .Cells(l, 6)))
                            l = l + 1
                        End With
                    End If
                    .Close 0
                End With
            Next k
        Next j
    Next i
    Application.StatusBar = "資料載入完成......"
    Application.StatusBar = ""
    Application.StatusBar = False
    Application.ScreenUpdating = True
End Sub
執行結果

沒有留言:

張貼留言