Find Using VBA

  • I know I am doing some minor mistake somewhere with "Find" Feature in VBA, Probably someone can shed light on it.


    I want to find a string "Time" in my worksheet.


    If it exists, then Do


    This


    Else


    Do that


    I am not able to put up this logic in VBA.. Any thoughts on it.


    Thanks very much for your help.


    Ram P

    Ram P

  • What is your code that your using. The following is from the XL VBA Help


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


    Essentially what .Find does is return a range object that has the first instance of the searched for string. So you need to check to see if the object has been created essentially.


    Or if you recorded the find from the worksheet you'll need to get the current address of the current cell then run the find and compare the new address. If the address didn't change then the string wasn't found.


    But it would be best to see the code that you're using.




    Regards

  • I don't want to search for Address...


    I just want to find data, if exists do one series of steps else do another series of steps.


    Thanks a lot for your kind attentin and help


    Ram P

    Ram P

  • Hi Ram P,


    Working on a little from the example iwrk4dedpr posted.


    Code
    With Worksheets(1).Range("a1:a500") 
        Set c = .Find("Time", lookin:=xlValues) 
        If Not c Is Nothing Then 
            ' code here when found
        else
            ' code here when NOT found
        End If 
    End With


    The use of Address in the previous example is used when you want to find all occurances of the text you are searching for. It stop the search cycling thru the same information twice.

    [h4]Cheers
    Andy
    [/h4]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!