-
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.
-
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.
-
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?
-
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
-
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
-
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.
-
-
[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'
-
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
-
Forum Rules