Loop that will Delete all rows where a cell Dosn't contain "i" in string

mattd4385

New Member
Joined
Nov 15, 2010
Messages
42
Col A contains no blanks

Col F contains Blanks and random string

Col F:
i
h
A

iw
LA
tcpi

tmi

How can i create a code to loop throught col F and remove all of the cells that don't contain "i" anywhere in the string. To give the following result

Results:
Col F:
i
iw
tcpi
tmi
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
try this

Code:
Sub delete_not_i()
Dim LR As Long
LR = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row 'get last row column A
For i = LR To 2 Step -1
    If InStr(Cells(i, "F"), "i") = 0 Then
        Rows(i).Delete
    End If
Next i
End Sub
 
Upvote 0
It works great thank you so much for you help

I am still trying to understand how the code knows what the last row was in col A if you never used the letter A in the code.
 
Upvote 0
no I just used column F to decide on the last row, since that was where you wanted to test for the missing "i"
 
Upvote 0
An observation if you're going to use this code on a regular basis.

If the last row(s) were blank / empty in column F, but held a value in column A, should (and would) the code delete those rows?


edit: ignore that, you already raised the point while I was typing.
 
Upvote 0
You would change this line

LR = ActiveSheet.Range("F" & Rows.Count).End(xlUp).Row

to this
LR = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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