Solved

Search sheet for text string

Posted on 2014-02-20
3
254 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 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

912 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now