Set sh = Worksheets("試驗頁")
Set myXML = CreateObject("Microsoft.XMLHTTP")
Set myHTML = CreateObject("HTMLFile")
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(3).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
arDATA(k, j) = .Rows(i - 1).Cells(j - 1).innertext
Next
End If
Next
不了解版主的意思,
1. "各少一行一列" 是甚麼資料漏掉了? 請舉例以便明白。
2. 依照這個版本, 它已經是 從 [A1] 開始了。
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
sh.Cells(k, j) = .Rows(i - 1).Cells(j - 1).innertext
Next
End If
Next作者: zheng211016 時間: 2019-5-11 00:17
請注意參考紅字的部分,陣列從 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