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

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

问题描述

我尽可能多地上网查看(除了 Microsoft 支持网站,该网站因某种原因在工作中被屏蔽).我试图简单地跳过一个错误.我在此处编写的代码已简化,但应该以相同的方式工作.

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.

我的代码应该做什么:我的一个潜艇在循环中创建形状并命名它们(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 error resume,我仍然会遇到相同的错误!我做错了什么?

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 编辑器中,选择 Tools ->选项.在打开的窗口中,选择 General 选项卡,然后选择 Break on Unhandled Errors 单选按钮.这应该允许 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.

我怀疑您选择了Break on All Errors.

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

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