¿Cómo detectar si VBA Excel encontró algo?

Resuelto Alex Gordon asked hace 14 años • 4 respuestas

Estoy usando esto en una macro para buscar cosas en mi hoja:

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

¿Cómo puedo saber si encontró algo o no?

Alex Gordon avatar Oct 20 '09 00:10 Alex Gordon
Aceptado
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
manji avatar Oct 19 '2009 17:10 manji

Finddevuelve un objeto Range que tendrá un valor Nothingsi Whatno se encuentra. De la ayuda:

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
Ryan Shannon avatar Oct 19 '2009 17:10 Ryan Shannon