Do Until Loop

sachavez

Active Member
Joined
May 22, 2009
Messages
469
Need help with VBA code:

Need code to loop though worksheet and find specific text, i.e. "B A SMITH" and delete entire row.

The number of rows containing "B A SMITH" is different each day.

Thanks in advance!

Steve
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try:

Code:
Public Sub DeleteNames()
Dim rng     As Range
    
Application.ScreenUpdating = False
With Cells
    Set rng = .Find("B A Smith", LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious)
    If Not rng Is Nothing Then
        Do
            rng.EntireRow.Delete
            Set rng = .FindNext(rng)
        Loop While Not rng Is Nothing
    End If
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Works like a champ, thanks!, MrKowz!

Now, I have multiple names that I'm searching for and deleting entire row. Do I have to copy the entire code for each name, or is there a way to "find" all of the names and then do the loop?

Let me know if I'm not clear.

Thanks again!
 
Upvote 0
I could have sworn that I posted the other solution. Sorry about that - here it is - adjust the names in the nameary variable to fit all of the names you need:

Code:
Public Sub DeleteNames()
Dim rng     As Range, _
    nameary As Variant, _
    i       As Long
 
nameary = Array("B A Smith", "J A Nobody", "P A Edwards")
 
Application.ScreenUpdating = False
For i = LBound(nameary) To UBound(nameary)
    With Cells
        Set rng = .Find(nameary(i), LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlPrevious)
        If Not rng Is Nothing Then
            Do
                rng.EntireRow.Delete
                Set rng = .FindNext(rng)
            Loop While Not rng Is Nothing
        End If
    End With
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,610
Members
452,931
Latest member
The Monk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top