VBA Delete row based on cell value

dfenton21

Board Regular
Joined
Jun 23, 2007
Messages
135
Hi, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
I have a spread with about 4000 rows. Some cells in column b are blank, and some cells in column b begins with ...<o:p></o:p>
<o:p> </o:p>
I would like a piece of VBA code that would loop though each row, and delete the row if the cell in column b is blank or begins with …<o:p></o:p>
<o:p> </o:p>
Is that possible?<o:p></o:p>
<o:p> </o:p>
Thanks.<o:p></o:p>
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
How about...

Not sure what your begins with is, but in this I set it for anything that begins with B. Update to meet your needs...

Code:
Sub DeleteB()
Dim LR As Long

Application.ScreenUpdating = False
    
    For LR = Range("B" & Rows.Count).End(xlUp).Row To 2 Step -1
        If Range("B" & LR).Value = "" Or Left(Range("B" & LR), 1) = "B" Then
            Rows(LR).EntireRow.Delete
        End If
    Next LR

Application.ScreenUpdating = True
End Sub
 
Upvote 0
hallo jeffreybrown,

I have been looking for help on this issue, and found this thread. I noticed dfenton21 did not have the decency to thank you for your help. I would like to thank you for this code, it works beautifully!..have a great day..
 
Upvote 0
hallo jeffreybrown,

I have been looking for help on this issue, and found this thread. I noticed dfenton21 did not have the decency to thank you for your help. I would like to thank you for this code, it works beautifully!..have a great day..
In fairness to dfenton21, her/his specific request was actually to delete if a cell started with ..., whereas the posted code did not do this.
An easy way to do this sort of thing, which dfenton21 may actually have been looking for is something like
Code:
Sub dcfbf()
With Range("B:B")
    .Replace "...*", ""
    .SpecialCells(xlBlanks).EntireRow.Delete
End With
End Sub
If you like, replace the ... by B or whatever you want.
 
Upvote 0
hi kalak,

I understand. The code as was given worked for my purpose, which was to delete rows which contained a certain value. Thanx for your feedback...
 
Upvote 0
I saw Rick Rothstein use something like the below and I've been using it ever since.

Code:
Sub Test()
  Dim Addr As String, VarA As Variant, VarB As Variant
  VarA = ""
  VarB = "..."
  Addr = "N1:N" & Cells(Rows.Count, "N").End(xlUp).Row
  Range(Addr) = Evaluate(Replace("IF((0+@=" & VarA & ")+(0+@=" & VarB & "),@)", "@", Addr))
  Columns("N").SpecialCells(xlConstants, 4).EntireRow.Delete
End Sub
 
Upvote 0
Hi Kalak,
Thank you for your VBA code. I achieved my goal to delete rows where there is specific content in a cell in the row B.
Great job!
 
Upvote 0

Forum statistics

Threads
1,223,270
Messages
6,171,103
Members
452,379
Latest member
IainTru

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