PC Review


Reply
Thread Tools Rate Thread

Code to delete rows excluding Header if not = to

 
 
PVANS
Guest
Posts: n/a
 
      22nd Mar 2010
Good morning

I have the following code to delete all rows that do not contain the
following two variables, in column P, "cw" and "ea".

Set ws = ActiveSheet

For p = ws.Range("P65536").End(xlUp).Row To 1 Step -1
If ws.Cells(p, 16) <> "cw" Then
If ws.Cells(p, 16) <> "ea" Then
ws.Rows(p).Delete
End If
End If
Next

It works very well apart from the fact that it deletes the Header row.
Please can someone assist me in modifying this code so it does not delete Row
1 (the header row).

Thanks for any help in advance, I really appreciate it.

 
Reply With Quote
 
 
 
 
kc-mass
Guest
Posts: n/a
 
      22nd Mar 2010
Try changing your loop control to :

For p = ws.Range("P65536").End(xlUp).Row To 2 Step -1

Regards

Kevin


"PVANS" <(E-Mail Removed)> wrote in message
news:692E1DCE-3A31-4698-850E-(E-Mail Removed)...
> Good morning
>
> I have the following code to delete all rows that do not contain the
> following two variables, in column P, "cw" and "ea".
>
> Set ws = ActiveSheet
>
> For p = ws.Range("P65536").End(xlUp).Row To 1 Step -1
> If ws.Cells(p, 16) <> "cw" Then
> If ws.Cells(p, 16) <> "ea" Then
> ws.Rows(p).Delete
> End If
> End If
> Next
>
> It works very well apart from the fact that it deletes the Header row.
> Please can someone assist me in modifying this code so it does not delete
> Row
> 1 (the header row).
>
> Thanks for any help in advance, I really appreciate it.
>



 
Reply With Quote
 
PVANS
Guest
Posts: n/a
 
      22nd Mar 2010
aha! managed to fix it, I simply changed:
For p = ws.Range("P65536").End(xlUp).Row To 1 Step -1

to:
For p = ws.Range("P65536").End(xlUp).Row To 2 Step -1

Have tested that it still works correctly, by placing a row in row to that
is not equal to "cw" and it was correctly deleted.

Thanks to any and everyone who took a look at this for me

"PVANS" wrote:

> Good morning
>
> I have the following code to delete all rows that do not contain the
> following two variables, in column P, "cw" and "ea".
>
> Set ws = ActiveSheet
>
> For p = ws.Range("P65536").End(xlUp).Row To 1 Step -1
> If ws.Cells(p, 16) <> "cw" Then
> If ws.Cells(p, 16) <> "ea" Then
> ws.Rows(p).Delete
> End If
> End If
> Next
>
> It works very well apart from the fact that it deletes the Header row.
> Please can someone assist me in modifying this code so it does not delete Row
> 1 (the header row).
>
> Thanks for any help in advance, I really appreciate it.
>

 
Reply With Quote
 
PVANS
Guest
Posts: n/a
 
      22nd Mar 2010
Hi Kevin,

Thanks for the reply - I had literally just tried that and it works
perfectly. Thanks for the help. I do appreciate it

Have a good day

Paul

"kc-mass" wrote:

> Try changing your loop control to :
>
> For p = ws.Range("P65536").End(xlUp).Row To 2 Step -1
>
> Regards
>
> Kevin
>
>
> "PVANS" <(E-Mail Removed)> wrote in message
> news:692E1DCE-3A31-4698-850E-(E-Mail Removed)...
> > Good morning
> >
> > I have the following code to delete all rows that do not contain the
> > following two variables, in column P, "cw" and "ea".
> >
> > Set ws = ActiveSheet
> >
> > For p = ws.Range("P65536").End(xlUp).Row To 1 Step -1
> > If ws.Cells(p, 16) <> "cw" Then
> > If ws.Cells(p, 16) <> "ea" Then
> > ws.Rows(p).Delete
> > End If
> > End If
> > Next
> >
> > It works very well apart from the fact that it deletes the Header row.
> > Please can someone assist me in modifying this code so it does not delete
> > Row
> > 1 (the header row).
> >
> > Thanks for any help in advance, I really appreciate it.
> >

>
>
> .
>

 
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
delete rows with header, excluding 1st row J.W. Aldridge Microsoft Excel Programming 1 30th Mar 2010 07:22 PM
VBA code to delete rows =?Utf-8?B?U2VjcmV0IFNxdWlycmVs?= Microsoft Excel Misc 3 21st Jan 2007 03:01 PM
Can this code be modified to delete rows? jamesfc30@earthlink.net Microsoft Excel Programming 7 19th Aug 2006 11:58 AM
code to delete rows =?Utf-8?B?aWFuYWxleGg=?= Microsoft Excel Misc 5 5th May 2005 10:46 AM
Code to delete rows Ivor Williams Microsoft Access Forms 1 1st May 2005 06:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 PM.