VBA for deleting a row when the column contains (not exact match)

garypea123

Board Regular
Joined
Mar 16, 2020
Messages
221
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have the below VBA which will look at Column A and delete any row that has an exact match for Vacant

Is there a simple way I can get this to delete a row which contains the word Vacant (so Vacant 1, Vacant 2 etc etc). So pretty much a partial match but only if the word "Vacant" appears in the column.

Thanks
Gary


Excel Delete.PNG
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
VBA Code:
If Cells(i, 1) Like "*Vacant*" Then
 
Upvote 0
I have tried to apply the below, but it is not deleting anything.

I think I am likely doing something wrong here.

In column B if it contains the word Gary I would like it to delete that row.



Sub Delete_Gary()


Last = Cells(Rows.Count, "B").End(xlUp).Row
For i = Last To 6 Step -1
If Cells(i, 2) Like "Gary" Then
Cells(i, 2).EntireRow.Delete
End If
Next i

End Sub



Excel Delete.PNG
 
Upvote 0
You need the * at the end like "Gary*"
 
Upvote 0
Try this change

Rich (BB code):
If Cells(i, 2) Like "Gary" Then
Cells(i, 2).EntireRow.Delete
End If

If LCase(Cells(i, 2).Value) Like "*gary*" Then Rows(i).Delete
 
Upvote 0
Try this change

Rich (BB code):
If Cells(i, 2) Like "Gary" Then
Cells(i, 2).EntireRow.Delete
End If

If LCase(Cells(i, 2).Value) Like "*gary*" Then Rows(i).Delete
Thanks, I tried this code also, and works like a dream also :)

Thank you both very much. I am new to this, and so struggle sometimes as I have no official lessons. Just google to help me :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

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