웹 사이트를 열고 위치를 선택하고 HTML 표를 Excel 시트로 복사 한 다음 다른 위치에서 반복해야하는 코드가 있습니다. 그러나 'For'루프를 실행하면 4 번째 반복에서 오류가 발생합니다. 메시지에 "개체 변수 또는 With 블록 변수가 설정되지 않았습니다." 디버그 도구가 44 번째 줄을 가리키고 있습니다.VBA 코드 - 4 번째 루프에서 멈춤
Sub ParseTable()
Dim IE As InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument 'Document object
Dim eleColtr As MSHTML.IHTMLElementCollection 'Element collection for tr tags
Dim eleColtd As MSHTML.IHTMLElementCollection 'Element collection for td tags
Dim eleRow As MSHTML.IHTMLElement 'Row elements
Dim eleCol As MSHTML.IHTMLElement 'Column elements
Dim ieURL As String 'URL
Dim x As Integer
Dim y As String
y = "A1"
For x = 1 To 4
If x <> 2 Then 'Skip iteration 2
Set IE = New InternetExplorer
IE.Visible = True
Application.ScreenUpdating = False
ieURL = "***"
IE.navigate ieURL
'Wait
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set htmldoc = IE.document 'Document webpage
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
IE.document.getElementById("ddlLevel1").selectedIndex = x
IE.document.getElementById("ddlLevel1").FireEvent ("onchange")
Do While IE.Busy Or IE.readyState <> 4
DoEvents
Loop
Set eleColtr = IE.document.getElementsByTagName("tr") 'Find all tr tags
'This section populates Excel
i = 0 'start with first value in tr collection
For Each eleRow In eleColtr 'for each element in the tr collection
Set eleColtd = IE.document.getElementsByTagName("tr")(i).getElementsByTagName("td") 'get all the td elements in that specific tr
j = 0 'start with the first value in the td collection
For Each eleCol In eleColtd 'for each element in the td collection
Sheets("Sheet1").Range(y).Offset(i, j).Value = eleCol.innerText 'paste the inner text of the td element, and offset at the same time
j = j + 1 'move to next element in td collection
Next eleCol 'rinse and repeat
i = i + 1 'move to next element in td collection
Next eleRow 'rinse and repeat
Sheets("Sheet1").Range(y).Offset(i, 0).Select
y = "A" & ActiveCell.Row
IE.Quit
End If
Next x
Application.ScreenUpdating = True
End Sub
가능한 원인을 파악하지 못했습니다. 나는 Excel 시트의 처음 4 개 위치 (위치 2 제외)에서 테이블을 가져 왔습니다. 내 길고 비효율적 인 코드를 용서해주십시오 (저는 프로그래머가 아닙니다). 내가 사용하는 웹 페이지에는 로그인이 필요하고 기밀 데이터가 있지만 가능한 한 많은 정보를 제공하려고 노력할 것입니다. 어떤 도움을 주셔서 미리 감사드립니다!
선 (44)는 말한다'J를 추가
For Each eleRow In eleColtr
후 각 루프의 시작에 지연을 추가 할 것입니다. 그 맞습니까? – YowE3K
나는 IE.document.getElementsByTagName ("tr") (i) .getElementsByTagName ("td")'오류가 여기에 있다고 생각한다. 아마도 당신은이 "tr"안에 "td"를 얻으려고 시도하고있을 것이지만, "tr"그 자체는 어떤 시점에서는 아무것도 아닙니다. 오류가 발생하기 전에 실행을 중지하고 변수를 분석해야합니다. – mathiasfk
몇 가지 다른 가능성이 나열되어 있습니다. [여기] (https://msdn.microsoft.com/en-us/library/5szkzs17.aspx) – mathiasfk