Looping Through Sheet

S

scott

My Below Sub accepts a string, finds it and deletes the row in which the
string is found. Can someone help me modify it to continue searching through
all cells that contain data and delete any other rows where the string
occurs?

USAGE:

DeleteRowswString("myword")

CODE

Sub DeleteRowwString(ByVal sString As String)

Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = sString Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub
 
T

Tom Ogilvy

Sub DeleteRowwString(ByVal sString As String)

Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Intersect(Activesheet.UsedRange.Entirerow, _
Activesheet.Columns(1)).Cells
nCells = theRange.Rows(theRange.rows.count).Row
For I = nCells To 1 Step -1
If Application.countif(Cells(i,1).EntireRow,"*" & sString _
& "*") > 0 then
Cells(i,1).EntireRow.Delete
End If
Next
End Sub
 
D

Dick Kusleika

Scott

You might find using the Find method to be faster than looping through all
the cells. Try this

Sub DeleteRowwString(ByVal sString As String)

Dim rFound As Range
Dim rLook As Range

If TypeName(Selection) = "Range" Then
Set rLook = Selection

Set rFound = rLook.Find(sString, , xlValues, xlWhole)

If Not rFound Is Nothing Then
Do
rFound.EntireRow.Delete
Set rFound = rLook.FindNext
Loop Until rFound Is Nothing
End If
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Looping through Sheet 5
Delete Columns based on Strings 2
Need Excel Macro to Find Specific Text and Characters 1
R1C1 1
automatic page break 1
end it 2
HELP Copy And Paste 7
output standardization to to cell 3

Top