Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4835|回复: 11

[已解决]为什么On Error Goto Line对定位、查找失效?

[复制链接]
发表于 2014-3-10 17:58 | 显示全部楼层 |阅读模式
如附件所示。我想将含有公式错误值的工作表统计出来。但在循环过程中,如果工作表中的公式无错误值,则定位不到单元格,会弹出错误提示,在前面加上ON ERROR GOTO LINE无效。类似地,用Find方法查找如果找不到时ON ERROR GOTO LINE也没法阻止弹出程序中断警告。
请问如何避免这类错误?谢谢各位了先。
最佳答案
2014-3-10 21:08
excel盲2013 发表于 2014-3-10 20:40
你的解决方案1有效,谢谢!
但方案二定义变量的办法还是会弹出警告。如果前面加上resume next.最后得出的 ...

Sub 错误处理3()
Dim str As String
Dim sht As Worksheet
On Error Resume Next
For Each sht In Worksheets
    Err.Clear
    Set rng = sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
    If Err.Number = 0 Then
        str = str & sht.Name & Chr(10)
    End If
Next sht
MsgBox "公式有错误值的工作表有:" & Chr(10) & str
End Sub

错误处理 on error goto line.rar

8 KB, 下载次数: 10

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-3-10 18:07 | 显示全部楼层
on error resume next
用这句继续执行
Application.DisplayAlerts = False
用这句关闭警告框
回复

使用道具 举报

 楼主| 发表于 2014-3-10 18:17 | 显示全部楼层
风林火山 发表于 2014-3-10 18:07
on error resume next
用这句继续执行
Application.DisplayAlerts = False
  1. Sub 错误处理()
  2. Dim str As String
  3. Dim sht As Worksheet
  4.     For Each sht In Worksheets
  5.         On Error GoTo a
  6.         Application.DisplayAlerts = False
  7.         'On Error Resume Next
  8.         If sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Count > 0 Then
  9.             str = str & sht.Name & Chr(10)
  10.      
  11.         End If
  12. a:  Next sht
  13. Application.DisplayAlerts = True

  14. MsgBox "公式有错误值的工作表有:" & Chr(10) & str

  15. End Sub
复制代码
之前试过,木有用唉。那判断有没有错误值除了定位,还有别的办法没?
回复

使用道具 举报

 楼主| 发表于 2014-3-10 18:19 | 显示全部楼层
风林火山 发表于 2014-3-10 18:07
on error resume next
用这句继续执行
Application.DisplayAlerts = False
  1. Sub 错误处理()
  2. Dim str As String
  3. Dim sht As Worksheet
  4.     For Each sht In Worksheets
  5.         On Error Resume Next
  6.         Application.DisplayAlerts = False
  7.         'On Error Resume Next
  8.         If sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Count > 0 Then
  9.             str = str & sht.Name & Chr(10)
  10.      
  11.         End If
  12. a:  Next sht
  13. Application.DisplayAlerts = True

  14. MsgBox "公式有错误值的工作表有:" & Chr(10) & str

  15. End Sub
复制代码
用ON ERROR RESUME NEXT,最后会把所有工作表都列举出来。
回复

使用道具 举报

发表于 2014-3-10 19:52 | 显示全部楼层
excel盲2013 发表于 2014-3-10 18:19
用ON ERROR RESUME NEXT,最后会把所有工作表都列举出来。

如果公式没有错误,那就退出循环
回复

使用道具 举报

发表于 2014-3-10 20:04 | 显示全部楼层
风林火山 发表于 2014-3-10 19:52
如果公式没有错误,那就退出循环

  • If sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Count <= 0 Then
        

            else
  •      str = str & sht.Name & Chr(10)
  • End If
这样应该就可以了


还有一个办法就是定义一个变量

dim Rng

set rng = sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
if typename(rng) = "Range" then
str = str & sht.Name & Chr(10)
end if

用一个可变类型的变量来赋值,这样就不会出错,也不用防错语句
只要后面加上 .count就会出错。
回复

使用道具 举报

发表于 2014-3-10 20:27 | 显示全部楼层
cbg2008 发表于 2014-3-10 20:04
  • If sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Count

  • 谢谢师傅,记住了
    回复

    使用道具 举报

     楼主| 发表于 2014-3-10 20:40 | 显示全部楼层
    cbg2008 发表于 2014-3-10 20:04
  • If sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Count

  • 你的解决方案1有效,谢谢!
    但方案二定义变量的办法还是会弹出警告。如果前面加上resume next.最后得出的含公式错误值的工作表列表是不正确的。可在附件里运行下试试。
    1. Sub 错误处理2()‘有效
    2. Dim str As String
    3. Dim sht As Worksheet
    4.     For Each sht In Worksheets
    5.         On Error Resume Next
    6.         If sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors).Count <= 0 Then '
    7.             'do nothing
    8.             Else
    9.             str = str & sht.Name & Chr(10)
    10.      
    11.         End If
    12. a:  Next sht


    13. MsgBox "公式有错误值的工作表有:" & Chr(10) & str

    14. End Sub
    复制代码
    1. Sub 错误处理3()’达不到目的。
    2. Dim str As String
    3. Dim sht As Worksheet
    4.     For Each sht In Worksheets
    5.         On Error Resume Next
    6.         Dim rng: Set rng = sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
    7.         
    8.         If TypeName(rng) = "Range" Then

    9.             str = str & sht.Name & Chr(10)
    10.      
    11.         End If
    12. a:  Next sht


    13. MsgBox "公式有错误值的工作表有:" & Chr(10) & str

    14. End Sub
    复制代码
    回复

    使用道具 举报

    发表于 2014-3-10 21:08 | 显示全部楼层    本楼为最佳答案   
    excel盲2013 发表于 2014-3-10 20:40
    你的解决方案1有效,谢谢!
    但方案二定义变量的办法还是会弹出警告。如果前面加上resume next.最后得出的 ...

    Sub 错误处理3()
    Dim str As String
    Dim sht As Worksheet
    On Error Resume Next
    For Each sht In Worksheets
        Err.Clear
        Set rng = sht.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
        If Err.Number = 0 Then
            str = str & sht.Name & Chr(10)
        End If
    Next sht
    MsgBox "公式有错误值的工作表有:" & Chr(10) & str
    End Sub

    回复

    使用道具 举报

     楼主| 发表于 2014-3-10 21:11 | 显示全部楼层
    cbg2008 发表于 2014-3-10 21:08
    Sub 错误处理3()
    Dim str As String
    Dim sht As Worksheet

    万分感谢!
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

    GMT+8, 2024-5-18 01:55 , Processed in 0.276623 second(s), 13 queries , Gzip On, Yac On.

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表