ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete Rows except (https://www.excelbanter.com/excel-programming/382174-delete-rows-except.html)

J.W. Aldridge

Delete Rows except
 
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


Ron de Bruin

Delete Rows except
 
If Left(Cells(RowNdx, "A"), 7) < "PCPB" Then

Change the 7 to 4


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"J.W. Aldridge" wrote in message oups.com...
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.W. Aldridge

Delete Rows except
 


......worked like a charm!

Thanx.


J.W. Aldridge

Delete Rows except
 

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


Ron de Bruin

Delete Rows except
 
For RowNdx = LastRow To 1 Step -1

Change to

For RowNdx = LastRow To 2 Step -1



--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"J.W. Aldridge" wrote in message ups.com...

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


Mike Fogleman

Delete Rows except
 
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

"J.W. Aldridge" wrote in message
ups.com...

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





All times are GMT +1. The time now is 04:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com