PC Review


Reply
Thread Tools Rate Thread

Delete Rows except

 
 
J.W. Aldridge
Guest
Posts: n/a
 
      29th Jan 2007
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

 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      29th Jan 2007
> 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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>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
>

 
Reply With Quote
 
J.W. Aldridge
Guest
Posts: n/a
 
      29th Jan 2007


......worked like a charm!

Thanx.

 
Reply With Quote
 
J.W. Aldridge
Guest
Posts: n/a
 
      29th Jan 2007

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

 
Reply With Quote
 
Ron de Bruin
Guest
Posts: n/a
 
      29th Jan 2007
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> 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
>

 
Reply With Quote
 
Mike Fogleman
Guest
Posts: n/a
 
      29th Jan 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Autofil on variable rows, delete extract and show remaining rows 1plane Microsoft Excel Programming 3 17th Nov 2009 10:49 AM
Hpw do I delete multiple empty rows found between filled rows? Bill Microsoft Excel Worksheet Functions 1 15th Nov 2009 12:52 AM
Copy pasting Rows, but need to Delete any Shapes/Pictures that are within copied rows Corey Microsoft Excel Programming 2 1st Aug 2007 02:02 AM
Cut filtered rows, paste into next empty row of new sheet, and delete cut rows Scott Microsoft Excel Worksheet Functions 0 13th Dec 2006 01:25 AM
Delete rows with numeric values, leave rows with text =?Utf-8?B?R1NwbGluZQ==?= Microsoft Excel Programming 5 11th Oct 2005 12:44 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:11 PM.