我有使用根据单元格值更改的动态 URL 来提取团队信息的代码。
我不断收到运行时错误“91”说“对象变量或未设置块变量”,当我调试时它指向该行
Set tr_coll = tbl(0).getelementsbytagname("TR")
此代码适用于其他数据提取,但由于某种原因现在给了我这个错误。
Sub VS()
Dim src As Worksheet, tgt As Worksheet
Dim url As String, team1 As String
Dim team2 As String, j As Integer, row As Integer
Dim XMLHTTP As Object, html As Object
Dim tr_coll As Object, tr As Object
Dim td_coll As Object, td As Object
Set src = Sheets("Match-up")
Set tgt = Sheets("vs")
team1 = src.Range("b1")
team2 = src.Range("aa1")
url = "http://www.premierleague.com/en-gb/matchday/matches/2015-2016/epl.past-meetings.html/" & team1 & "-vs-" & team2
Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.send
Set html = CreateObject("htmlfile")
html.body.innerHTML = XMLHTTP.ResponseText
Set tbl = html.getelementsbytagname("Table")
Set tr_coll = tbl(0).getelementsbytagname("TR")
For Each tr In tr_coll
j = 1
Set td_col = tr.getelementsbytagname("TD")
For Each td In td_col
Cells(row + 1, j).Value = td.innerText
j = j + 1
Next
row = row + 1
Next
End Sub