- 帖子
- 525
- 主題
- 56
- 精華
- 0
- 積分
- 603
- 點名
- 94
- 作業系統
- win 10
- 軟體版本
- []
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2013-3-19
- 最後登錄
- 2025-4-23
           
|
7#
發表於 2019-5-11 07:55
| 只看該作者
回復 6# zheng211016
請注意參考紅字的部分,陣列從 1 開始:
Option Base 1
Sub byXMLhttp_Test2()
Dim sh As Worksheet
Dim t!, i%, j%, k%
Dim myXML As Object, myHTML As Object, myTable, arDATA, URL$
URL = "https://histock.tw/stock/financial.aspx?no=3032&t=2"
Set sh = Worksheets("試驗頁")
Set myXML = CreateObject("Microsoft.XMLHTTP")
Set myHTML = CreateObject("HTMLFile")
sh.Cells.Clear
With myXML
.Open "GET", URL, False
.send
If .Status <> 200 Then MsgBox "No successful web connection", vbOKOnly: Exit Sub
myHTML.body.innerhtml = .responsetext
End With
Set myTable = myHTML.getelementsbytagname("TABLE")(0)
With myTable
i = .Rows.Length: j = .Rows(0).Cells.Length
ReDim arDATA(i, j)
k = 0 '
For i = 1 To .Rows.Length
If .Rows(i - 1).innertext <> "" Then
k = k + 1
For j = 1 To .Rows(0).Cells.Length
Cells(k, j) = .Rows(i - 1).Cells(j - 1).innertext
arDATA(k, j) = .Rows(i - 1).Cells(j - 1).innertext
Next
End If
Next
End With
sh.[A15].Resize(UBound(arDATA), UBound(arDATA, 2)) = arDATA
Set myXML = Nothing
Set myHTML = Nothing
Set myTable = Nothing
End Sub |
|