VBA Excel简单的错误处理 [英] VBA Excel simple Error Handling

查看:156
本文介绍了VBA Excel简单的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽可能多地看了网络(除了微软支持网站,由于某些原因在工作中被阻止)。我试图简单地跳过一个错误。我在这里写的代码是简化的,但应该以同样的方式工作。

I have looked online as much as I could (except for the Microsoft support website, which is blocked at work for some reason). I am trying to simply skip an error. My code written here is simplified but should work the same way.

我的代码应该做什么:
我的一个subs创建循环中的形状并将它们命名(btn_1,btn_2等)。但是在创建它们之前,它调用一个尝试删除它们的子代,以便不创建重复项。此子循环遍历(btn_1,btn_2等),并使用以下方式删除形状:

What my code is supposed to do: One of my subs creates shapes in a loop and names them (btn_1, btn_2, etc). But before creating them, it calls a sub that tries to delete them so as not to create duplicates. This sub loops through (btn_1, btn_2, etc) and deletes the shapes using:

for i = 1 to (a certain number)
    Set shp = f_overview.Shapes("btn_" & i)
    shp.delete
next

当然,它的形状不能被删除,因为它根本不存在。我发现大多数时候,推荐的修复是在设置形状之前添加(接下来的错误恢复),因为我收到一个不存在的错误。我已经在循环中尝试了,循环之前等等,如下所示:

Of course, it happens that the shape cannot be deleted because it simply does not exist. I have found that most of the time, the reccomended fix is to add (on error resume next) before setting the shape, as I get an error saying it does not exist. I have tried it inside the loop, before the loop, etc, like so:

for i = 1 to (a certain number)
    On Error Resume Next
    Set shp = f_overview.Shapes("btn_" & i)
    shp.delete
next

据我所知,如果形状不存在,它应该是循环的,但是我仍然得到相同的错误,无论我是否添加On错误恢复下一步!我做错了什么?

As far as I understand it is supposed to loop right through if the shape doesn't exist, but I still get the same error whether or not I add the On error resume next! What am I doing wrong?

编辑:形状确实存在时没有错误。

There is no error when the shapes do exist.

推荐答案

听起来你错误的陷阱选项设置错误。在VBA编辑器中,选择工具 - >选项。在打开的窗口中,选择常规选项卡,然后选择未处理错误单选按钮中的 Break。这应该允许Excel正确处理 On Error Resume Next 命令。

It sounds like you have the wrong error trapping option set. Within the VBA Editor, Select Tools -> Options. In the window that opens, select the General tab, and pick the Break on Unhandled Errors radio button. This should allow Excel to properly process the On Error Resume Next command.

我怀疑你有$ $ c>所有错误中断

I suspect that you have Break on All Errors selected.

这篇关于VBA Excel简单的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆