Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Search sheet for text string

Posted on 2014-02-20
3
Medium Priority
?
261 Views
Last Modified: 2014-02-21
Hi,
I recorded some code to find the text string "Bad Piece" and delete cell next to it.
    Cells.Find(What:="Bad Piece", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
    ActiveCell.Offset(0,1).Delete

Open in new window

What I cannot manage to do is amend the code so that it continues searching for the same text string until it reaches the end of the data.
Unfortunately, the datasheet is downloaded from a client and has many blank cells and sometimes columns.
The term "Bad Piece" can be found in a number of rows and can also be in different columns. I know that "Ctrl-End" gets to the last cell of the data irrespective of gaps, but I cannot fathom how to iterate through the sheet to find all the instances of "Bad Piece" and delete the cell to the right of it.
As always any help is appreciated.
0
Comment
Question by:Stephen Byrom
3 Comments
 
LVL 22

Assisted Solution

by:Dreamboat
Dreamboat earned 1000 total points
ID: 39875453
Since no one answered in the last hour, I'll throw this out there for you:

This is the link:

http://www.mrexcel.com/forum/excel-questions/80940-delete-cells-based-text-value-adjacent-cell.html

this is the code:

Sub Deleter1()

Lastrow = Range("A65536").End(xlUp).Row

For x = Lastrow To 1 Step -1
If Range("A" & x).Value = "Test" And Range("B" & x).Value = 0.75 Then
Range("A" & x, "B" & x).ClearContents
End If
Next x

End Sub

Open in new window


Since you're in 2010 with a million-some rows, you'll want to change the A65536 to A1048576. And you'll want to change "Test to "Bad Data", and you'll probably want to delete the 2nd part of the argument, giving you

Sub Deleter1()

Lastrow = Range("A1048576").End(xlUp).Row

For x = Lastrow To 1 Step -1
If Range("A" & x).Value = "Bad Data" Then
Range("B" & x).ClearContents
End If
Next x

End Sub

Open in new window


Change columns A and B to your respective columns. This tested fine for me.
0
 
LVL 81

Accepted Solution

by:
byundt earned 1000 total points
ID: 39875471
Did you want to clear the adjacent cell (as Dreamboat was doing) or delete it?

The following code will delete the adjacent cells, moving the remaining cells up. I put a loop counter in there because I had an infinite loop the first time I tested.
Sub BadPieces()
Dim cel As Range
Dim addr As String
Dim i As Long
Application.ScreenUpdating = False
Set cel = Cells.Find(What:="Bad Piece", After:=Cells(Rows.Count, Columns.Count), LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)
If cel Is Nothing Then Exit Sub
addr = cel.Address
Do
    i = i + 1
    cel.Offset(0, 1).Delete
    Set cel = Cells.FindNext(cel)
    If cel.Address = addr Then Exit Do
    If i > 1000 Then Exit Do    'Prevents infinite loops
Loop
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 39876456
Thanks to you both.
I have split the points as both solutions work the way I want with a bit of adjustment.
Thanks to you both for your time.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question