Delete rows if (certain value) exists in data set.

griffindor2020

New Member
Joined
Jun 6, 2020
Messages
20
Office Version
  1. 2016
Platform
  1. MacOS
Hello,

I've been trying to delete rows in my data set that contain the following keyword "lename"

This is the following script I found and the following error

Script :
Sub Delete_All_Rows_IF_Cell_Contains_Certain_String_Text()
Dim lRow As Long
Dim iCntr As Long
lRow = 150000
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 3).Value = "lename" Then
Rows(iCntr).Delete
End If
Next
End

Error:
Compile Error : Expected End Sub

Let me know what went wrong and what is the best way to fix it :)

Thank you.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Just add the word Sub after the word End (On the same line)
 
Upvote 0
Just add the word Sub after the word End (On the same line)

This is my code now

Sub Delete_All_Rows_IF_Cell_Contains_Certain_String_Text()
Dim lRow As Long
Dim iCntr As Long
lRow = 150000
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 3).Value = "lename" Then
Rows(iCntr).Delete
End If
Next
End Sub

But it hasn't deleted any of the rows with the value "lename".
 
Upvote 0
Does lename appear in col C and is it all lower case?
 
Upvote 0
Hey,

lename appears in column D ... should I change If Cells(iCntr, 3).Value = "lename" for If Cells(iCntr, 4).Value = "lename"

?

Let me know ! :)
 
Upvote 0
This should be faster than your code.
VBA Code:
Sub Delete_All_Rows_IF_Cell_Contains_Certain_String_Text()
   Dim iCntr As Long
   Dim Rng As Range
   
   For iCntr = Range("D" & Rows.Count).End(xlUp).Row To 1 Step -1
      If Cells(iCntr, 3).Value = "lename" Then
         If Rng Is Nothing Then Set Rng = Rows(i) Else Set Rng = Union(Rng, Rows(i))
      End If
   Next iCntr
   If Not Rng Is Nothing Then Rng.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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