【问题标题】:how to detect whether VBA excel found something?如何检测VBA excel是否发现了什么?
【发布时间】:2010-12-08 01:51:37
【问题描述】:

我在宏中使用它来查找工作表中的内容:

Selection.Find(What:=email, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Activate

我怎么知道它是否找到了什么?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    Find 返回一个 Range 对象,如果未找到 What,则该对象的值为 Nothing。来自帮助:

    With Worksheets(1).Range("a1:a500")
        Set c = .Find(2, lookin:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Value = 5
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
    

    【讨论】:

    • 解释说值是NOTHING if 语句没有发现任何真正有帮助的东西,我正在测试变量是否是nullIsNull,但这不起作用!
    【解决方案2】:
    Dim rng As Range
    
    Set rng = Selection.Find(What:=email, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)
    
    If Not rng Is Nothing Then 'when rng <> nothing means found something'
        rng.Activate
    End IF
    

    【讨论】:

    • 阅读If Not rng Is Nothing Then 让我头疼....您的评论帮助很大!谢谢
    【解决方案3】:

    Selection.Find 就像使用 Ctrl+F 来查找一个值。然后,您可以检查 Activecell.Value 以查看是否获得了所需的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      相关资源
      最近更新 更多