ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > Excel VBA > 如何利用Err对象跳过错误

如何利用Err对象跳过错误

作者:绿色风 分类: 时间:2022-08-17 浏览:100
楼主
芐雨
原案例:附件中有很多工作簿,利用ADO的SQL union all语句把路径下所有工作簿的内容合并到一个工作表中,因工作簿中可能存在没有内容的工作表,select * from 空表时就会出现错误。
解决思路
在语句执行前加入 On Error Resume Next语句,使得程序忽略”运行时错误“而继续执行,当该语句初执行之后,则可以通过判断Err对象的Number属性是否为0而得知工作表是否为空表。

代码如下:(注释部分)
  1. Sub 批量合并()
  2.     Dim cnn As Object, rs As Object, SQL$, Mypath$, MyFile$, m&, sht As Worksheet, sh As Worksheet, Temp$
  3.     On Error Resume Next '****错误时跳过*****
  4.     With Application.FileDialog(msoFileDialogFolderPicker)
  5.         .InitialFileName = ThisWorkbook.Path & "\"
  6.         If .Show = False Then Exit Sub
  7.         Mypath = .SelectedItems(1) & "\"
  8.     End With
  9.     Application.ScreenUpdating = False
  10.     Application.DisplayAlerts = False
  11.     Set sht = ActiveSheet
  12.     Set sh = Sheets.Add
  13.     MyFile = Dir(Mypath & "*.xlsx")
  14.     sht.Range("A2:N1048576").ClearContents
  15.     Do While MyFile <> ""
  16.         If InStr(MyFile, ThisWorkbook.Name) = 0 Then
  17.             Set cnn = CreateObject("ADODB.Connection")
  18.             cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile
  19.             Set rs = cnn.OpenSchema(20)    '
  20.             Do Until rs.EOF
  21.                 If rs.Fields("TABLE_TYPE") = "TABLE" Then
  22.                     s = Replace(rs("TABLE_NAME").Value, "'", "")
  23.                     If Right(s, 1) = "$" Then
  24.                         m = m + 1
  25.                         If m > 49 Then
  26.                             sht.Range("A1048576").End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  27.                             m = 1
  28.                             SQL = ""
  29.                         End If
  30.                         Temp = "select top 1 *  from [Excel 12.0;Database=" & Mypath & MyFile & ";].[" & s & "A:N] where 车间 IS NOT NULL "
  31.                         sh.Range("A1").CopyFromRecordset cnn.Execute(Temp) '****对象中的内容复制到工作表****
  32.                         If Err.Number = 0 Then   '****等于0时,即上一行代码没有出错,可以写入SQL语句中,反之跳过****
  33.                             If Len(SQL) Then SQL = SQL & " union all "
  34.                             SQL = SQL & "select * from [Excel 12.0;Database=" & Mypath & MyFile & ";].[" & s & "A:N] where 车间 IS NOT NULL "
  35.                         End If
  36.                         Err.Clear
  37.                     End If
  38.                 End If
  39.                 rs.MoveNext
  40.             Loop
  41.         End If
  42.         MyFile = Dir()
  43.     Loop
  44.     If Len(SQL) Then sht.Range("A1048576").End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  45.     rs.Close
  46.     cnn.Close
  47.     Set rs = Nothing
  48.     Set cnn = Nothing
  49.     sh.Delete
  50.     Application.ScreenUpdating = True
  51.     Application.DisplayAlerts = True
  52. End Sub


知识点:
Err对象是存在于Excel程序中的全局对象 ,其伴随着Excel程序而始终存在。该对象记录VBA程序”运行时错误“的信息。 Err对象的Numer属性记录”运行时错误“的错误号。当没有错误发生时,属性为0。当错误发生时,为该错误对应的错误号。
例:
  1. Sub 利用错误号判断工作表是否存在()
  2.     Dim sht As Worksheet
  3.     Err.Clear  '消除现有错误
  4.     On Error Resume Next    '忽略错误继续执行
  5.     Set sht = Worksheets(工作表名)
  6.     If Err.Number = 0 Then   '判断是否发生错误
  7.         MsgBox "此工作表存在"
  8.     Else
  9.         MsgBox "此工作表不存在"
  10.     End If
  11. End Sub

附件:

车间收入.zip



免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap