【问题标题】:Excel macro search ends in error when nothing found未找到任何内容时,Excel 宏搜索以错误结束
【发布时间】:2018-09-27 09:37:05
【问题描述】:

只要搜索找到数据,我的案例 1 excel 宏代码就会运行,但是当搜索结果中没有任何内容时,就会出现所述错误。所以我尝试放入一个“集合”,参见案例 2……但该案例在任何搜索中都会爆炸。

案例 1:运行时错误“91”:对象变量或未设置块变量

 Cells.Find(What:=sCurrentISOtext & "_", After:=ActiveCell, _
            LookIn:=xlFormulas, LookAt :=xlWhole , _
           SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
            MatchCase:= False, SearchFormat:=False).Activate

案例 2:运行时错误“424”:需要对象

  Dim c As Range 

  Set c = Cells.Find(What:=sCurrentISOtext & "_", After:=ActiveCell, _
                     LookIn:=xlFormulas, LookAt :=xlWhole, _
                     SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
                     MatchCase:= False, SearchFormat:=False).Activate

你的意思是这样的??它仍然失败。

案例 3:运行时错误“91”:对象变量或未设置块变量

Dim c As Range      

c = Cells.Find(What:=sCurrentISOtext & "_", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole = 0, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase _
        :=False, SearchFormat:=False)

If Not c Is Nothing Then   
    c.Activate     
    ' and do something here < > 
End If 

【问题讨论】:

  • 你的意思是这样的??它仍然失败。案例 3:运行时错误“91”:对象变量或未设置块变量 Dim c As Range c = Cells.Find(What:=sCurrentISOtext & "_", After:=ActiveCell, LookIn:=xlFormulas, _ LookAt :=xlWhole = 0, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase _ :=False, SearchFormat:=False) If Not c Is Nothing Then c.Activate ' 并在此处执行操作 End If
  • tldr: 如果在此期间重新定义 Find 操作,则无法 FindNext。
  • 如果您坚持混淆您的问题,那么 cmets 将不再是 20 个问题的游戏。 edit您的问题包括这些说明。

标签: excel vba


【解决方案1】:

这自然会失败,您在 null(失败)结果上调用“激活” - 所以在运行时没有什么可以激活的。您必须包含在 If 语句中 -

Dim c As Range

Set c = Cells.Find(What:=sCurrentISOtext & "_", _
                   After:=ActiveCell, _
                   LookIn:=xlFormulas, _
                   LookAt:=xlWhole = 0, _
                   SearchOrder:=xlByColumns, _
                   SearchDirection:=xlNext, _
                   MatchCase:= False, _
                   SearchFormat:=False)

If c Is Nothing Then
    'do something
Else
    c.Activate
End If

【讨论】:

    【解决方案2】:

    这是我在赶时间时使用的一个丑陋的解决方法 - 有更优雅的错误陷阱,但这可以完成。

    On Error GoTo notFound
    Dim c As Range
    
    Set c = Cells.Find(What:=sCurrentISOtext & "_", _
                       After:=ActiveCell, _
                       LookIn:=xlFormulas, _
                       LookAt:=xlWhole = 0, _
                       SearchOrder:=xlByColumns, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False, _
                       SearchFormat:=False)
    c.Activate
    Exit Sub
    
    notFound:
    
    Application.InputBox "Could not find the range you wanted." 
    ' >> You can put in whatever action you want here -- for example,        <<
    ' >> if you're using this as a module, then "Exit Sub" or "Goto nextOne" <<
    ' >> could be used go to the next step in your process.                  <<
    

    【讨论】:

    • 考虑重新格式化您的答案,很难理解您的意思。使用提供的工具,例如{ } 图标来表示代码。
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 2011-09-20
    • 2019-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多