Macro to delete entire row if first 2 letters in column B = P4

katamara

New Member
Joined
Mar 16, 2013
Messages
6
I've seen a few examples of macros to delete the row if the first letter is something, but not if the first 2 is equal to something.
I'm looking to go thru every row that contains data and look in Column B to see if it starts with P4 or P5. If that's true, then delete the entire row.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I've seen a few examples of macros to delete the row if the first letter is something, but not if the first 2 is equal to something.
I'm looking to go thru every row that contains data and look in Column B to see if it starts with P4 or P5. If that's true, then delete the entire row.

Code:
Sub test()
With ActiveSheet
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
End With
For a = 1 To lastrow
Select Case Left(Cells(a, 2).Value, 2)
Case "P4", "P5"
Rows(a).Delete
End Select
Next
End Sub
 
Upvote 0
What is the LastCol variable for? or is just programming habit?

The first four lines (With.......End With) are a generic bit of code to return the last row and last column.

This particular question doesn't need them but there is no time overhead here.
 
Upvote 0
Code:
Sub test()
With ActiveSheet
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
End With
For a = 1 To lastrow
Select Case Left(Cells(a, 2).Value, 2)
Case "P4", "P5"
Rows(a).Delete
End Select
Next
End Sub


Thanks. I tweaked it just a bit to:
Sub test()
'removal of P4 and P5 on column B
Dim lastrow As Integer
Dim lastcol As String
Dim a As Integer

With ActiveSheet
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
End With
For a = lastrow To 1 Step -1
Select Case Left(Cells(a, 2).Value, 2)
Case "P4", "P5"
Rows(a).Delete
End Select
Next
End Sub

Thank you again it works great.
 
Upvote 0
Here is a non-looping method you can try that should be pretty fast...
Rich (BB code):
Sub DeleteP4andP5Rows()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "B").End(xlUp).Row
  Columns("B").Replace "P4", "=P4", xlPart
  Columns("B").Replace "P5", "=P5", xlPart
  On Error Resume Next
  Columns("B").SpecialCells(xlFormulas).EntireRow.Delete
  On Error GoTo 0
  Columns("B").Replace "=P4", "P4", xlPart
  Columns("B").Replace "=P5", "P5", xlPart
End Sub
NOTE: The last two lines of code (highlighted in red) can be deleted if your data will never have P4 or P5 located past the beginning of the text.
 
Upvote 0
Hi guys,

I have a very similar problem: I want to delete all rows that have "#N/A" in column AP. Only difference to the above is that cells AP contain a Vlookup. Would a macro still work?

BengalMagic, I tried using your script, but I couldn't get it to work... :(

Would anyone have a solution for that?
Sorry for hijacking the thread, too :)
 
Upvote 0
Hi guys,

I have a very similar problem: I want to delete all rows that have "#N/A" in column AP. Only difference to the above is that cells AP contain a Vlookup. Would a macro still work?
As long as #N/A is the only type of error that will ever be in Column AP, you can use this relatively simple macro...
Code:
Sub RemoveRowsWithNslashAerrorsInColumnAP()
  On Error Resume Next
  Columns("AP").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
  On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,417
Members
448,895
Latest member
omarahmed1

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