【问题标题】:item not found in "Find" vba在“查找”vba 中找不到项目
【发布时间】:2022-01-12 13:57:34
【问题描述】:

我正在从列表中查找用户 ID #s。然而,一些用户不再存在。我试过test 方法、on error go to 方法和if err.number<> 0 then 方法。我仍然收到Run-time error '91': object variable or with block variable not set。该号码在列表中不存在。下面是我的代码,尝试了几次没有结果

On Error GoTo errorLn

If Err.Number <> 0 Then
 GoTo errorLn
End If
Cells.Find(What:=uSSO, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Select

还有哪些其他选择?还是我放错了“错误”行? “cells.Find...”前后我都试过了

【问题讨论】:

    标签: vba excel find


    【解决方案1】:

    大概你会想做一些不同于消息框的事情。

    Dim myCell As Range
    
    Set myCell = Cells.Find(What:=uSSO, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
    If (Not myCell Is Nothing) Then
        MsgBox "something!"
    
    Else
        MsgBox "nothing"
    End If
    

    【讨论】:

      【解决方案2】:

      我相信您需要对其进行一点重组。使用On Error Resume Next 处理错误不是最佳 做法,但您可以试试这个:

      On Error Resume Next
      Cells.Find(What:=uSSO, After:=ActiveCell, LookIn:=xlFormulas, _
          LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False, SearchFormat:=False).Select
      
      If Err.Number <> 0 Then
       '''Do your error stuff'''
       GoTo errorLn
      Else
          Err.Clear
      End If
      

      这对你的情况有用吗?

      来源http://www.mrexcel.com/forum/excel-questions/143988-check-if-value-exists-visual-basic-applications-array.html

      【讨论】:

      • 如果我在 for 循环中,“on error resume next”是否会恢复 for 循环中的下一步?
      • 对 - 它基本上只是继续,好像一切都很好,这就是你需要小心使用它的原因之一。有关处理错误的不同方法的详细说明,请查看cpearson.com/excel/errorhandling.htm
      【解决方案3】:

      试试这个

      Sub Sample1()
          Dim oSht As Worksheet
          Dim uSSO As String
          Dim aCell As Range
          
          On Error GoTo Whoa
          
          '~~> Change this to the relevant sheet
          Set oSht = Sheets("Sheet1")
          
          '~~> Set User ID here
          uSSO = "User ID"
          
          Set aCell = oSht.Cells.Find(What:=uSSO, LookIn:=xlFormulas, _
          LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False, SearchFormat:=False)
          
          '~~> Check if found or not
          If Not aCell Is Nothing Then
              MsgBox "Value Found in Cell " & aCell.Address
          Else
              MsgBox "Value Not found"
          End If
          
          Exit Sub
      Whoa:
          MsgBox Err.Description
      End Sub
      

      我还建议阅读此链接,我已经介绍了 .Find.FindNext

      主题:Excel VBA 中的 .Find 和 .FindNext

      链接https://web.archive.org/web/20160316214709/https://siddharthrout.com/2011/07/14/find-and-findnext-in-excel-vba/

      【讨论】:

      • 你在我开始写的时候发布了这个...... :-)
      【解决方案4】:

      只是为了后代,这就是你在不处理错误的情况下的做法(来自:http://www.mrexcel.com/forum/excel-questions/519070-visual-basic-applications-error-handling-when-dealing-cells-find.html)。

      Dim rngFound As Range
      
      Set rngFound = Sheets("WhateverSheet").UsedRange.Find(What:="SoughtValue",LookIn:=xlFormulas)
      
      If Not rngFound Is Nothing Then
        'you found the value - do whatever
      Else
        ' you didn't find the value
      End if
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 2014-07-17
        • 1970-01-01
        • 2019-06-30
        • 2016-11-22
        • 1970-01-01
        相关资源
        最近更新 更多