vba发生错误时如何重启for循环

时间:2017-10-04 17:38:30

标签: excel-vba vba excel

我正在使用VBA进行IE自动化(基本上我打开IE并从工作表转到特定的URL,然后使用工作表中的凭据登录,然后从网页提取数据到excel)这必须发生在20个网站上,所以我添加了循环,它工作正常。

我想要的是,如果在循环中发生任何错误,则循环必须重新启动。 我也试过“在错误得到0,错误得到-1”但它没有工作。 下面是我的代码 - 请原谅我编码不好我是VBA的新手。

    Sub Get_Data()
    Sheets("Sheet2").Select
    Range("E2").Select
    Range("H6:H120").ClearContents
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.application")
    IE.Visible = True
    Dim E As Long
    Dim S As Long
    E = Range("A" & Rows.Count).End(xlUp).Row

JumpToHere:
    For j = S To E
        S = Range("H" & Rows.Count).End(xlUp).Row
        Sheets("Sheet2").Select
        Range("E" & S).Select
        ActiveCell.Offset(1, -2).Select
        Dim X As Variant
        X = ActiveCell.Value
        IE.navigate X
        Do
            If IE.ReadyState = 4 Then
                IE.Visible = True
                Exit Do
            Else
                DoEvents
            End If
        Loop

        ActiveCell.Offset(0, 1).Select
        Dim Y As Variant
        Y = ActiveCell.Value
        IE.document.all("username").Value = Y
        ActiveCell.Offset(0, 1).Select
        Dim Z As Variant
        Z = ActiveCell.Value
        IE.document.all("password").Value = Z
        IE.document.all("merchant_login_submit_button").Click
        Application.Wait (Now + TimeValue("0:00:8"))

        Set ElementCol = IE.document.getElementsByTagName("span")
        For Each link In ElementCol
            If link.innerHTML = "Authentication Failed" Then
                ActiveCell.Offset(0, 3).Value = "Authentication Failed"
                GoTo JumpToHere
            End If
        Next

        Set tags = IE.document.getElementsByTagName("input")
        For Each tagx In tags
            If tagx.Value = "Continue to Control Panel" Then
                tagx.Click
                Application.Wait (Now + TimeValue("0:00:3"))
                Exit For
            End If
        Next


        Set ElementCol = IE.document.getElementsByTagName("a")
        For Each link In ElementCol
            If link.innerHTML = "Reports" Then
                link.Click
            End If
        Next
        Application.Wait (Now + TimeValue("0:00:06"))
        Dim checkdate As Integer
        checkdate = Format(Date, "dd") - 1

        IE.document.getElementById("snapshot_group_by").Value = "payment_processor"
        IE.document.getElementById("snapshot_end_date_day").Value = checkdate
        IE.document.all("reports_submit_button").Click
        Application.Wait (Now + TimeValue("0:00:3"))

        Dim ws As Worksheet
        Dim rng As Range
        Dim tbl As Object
        Dim rw As Object
        Dim cl As Object
        Dim tabno As Long
        Dim nextrow As Long
        Dim I As Long

        Set ws = Worksheets.Add

        For Each tbl In IE.document.getElementsByTagName("TABLE")
            tabno = tabno + 1
            nextrow = nextrow + 1
            Set rng = ws.Range("B" & nextrow)
            rng.Offset(, -1) = "Table " & tabno
            For Each rw In tbl.Rows
                For Each cl In rw.Cells
                    rng.Value = cl.outerText
                    Set rng = rng.Offset(, 1)
                    I = I + 1
                Next cl
                nextrow = 0
                Set rng = rng.Offset(1, -I)
                I = 0
            Next rw
        Next tbl

        ws.Cells.ClearFormats
        Sheets("Sheet2").Select
        ActiveCell.Offset(0, 3).Value = ActiveSheet.Previous.Range("F4")
        Application.DisplayAlerts = False
        ActiveSheet.Previous.Delete
        Application.DisplayAlerts = True

        Set ElementCol = IE.document.getElementsByTagName("a")
        For Each link In ElementCol
            If link.innerHTML = "Logout" Then
                link.Click
            End If
        Next
    Next j

End Sub

1 个答案:

答案 0 :(得分:0)

听起来你真正的问题是你的代码没有正常等待。在您调用Application.Wait或任何元素IE.Navigate或形成.Click事件时,请使用正确的等待循环,而不是.Submit

VBA HTML not running on all computers

否则,您的代码中没有任何活动的错误捕获。使用On Error语句包装循环,如下所示。

第一个,On Error GoTo MyErrorHandler指示程序在循环中遇到错误时该怎么做。如果出现错误,MyErrorHandler标签下的代码将会执行,并在NextJ标签处继续执行。循环结束后,On Error GoTo 0返回正常(即无)错误处理。在循环外发生的任何错误仍会在运行时引发异常。

Option Explicit
Sub Get_Data()
    '// Dim your variables

    '// Executable code starts here

JumpToHere:    
    For j = S To E
        On Error GoTo MyErrorHandler
        ' Now ANY ERROR, ANYWHERE in the loop will go to the error handler

NextJ:
    Next j
    '// Code below this line won't be subject to the error handler
    On Error GoTo 0

    '// more code if you have it

    ' Exit gracefully if there was no error:
    Exit Sub

    '// Here is the error handler:
    MyErrorHandler:
        Err.Clear()
        Resume NextJ
End Sub

如果您真的想重新开始循环,那么请NextJ代替Resume JumpToHere

相关问题