2017-05-31 39 views
-3
'start a new subroutine called SearchBot 

Sub SearchBot() 

'dimension (declare or set aside memory for) our variables 
Dim objIE As InternetExplorer 'special object variable representing the IE browser 
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element 
Dim y As Integer 'integer variable we'll use as a counter 
Dim result As String 'string variable that will hold our result link 



Dim x As Integer 
    Application.ScreenUpdating = False 
    ' Set numrows = number of rows of data. 
    NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count 
    ' Select cell a1. 
    Range("A1").Select 
    ' Establish "For" loop to loop "numrows" number of times. 
    For x = 1 To NumRows 
    ' Insert your code here. 

    'initiating a new instance of Internet Explorer and asigning it to objIE 
Set objIE = New InternetExplorer 

'make IE browser visible (False would allow IE to run in the background) 
objIE.Visible = True 

'navigate IE to this web page (a pretty neat search engine really) 
objIE.navigate "http://ec.europa.eu/taxation_customs/vies/vatResponse.html" 

'wait here a few seconds while the browser is busy 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

'in the search box put cell "A2" value, the word "in" and cell "C1" value 
objIE.document.getElementById("countryCombobox").Value = "GB" 
objIE.document.getElementById("number").Value = ActiveCell.Value 

'click the 'go' button 
objIE.document.getElementById("submit").Click 

'wait again for the browser 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 



    Dim vatResponse As String 

    vatResponse = objIE.document.getElementById("vatResponseFormTable").getElementsByTagName("tr")(0).Children(0).textContent 

    ActiveCell.Offset(0, 2).Value = vatResponse 

    ' Selects cell down 1 row from active cell. 

    'Next 
    Application.ScreenUpdating = True  

'close the browser 
objIE.Quit 

ActiveCell.Offset(1, 0).Select 
    'End 

Next 

'exit our SearchBot subroutine 
End Sub 

慢慢点击所以基本上在这行代码:Excel宏错误代码424个工作时通过

vatResponse = objIE.document.getElementById("vatResponseFormTable").getElementsByTagName("tr")(0).Children(0).textContent 

我得到一个错误信息说,我有一个错误代码424

+2

请您为您的问题提供更好的解释 – Jordan

+3

请在您的问题中添加更多信息和代码。请阅读https://stackoverflow.com/help/how-to-ask – UGP

+0

我已添加更多详细信息,@UGP –

回答

0

有时页面通过一些脚本在内部加载,因此代码运行速度非常快,因此您在文档中找不到实际获得的html元素。所以不知何故,你必须等到页面完全加载。

请尝试这种方法,看看代码是否运行而不会产生错误。

Sub SearchBot() 

'dimension (declare or set aside memory for) our variables 
Dim objIE As InternetExplorer 'special object variable representing the IE browser 
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element 
Dim vatFormTable As IHTMLElement 
Dim tr As IHTMLElement 
Dim y As Integer 'integer variable we'll use as a counter 
Dim result As String 'string variable that will hold our result link 

Dim x As Integer 
Application.ScreenUpdating = False 
' Set numrows = number of rows of data. 
NumRows = Range("A" & Rows.Count).End(xlUp).Row 
' Select cell a1. 
Range("A1").Select 
' Establish "For" loop to loop "numrows" number of times. 
For x = 1 To NumRows 
    ' Insert your code here. 

    'initiating a new instance of Internet Explorer and asigning it to objIE 
Set objIE = New InternetExplorer 

'make IE browser visible (False would allow IE to run in the background) 
objIE.Visible = True 

'navigate IE to this web page (a pretty neat search engine really) 
objIE.navigate "http://ec.europa.eu/taxation_customs/vies/vatResponse.html" 

'wait here a few seconds while the browser is busy 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 

'in the search box put cell "A2" value, the word "in" and cell "C1" value 
objIE.document.getElementById("countryCombobox").Value = "GB" 
objIE.document.getElementById("number").Value = ActiveCell.Value 

'click the 'go' button 
objIE.document.getElementById("submit").Click 

'wait again for the browser 
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop 


    On Error Resume Next 
    Dim vatResponse As String 
    Do While vatFormTable Is Nothing 
     Set vatFormTable = objIE.document.getElementById("vatResponseFormTable") 
    Loop 

    Do While tr Is Nothing 
     Set tr = vatFormTable.getElementsByTagName("tr")(0) 
    Loop 

    vatResponse = tr.Children(0).innerText 
    ActiveCell.Offset(0, 2).Value = vatResponse 

    ' Selects cell down 1 row from active cell. 

'Next 
Application.ScreenUpdating = True 

'close the browser 
objIE.Quit 

ActiveCell.Offset(1, 0).Select 
    'End 

Next 

'exit our SearchBot subroutine 
End Sub