EXCEL表格中使用VBA错误捕捉On Error Goto的两种方法
时间:2023-08-27 15:34:46
EXCEL怎么使用VBA错误捕捉On Error Goto?今天我们就来给大家介绍使用VBA错误捕捉On Error Goto的两种方法,都很简单,还有实例说明,需要的朋友可以参考下
VBA中错误捕捉的方法主要有两种
第一种捕捉方法:
1、语法:
On Error GoTo Err_Handle '如果遇到错误就跳转到错误处理代码
语句xxxxx
Exit Sub/Function(如果没有这条语句,即使没有错误也会执行错误处理语句)
Err_Handle: 语句 xxxxxx
2、举例:
Sub 查找数字() ’本段代码查找电子表格中有没有数字12
On Error GoTo Err_Handle
MsgBox Cells.Find(12).Address
Exit Sub '如果没有这个退出过程语句,会一直执行下去
Err_Handle:
MsgBox ("不存在该数字")
End Sub
(1)、电子表中数据和代码
(2)、查找12的结果:
(3)、查找13的结果:
第二种捕捉方法:
1、语法:
On Error Resume Next '如果遇到错误,不管错误,继续往下执行,但如果嵌套了其他错误处理语句,这些错误处理语句还是会按照自己规则运行
语句xxxx
On Error GoTo 0 ’结束错误捕捉
2、举例代码:
Sub 查找数字()
Dim i As Integer
On Error Resume Next '遇到错误向下执行
MsgBox Cells.Find(13).Address
On Error GoTo Err_Handle
i = 10 / 0 ’会跳转到错误处理语句,而不会运行 Exit sub
Exit Sub
Err_Handle:
MsgBox i
On Error Resume Next
End Sub
3、结果说明和图例:
excel表中没有13这个数字,但是不会报错也不会有弹窗
在On Error Resume Next 和 Err_Handle之间代码,还是可以发挥错误捕获功能,因此可以出现下面弹窗:
excel中怎么利用VBA制作一个登录数据验证窗口?
Excel2007怎么开启信任对VBA工程对象模型的访问?
excel怎么利用vba实现批量套打快递单批量打印单据?
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何为word文档添加网页地址超链接
![](https://img.aspxhome.com/file/2023/8/33028_0s.jpg)
多种方法实现在word文档中的小方框里面打钩符号和打叉符号
![](https://img.aspxhome.com/file/2023/8/33978_0s.png)
U盘接口坏了修复有技巧
![](https://img.aspxhome.com/file/2023/2/a291615_0s.png)
Foxmail如何添加qq邮箱?Foxmail添加qq邮箱的方法步骤
![](https://img.aspxhome.com/file/2023/8/a332990_0s.jpg)
如何开始使用Redis Desktop Manager for Mac?RDM Mac使用方法
![](https://img.aspxhome.com/file/2023/4/a213114_0s.png)
软件打开报FM20.DLL文件错误问题解决方案
![](https://img.aspxhome.com/file/2023/9/a342301_0s.jpg)
如何在PowerPoint2007备注页中添加图片设置(图文)
![](https://img.aspxhome.com/file/2023/10/a345023_0s.jpg)
Excel使用统计功能COUNTIF快速制作统计考勤表数据
![](https://img.aspxhome.com/file/2023/0/a143170_0s.gif)
Win10 Defender不再“花瓶”:卡巴斯基不淡定了!
![](https://img.aspxhome.com/file/2023/5/a315892_0s.png)
Win10系统如何从资源管理器中移除/找回OneDrive
![](https://img.aspxhome.com/file/2023/28/a242921_0s.jpg)
如何在电脑硬件不满Win11最低更新要求下更新Win11版本?
![](https://img.aspxhome.com/file/2023/27/a237744_0s.jpg)
Win10照片查看器内存不足怎么解决?照片查看器内存不足解决方法
![](https://img.aspxhome.com/file/2023/1/a280736_0s.jpg)
Win7系统如何安装ESD文件?
![](https://img.aspxhome.com/file/2023/27/a231145_0s.jpg)
Win10如何设置安全等级?电脑安全等级设置教程
![](https://img.aspxhome.com/file/2023/26/a220739_0s.jpg)
电脑访问网络共享提示“找不到网络路径”怎么办?
![](https://img.aspxhome.com/file/2023/6/a326520_0s.jpg)
win10开机黑屏什么都不显示?Win10开机黑屏无法进入系统
![](https://img.aspxhome.com/file/2023/30/a270509_0s.png)
Windows10系统怎么一键重装系统?石大师一键重装系统步骤
![](https://img.aspxhome.com/file/2023/1/a275358_0s.jpg)
excel文件已损坏无法打开怎么办?
![](https://img.aspxhome.com/file/2023/30/a268108_0s.png)
压缩文件损坏怎么办 修复损坏文件的操作方法
![](https://img.aspxhome.com/file/2023/1/a273487_0s.jpg)
如何不让系统更改Win10默认打印机
![](https://img.aspxhome.com/file/2023/30/a263276_0s.png)