Delete Rows except

J

J.W. Aldridge

I found the following code, but it doesnt work for me.
I need a code that will delete Rows A-H based on the data in column
A.

Example:

PCPB
PCPA
PCPC
PCPB
PCPD
PCPC
PCPB

I want to delete every row that doesn't start with PCPB.

This code doesnt work for me:

Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
'<change column letter if desired
For RowNdx = LastRow To 1 Step -1
'<change '1' to ending row if desired
If Left(Cells(RowNdx, "A"), 7) <> "PCPB" Then
Rows(RowNdx).Delete
End If
Next RowNdx
 
J

J.W. Aldridge

one more thing....

If I wanted the macro to start at A2 (instead of A1)?
(row a1 has my headers)



Sub Apples()
Dim LastRow As Long
Dim RowNdx As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
'<change column letter if desired
For RowNdx = LastRow To 1 Step -1
'<change '1' to ending row if desired
If Left(Cells(RowNdx, "A"), 4) <> "PCPB" Then
Rows(RowNdx).Delete
End If
Next RowNdx

End Sub
 
M

Mike Fogleman

For RowNdx = LastRow To 2 Step -1

This loop works from the bottom row, up. So LastRow To 2 will stop after
doing row 2.

Mike F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top