Deleting Rows if the Row Contains Specific Text

julia55

Board Regular
Joined
Dec 2, 2010
Messages
73
I am trying to figure out how to delete a whole row if the row contains the text "Run Date". However, the cell with that text will also have varying text before and after run date. I tried using the below, but it won't work since my cell doesn't contain exact text.


Sub DeleteRowWithContents()
Last = Cells(Rows.Count, "D").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "D").Value) = "Run Date" Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
 

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
Hi there, welcome to the board!

Check out the InStr() function, instead of a straight comparison.
 
Upvote 0
I figured out a code that worked. I used the below:

Sub DeleteRowsTOTALONLY()
Dim c As Range
Dim SrchRng
Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("I65536").End(xlUp))
Do
Set c = SrchRng.Find("Total Only", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing
End Sub
 
Upvote 0
To finish out the trail of thought...


<font face=Courier New><SPAN style="color:#00007F">If</SPAN> InStr(1, Cells(i, "D").Value, "Run Date") <> 0 <SPAN style="color:#00007F">Then</SPAN></FONT>


Also, some minor changes to your routine...


<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> DeleteRowsTOTALONLY()<br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range, SrchRng <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> SrchRng = ActiveSheet.Range("A1:I" & Cells(Rows.Count, "I").End(xlUp).Row)<br>    <SPAN style="color:#00007F">Do</SPAN><br>        <SPAN style="color:#00007F">Set</SPAN> c = SrchRng.Find("Total Only", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN> c.EntireRow.Delete<br>    <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


It will keep your range dynamic, and the range object is declared as one as opposed to leaving it as a Variant type. Also, the Find method will remember what you used last as its settings and use those, so be sure to specify any settings you don't want as 'assumed'.

HTH
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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