通过循环刷新工作表数据

时间:2013-06-19 18:00:21

标签: excel vba

我让我的宏工作得非常漂亮,但是我需要循环运行100次,然后我第二次运行

Run-time error '-2147417848 (80010108)':

Automation error
The object invoked has disconnected from its clients.

我尝试过以下选项:

Application.CalculateFullRebuild
'ActiveSheet.EnableCalculation = False
'ActiveSheet.EnableCalculation = True
'Application.Calculation = xlCalculationManual
'Application.Calculation = xlCalculationAutomatic
'Application.Wait Time + TimeSerial(0, 0, 1)
'ActiveSheet.Calculate

然而他们都会循环两次,并且在第3次我得到上述错误。以下是我的代码。

Private Sub Populate_Click()
    Dim i As Long
    Dim a As Long
    Dim x As Long
    Dim b As Long

    Dim IE As Object
    Dim objElement As Object
    Dim objElementA As Object
    Dim objCollection As Object
    Dim objCollectionA As Object


x = 0
While x < 101

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "website" 'make sure you've logged into the page

Do
DoEvents
Loop Until IE.READYSTATE = 3
Do
DoEvents
Loop Until IE.READYSTATE = 4

Application.CalculateFullRebuild
'ActiveSheet.EnableCalculation = False
'ActiveSheet.EnableCalculation = True
'Application.Calculation = xlCalculationManual
'Application.Calculation = xlCalculationAutomatic
'Application.Wait Time + TimeSerial(0, 0, 1)
'ActiveSheet.Calculate

Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value)
Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value)

Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
    If objCollection(i).Type = "button" And _
        objCollection(i).Value = "Prefill" Then
            Set objElement = objCollection(i)
    End If
    i = i + 1
Wend
objElement.Click

Application.Wait Time + TimeSerial(0, 0, 2)

Set objCollectionA = IE.document.getElementsByTagName("input")
a = 0
While a < objCollectionA.Length
    If objCollectionA(a).Type = "submit" And _
        objCollectionA(a).Value = "OK" Then
            Set objElementA = objCollectionA(a)
    End If
    a = a + 1
Wend
objElementA.Click

Do
DoEvents
Loop Until IE.READYSTATE = 3
Do
DoEvents
Loop Until IE.READYSTATE = 4
IE.Quit
x = x + 1
Wend

End Sub

1 个答案:

答案 0 :(得分:0)

Do
DoEvents
Loop Until IE.READYSTATE = 3

我删除了那段代码,现在它运行正常。