Do loop - select row when condition is met

Linda21

New Member
Joined
Aug 2, 2010
Messages
38
I am looping through rows to find a specific value. When the value is found, I want to select the row and delete the row. Then I continue through all the rows searching for the same value and deleting until the end.

I can't figure out the code to select the specific row containing the value when the value is met.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Here's an example

Code:
Sub DelRow()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("A" & i).Value = "Linda" Then Rows(i).Delete
Next i
End Sub
 
Upvote 0
Alternatively, filter your data for that specific value and delete all the visible rows and then unfilter your data. Saves you looping, which if you have a large data set, may be a quicker solution.
 
Upvote 0
Is the value always in the same column.
Is the values always all of the cell value (i.e. you want cell = "ABC" or cell starts with "ABC")

Code:
Dim rngSearch as range
Dim rngDelete as range
Dim cCell as cell
Dim myValue as string
 
myValue = "whatever" ' put your search string ni here
Set rngSearch = ActiveSheet.range("A1:Z999") ' put your range in here
 
    For each cCell of rngSearch
 
    If c.Value = myValue Then
        Set rngDelete = ActiveSheet.Row(cCell.Row)
        rngDelete.Delete
    End If
 
 
Next cCell
 
Upvote 0
Thank you for all the good ideas. I got it by simply adding the following. I am really new and having trouble with basic coding references that are probably second nature to all of you.

Rows(SourceDataRow).Delete
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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