Delete row if cell contains an asterisk symbol

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
Code:
Sub DeleteRow()
A = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = A To 2 Step -1

If Worksheets("Sheet1").Cells(i, 6).Value Like "***" Then
    Worksheets("Sheet1").Rows(i).Delete
End If

Next
End Sub

For some reason the code above deletes all rows.
I am only trying to delete rows where column F contains an asterisk symbol (*). Where am I going wrong?

Thank you in advance!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this - should be faster than deleting one row at a time.
Code:
Sub Magoosball()
Application.ScreenUpdating = False
Range("F2:F" & Cells(Rows.Count, "A").End(xlUp).Row).Replace what:="*~**", _
    replacement:="#N/A", lookat:=xlWhole
On Error Resume Next
Range("F2:F" & Cells(Rows.Count, _
    "A").End(xlUp).Row).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Another option
Code:
Sub DelAsteriskRows()
  Application.ScreenUpdating = False
  With Intersect(ActiveSheet.UsedRange, Columns("F"))
    .AutoFilter Field:=1, Criteria1:="*~**"
    .Offset(1).EntireRow.Delete
    .AutoFilter
  End With
  Application.ScreenUpdating = True
End Sub

Where am I going wrong?
An asterisk is a wildcard character in many situations, including with the 'Like' operator that you used (& Find/Replace and AutoFilter that JoeMo & I have used), so your code was looking for "anything" and deleting it. If you look at the two suggested codes you will see that we prefixed one of the asterisks with a "~" symbol to tell Excel that one is not to be treated as a wildcard.

BTW, about how many rows of data and what proportion of rows with asterisks? I'm asking, because if you have very large data with lots of disjoint rows to delete, there are faster methods (but longer code to write) to achieve the deletions.
 
Last edited:
Upvote 0
Solution
That worked perfectly. Thank you!
You are welcome - you have choices.

(and I assume that the answer to my last question is that your data is not very big - or else you didn't see my edit before you posted. :))
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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