How to delete a row based on a condition

rbysetty

New Member
Joined
Jul 3, 2012
Messages
22
I was hoping someone could help with this piece of code. I wam trying to delete the entire row if I find the text 'FemImplant' in column A. The tricky part is that this text is part of a sentence linked by '$'. So I need the code to parse out the cell content before '$' and see if it matches 'FemImplant' and delete that row. This is what I have so far but it is nto working.

Dim cell As Excel.Range
RowCount = DataSheet.UsedRange.Rows.Count
Set col = DataSheet.Range("A1:A" & RowCount)
Dim SheetName As String
Dim ParsedCell() As String

For Each cell In col

ParsedCell = cell.Value.Split("$")
SheetName = ParsedCell(0)

If SheetName = "FemImplant" Then
cell.EntireRow.Delete Shift:=xlUp

End If

Next
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:

Code:
Sub Test()
    With Range("A1").CurrentRegion
        .AutoFilter Field:=1, Criteria1:="=*FemImplant*"
        .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter
    End With
End Sub

Dom
 
Upvote 0
If you want to delete rows by looping through them you need to do so from the last row to the first otherwise you will skip consecutive rows that match the criteria.

Dom
 
Upvote 0
If you want to delete rows by looping through them you need to do so from the last row to the first otherwise you will skip consecutive rows that match the criteria.

Dom

How do I acheive this, I tried excel help but was of no use, could you please point me the right direction ?
how do i loop from the bottom ?
 
Upvote 0
Like this:

Code:
Sub test()

    Dim lngLoopRow As Long


    For lngLoopRow = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Range("A" & lngLoopRow) Like "*FemImplant*" Then
            Rows(lngLoopRow).Delete
        End If
    Next lngLoopRow


End Sub

Dom
 
Upvote 0

Forum statistics

Threads
1,203,630
Messages
6,056,422
Members
444,863
Latest member
powerdt

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