通过循环刷新工作表数据



我的宏工作得很好,但是我需要循环运行 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

然而,它们都会循环两次,第三次我得到上述错误。下面是我的代码。

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
Do
DoEvents
Loop Until IE.READYSTATE = 3

我删除了那段代码,现在它按预期运行。

最新更新