Larry
Larry

Reputation: 23

On error Handling of runtime error not working

I am not sure why the On Error goto does not handle the following error.
I have a web query set up in cell T10 that I select and change the URL and attempt to pull a table into the sheet.
I do this 20-30 times with different URL's.
Sometimes the data pull takes too long or something else happens that won't allow excel to get the data...
In those cases I want to handle the error and continue.
But I am still getting the runtime error '1004' and debug shows .Refresh BackgroundQuery:=False highlighted.

But shouldn't the On Error grab that and goto line CardDataPullError further down in the sheet?
I can invoke this issue by changing the IP to something other that my target.

On Error GoTo CardDataPullError

    NodeIP = "192.168.210.4"

    Range("T10").Select
    With Selection.QueryTable
        .Connection = "URL;http://" & NodeIP & ":21495/" & Card & "/ispCktDBPage" 
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "3"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

On Error GoTo 0

'below is another section of code that highlights the cell red 
'showing it had a problem pulling the data

GoTo SkipCard ' To skip error handler

CardDataPullError:
X = X
Cells(CardRow, CardCol).Interior.ColorIndex = 3 ' Red

SkipCard:
'other reasons to skip to

Upvotes: 2

Views: 3881

Answers (1)

Johan
Johan

Reputation: 76547

You forgot to put resume in the CardDataPullError error handler.
Therefore the error is not handled.

Change the code as follows:

CardDataPullError:
  X = X
  Cells(CardRow, CardCol).Interior.ColorIndex = 3 ' Red
  Resume SkipCard:

Upvotes: 4

Related Questions