How to reset "on Error" within a loop?

treygon

New Member
Joined
Aug 13, 2010
Messages
16
I am using a do until loop to bring in multiple webpages using a variable. After a webpage is brought into a temporary spreadsheet, I am searching for a specific phrase ("Last 3 years"). If that phrase is not present, I want it to delete that temp sheet and the loop to the next page. If it is there, then I want it to continue to the next search ("Fund Report Card") and then the rest of the code. However, when I use the following code, my error handling only works once and then I get a "run time error 91" on the second time it can't find the first phrase. How can I reset the error handling so it doesn't get hung up after the second time it errors out?

Code:
Sub getFundGrades2()
   
   
        
      
    cNum = 1
    Do Until cNum = 100000
        
        On Error GoTo EndOfSection
       
        Sheets.Add.Name = "DataTemp"
        Sheets("DataTemp").Select
        
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://www.testingweb.com/securityreport.aspx?id=" & cNum, Destination:= _
            Range("$B$2"))
            .Name = "securityreport.aspx?id=" & cNum
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingNone
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
        
    Cells.Find(What:="Last 3 Years", After:=[B1], LookIn:=xlFormulas, _
            LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
    
       
    Cells.Find(What:="Fund Report Card", After:=[A1], LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Offset(3, 0).Select
        
    fgSymbol = Left(Selection, WorksheetFunction.Find("-", Selection) - 2)
  
    fgRow = WorksheetFunction.CountA(Sheets("FG_Database").Range("FG_Count")) + 3
    
    Sheets("FG_Database").Cells(fgRow, 2) = fgSymbol
    Sheets("FG_Database").Cells(fgRow, 3) = cNum
    
    
EndOfSection:

On Error GoTo 0
    
    cNum = cNum + 1

    Worksheets("DataTemp").Select
    Application.DisplayAlerts = False
    Worksheets("DataTemp").Delete
    Application.DisplayAlerts = True
    
    Application.Goto Reference:=Worksheets("FG_Database").Cells(fgRow, 2), Scroll:=False

    Loop
    
    
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
No way for me to test this, but the syntax looks correct:

Code:
Option Explicit

Sub getFundGrades2()
Dim cNum As Long, fgRow As Long
Dim fgSymbol As String
Dim LASTrng As Range, FUNDrng As Range

Application.DisplayAlerts = False
Application.ScreenUpdating = False

If Not Evaluate("ISREF(DataTemp!A1)") Then
    Sheets.Add.Name = "DataTemp"
Else
    Sheets("DataTemp").Activate
    Cells.Clear
End If

On Error GoTo EndOfSection

For cNum = 1 To 100000
        
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://www.testingweb.com/securityreport.aspx?id=" & cNum, _
            Destination:=Range("$B$2"))
        .Name = "securityreport.aspx?id=" & cNum
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
        
    Set LASTrng = Cells.Find(What:="Last 3 Years", After:=[B1], LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    Set FUNDrng = Cells.Find(What:="Fund Report Card", After:=[A1], LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        
    fgSymbol = Left(FUNDrng.Offset(3), WorksheetFunction.Find("-", FUNDrng.Offset(3)) - 2)
  
    fgRow = WorksheetFunction.CountA(Sheets("FG_Database").Range("FG_Count")) + 3
    
    Sheets("FG_Database").Cells(fgRow, 2) = fgSymbol
    Sheets("FG_Database").Cells(fgRow, 3) = cNum
    
EndOfSection:
    Cells.Clear

Next cNum

Sheets("DataTemp").Delete
Application.Goto Reference:=Worksheets("FG_Database").Cells(fgRow, 2), Scroll:=False
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
EndOfSection:

    Err.Clear
    
    cNum = cNum + 1

    Worksheets("DataTemp").Select
    Application.DisplayAlerts = False
    Worksheets("DataTemp").Delete
    Application.DisplayAlerts = True
    
    Application.Goto Reference:=Worksheets("FG_Database").Cells(fgRow, 2), Scroll:=False

    Loop
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top