错误处理运行时错误无法正常工作

时间:2011-04-12 18:08:15

标签: error-handling excel-vba runtime vba excel

我不确定为什么On Error goto无法处理以下错误 我在单元格T10中设置了一个网页查询,我选择并更改网址并尝试将表格拉入表格。
我用不同的URL做了20到30次 有时数据拉动时间过长或发生其他不允许excel获取数据的事情...... 在那些情况下,我想处理错误并继续。
但我仍然得到运行时错误'1004'并且调试显示.Refresh BackgroundQuery:=False突出显示。

但On Error是否应该在表单中进一步向下抓取并转到行CardDataPullError? 我可以通过将IP更改为我的目标之外的其他内容来调用此问题。

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

1 个答案:

答案 0 :(得分:4)

您忘记将resume放入CardDataPullError错误处理程序中 因此,不处理错误。

按如下方式更改代码:

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