VBA 巧用 On Error GoTo,Exit Function/Exit Sub,Resume

本文介绍了在VBA中如何巧妙使用On Error GoTo、Exit Function和Exit Sub进行错误处理和循环控制。示例代码展示了在读取文件内容时,如何捕获并处理日期转换错误,同时更新数据计数。通过使用这些技巧,可以提高代码的健壮性和逻辑清晰度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 Set TextObj = FileObj.OpenTextFile(strPath)
 Do While Not TextObj.AtEndOfStream
   Dim Line$, DateStr$, Curentdatestr$, Datecountstr$
   Line = TextObj.ReadLine
   Curentdatestr = Left(Line, 10)
   'MsgBox "Curentdatestr : " & Curentdatestr
   'On Error GoTo here
   On Error GoTo errHandler
   d = CDate(Curentdatestr)
   If Not DateStr Like Curentdatestr Then
         cnt1 = 0
         cnt2 = 0
         DateStr = Curentdatestr
         ii = ii + 1
   End If
   If InStr(Line, Target1) Then
      If InStr(Line, Target3) Then
        cnt1 = cnt1 + 1
      E

优化以下VBA 代码:目前筛选数据的速度太慢了,要加快。Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim filePath As String Dim wb1 As Workbook, wb2 As Workbook Dim ws1 As Worksheet, ws2 As Worksheet Dim keyword1 As Variant, keyword2 As Variant Dim lastRow As Long, visibleRows As Long If Target.Column = 3 And Target.Row > 1 Then Cancel = True keyword1 = Target.Value keyword2 = Cells(Target.Row, 1).Value If IsEmpty(keyword1) Or IsEmpty(keyword2) Then MsgBox "所选单元格内容不能为空!", vbExclamation Exit Sub End If ' path filePath = "C:\Users\Skyler.zheng\OneDrive - Taikoo Engine Services (Xiamen) Company Limited\Desktop\SO list-Brg.xlsm" On Error Resume Next Dim localPath As String localPath = GetLocalFilePath(filePath) If localPath = "" Then localPath = filePath End If On Error GoTo 0 ' 打开表格2 Application.ScreenUpdating = False Application.DisplayAlerts = False On Error GoTo ErrorHandler ' 尝试打开工作簿 Set wb2 = Workbooks.Open(localPath, ReadOnly:=True) Set ws2 = wb2.Sheets("ZEGST001") ' 清除现有筛选 If ws2.AutoFilterMode Then ws2.AutoFilterMode = False End If ' 获取表格2的最后一行 lastRow = ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row ' 设置筛选条件 ws2.Range("A1").AutoFilter ' 筛选B列(与表格1的F列匹配) ws2.Range("A1").AutoFilter Field:=2, Criteria1:=keyword1 ' 筛选D列(与表格1的A列匹配) ws2.Range("A1").AutoFilter Field:=4, Criteria1:=keyword2 ' 检查是否有匹配的记录 On Error Resume Next visibleRows = ws2.Range("B2:B" & lastRow).SpecialCells(xlCellTypeVisible).Cells.Count On Error GoTo ErrorHandler If visibleRows > 0 Then ' 找到匹配记录,跳转到表格2并选中第一条记录 Dim firstVisibleRow As Long For firstVisibleRow = 2 To lastRow If Not ws2.Rows(firstVisibleRow).Hidden Then ws2.Rows(firstVisibleRow).Select ws2.Activate Exit For End If Next firstVisibleRow Else ' 未找到匹配记录,关闭表格2并提示 wb2.Close SaveChanges:=False MsgBox "No data", vbInformation End If CleanUp: Application.DisplayAlerts = True Application.ScreenUpdating = True Exit Sub ErrorHandler: ' 错误处理:关闭已打开的工作簿并显示错误信息 If Not wb2 Is Nothing Then wb2.Close SaveChanges:=False End If MsgBox "处理过程中出现错误: " & Err.Description, vbCritical Resume CleanUp End If End Sub Function GetLocalFilePath(onedrivePath As String) As String ' 将OneDrive路径转换为本地路径 ' 注意:此函数可能无法处理所有情况,具体取决于OneDrive同步设置 On Error Resume Next ' 尝试解析URL获取文件名 Dim fileName As String Dim parts() As String parts = Split(onedrivePath, "/") fileName = parts(UBound(parts)) ' 获取OneDrive本地同步文件夹路径 Dim localOneDrivePath As String localOneDrivePath = Environ("USERPROFILE") & "\OneDrive\文档" ' 根据实际情况调整 ' 构建本地文件路径 GetLocalFilePath = localOneDrivePath & "\" & fileName ' 检查文件是否存在 If Dir(GetLocalFilePath) = "" Then GetLocalFilePath = "" ' 文件不存在,返回空字符串 End If On Error GoTo 0 End Function
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值