2017年6月14日 星期三

Excel VBA 抓HTML 中指定的Table



Option Explicit
Sub EX()
    Dim i As Integer, E As Object, C As Integer, Sh As Worksheet
    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .Navigate "http://www.goodinfo.tw/stockinfo/ShowK_ChartFlow.asp?RPT_CAT=DR_3M&STOCK_ID=2454&CHT_CAT=MONTH"
        Do While .Busy Or .readyState <> 4: DoEvents:      Loop
        With .DOCUMENT
            Do
                If .all.tags("table").Length >= 17 Then Exit Do
                DoEvents
            Loop
            Set Sh = ActiveSheet
            Sh.UsedRange.Clear
            For Each E In .all.tags("table")(17).Rows
                i = i + 1 '
                For C = 0 To E.Cells.Length - 1
                    Sh.Cells(i, C + 1) = E.Cells(C).innertext
                Next
            Next
            With Sh
                 For i = 21 To .Range("A" & Rows.Count).End(xlUp).Row Step 18
                    .Cells(i, "A").Resize(2).EntireRow.Delete
                Next
            End With
        End With
        .Quit        '關閉網頁
    End With

End Sub

沒有留言:

張貼留言