错误处理运行时错误不起作用

发布于 2024-10-31 17:04:42 字数 1305 浏览 2 评论 0原文

我不确定为什么 On Error goto 不处理以下错误。
我在单元格 T10 中设置了一个 Web 查询,我选择并更改 URL 并尝试将表格拉入工作表中。
我使用不同的 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海螺姑娘 2024-11-07 17:04:42

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

更改代码如下:

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

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:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文