Consulting

Results 1 to 9 of 9

Thread: Solved: If condition for Find cell value

  1. #1
    VBAX Newbie
    Joined
    Jul 2006
    Posts
    4
    Location

    Solved: If condition for Find cell value

    I am trying to search for text in a spreadsheet that may or may not be present and delete it if it is found. I tried the following which does not work,

    [vba]
    If Cells.Find(What:="PRICES", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate _
    Then
    Range(ActiveCell, ActiveCell.Offset(3, 0)).Select
    Selection.ClearContents
    End If
    [/VBA]
    Can anyone suggest an alternative approach?

    Thanks for the help.

    Mike
    Last edited by DB_Mike; 07-12-2006 at 01:14 PM.

  2. #2
    VBAX Newbie
    Joined
    Jul 2006
    Posts
    4
    Location
    sorry - my attempt at using the VBA code identifier didn't work very well.

    never mind - I didn't realize I could fix it.
    Last edited by DB_Mike; 07-12-2006 at 01:15 PM.

  3. #3
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    Mike, I never use the built in Find, I always use a VBA constructed search, but it does strike me that maybe your code needs to have a range selection in it somewhere so that the "Find" function knows where to look for "PRICES".
    Did you try recording a Macro of doing the "Find" manually?

  4. #4
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    For Example I get this -
    Range("A1:A7").Select
    Selection.Find(What:="for", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate

  5. #5
    VBAX Newbie
    Joined
    Jul 2006
    Posts
    4
    Location
    I did record the find command to get the complete syntax. I realized from your comment that I didn't have a search range selected at the time the command is being evaluated so I established the range before the If statement but it still errors.

    I will try to find another way to accomplish this. What is the VBA constructed search that you referred to?

    Mike

  6. #6
    VBAX Guru
    Joined
    Mar 2005
    Posts
    3,296
    Location
    The VBA method uses a For/next, for/each loop or with/end with using the selection to test each cell in your column for the required "word". There are already a few examples on here, you could try a search for "Find Text" etc.
    If you don't find what you want can you post the workbook and we will construct something for you.

  7. #7

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Set c = Cells.Find(What:="PRICES")
    If Not c Is Nothing Then
    Range(c, c.Offset(3, 0)).ClearContents
    End If
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Newbie
    Joined
    Jul 2006
    Posts
    4
    Location
    Thanks to both of you. The 2nd solution with code from Zack solved my problem but I appreciate the responses.

Posting Permissions

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