Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Nov 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Conditional Find (Excel 2003)

    I'm still working on the same project as <post#=563,970>post 563,970</post#>, but now I'm trying to figure out some way to do a conditional find. Basically, if the spreadsheet contains a cell holding the text x, then I want to do y. But, what I can't figure out how to do is to make this a conditional statement. If I just write the code to look for each version of x, then if x isn't present, I get an error message. But, I can't figure how to turn this into any kind of If/Then statement. Can anyone think of a way to say:
    If a cell with x text is present, then perform y actions?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: Conditional Find (Excel 2003)

    Code could look like this:

    Sub FindTest()
    Dim ws As Worksheet
    Dim oRange As Range
    Set ws = ActiveSheet ' or a specific sheet
    Set oRange = ws.Cells.Find(What:="x", LookIn:=xlValues, LookAt:=xlPart)
    If Not oRange Is Nothing Then
    ' actions here
    Beep
    End If
    Set oRange = Nothing
    Set ws = Nothing
    End Sub

    Replace "x" with the text or value you want to find, and Beep with the series of actions you want to perform if the search text is found.
    The code as is will search for "x" as part of the cell contents. If you want "x" to be the entire contents, change xlPart to xlWhole.
    Click in the word Find (in the Visual Basic Editor) and press F1 for more info about it.

  3. #3
    Star Lounger
    Join Date
    Nov 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Conditional Find (Excel 2003)

    Once again, a perfect solution. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •