PC Review


Reply
Thread Tools Rate Thread

Code deletes first row/headers.

 
 
J.W. Aldridge
Guest
Posts: n/a
 
      8th Oct 2010
Code works, but keeps deleting the headers in row 1. How to avoid/fix
please?

Sub Delete_ROUTES_ColE()

Application.ScreenUpdating = False

Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long

With ActiveSheet

'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(2).Row 'The 1 represents the
RowIndex
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

'Loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1

'Check the values in the D column
With .Cells(Lrow, "E")


If .Value <> "APPLES" And .Value <> "GRAPES" And .Value <>
"PLUMS" And .Value <> "DATES" And .Value <> "PEARS"
Then .EntireRow.Delete

End With
Next Lrow

End With

Application.ScreenUpdating = True

End Sub
 
Reply With Quote
 
 
 
 
Don Guillett Excel MVP
Guest
Posts: n/a
 
      8th Oct 2010
On Oct 8, 10:51*am, "J.W. Aldridge" <jeremy.w.aldri...@gmail.com>
wrote:
> Code works, but keeps deleting the headers in row 1. How to avoid/fix
> please?
>
> Sub Delete_ROUTES_ColE()
>
> Application.ScreenUpdating = False
>
> * * * * Dim Firstrow As Long
> * * * * Dim Lastrow As Long
> * * * * Dim Lrow As Long
>
> * * * * With ActiveSheet
>
> * * * * 'Set the first and last row to loop through
> * * * * Firstrow = .UsedRange.Cells(2).Row 'The 1 represents the
> RowIndex
> * * * * Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
>
> * * * * 'Loop from Lastrow to Firstrow (bottom to top)
> * * * * For Lrow = Lastrow To Firstrow Step -1
>
> * * * * 'Check the values in the D column
> * * * * With .Cells(Lrow, "E")
>
> * * * * * * If .Value <> "APPLES" And .Value <> "GRAPES" And ..Value <>
> "PLUMS" And .Value <> "DATES" And .Value <> "PEARS"
> Then .EntireRow.Delete
>
> * * * * End With
> * * * * Next Lrow
>
> * * End With
>
> Application.ScreenUpdating = True
>
> End Sub


I would have done it differently but instead of cells(2), use
cells(2,1) or range("a2")
 
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
Is there a code that deletes all forms,queries or tables? Pietro Microsoft Access 8 21st Jan 2009 09:03 PM
Need a piece of code that deletes a wildcard tbl after import & ap =?Utf-8?B?ZmlzaHk=?= Microsoft Access External Data 2 28th Aug 2007 02:52 PM
Problem with Deletes in code =?Utf-8?B?QWx5aW5kYXI=?= Microsoft Access VBA Modules 5 27th Feb 2006 03:06 PM
Controlling Row Inserts and Deletes Via Code ozzer Microsoft Excel Programming 5 25th Feb 2006 05:40 AM
Modifying Code that deletes a Row Todd Huttenstine Microsoft Excel Programming 2 27th Nov 2003 10:41 PM


Features
 

Advertising
 

Newsgroups
 


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