#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Row Delete

This is similar to the "Is this Possible?" post.

I want to delete a record that meets a certain condition.
This works with the first condition, but anything after the "or" won't
delete.

y = 5
Do While Range("A" & y) < ""
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
End If
y = y + 1
Loop


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Row Delete

One problem is that if the lines to be deleted come right after eac
other, the second one will not get deleted. This is because when yo
delete the row and increment the y variable, it skips the row after th
one you deleted (because it moves up one).

The fix should be:


Code
-------------------
y = 5
Do While Range("A" & y) < ""
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
y = y - 1
End If
y = y + 1
Loo
-------------------



--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Row Delete

Another approach:

y = 5
Do While Range("A" & y) < ""
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
Else
y = y + 1
End if
Loop

Your conditional test requires an exact match. Are the entries an exact
match (no trailing spaces or anthing like that)?

--
Regards,
Tom Ogilvy



"kkknie " wrote in message
...
One problem is that if the lines to be deleted come right after each
other, the second one will not get deleted. This is because when you
delete the row and increment the y variable, it skips the row after the
one you deleted (because it moves up one).

The fix should be:


Code:
--------------------
y = 5
Do While Range("A" & y) < ""
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
y = y - 1
End If
y = y + 1
Loop
--------------------

K


---
Message posted from http://www.ExcelForum.com/



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Row Delete

Tom asks a good question about the trailing/leading spaces. If ther
are any, change the If statement to:

If Trim(Range("F" & y).Value) = "957-TYPE" Or _
Trim(Range("F" & y).Value) = "149-DUPLICATE" Then

Also, the comparison is case dependant, so if something may have gon
in lowercase, change to:

If UCase(Trim(Range("F" & y).Value)) = "957-TYPE" Or _
UCase(Trim(Range("F" & y).Value)) = "149-DUPLICATE" Then



--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Row Delete

Hi Doug,

If you are going to use this method for row deletion then work up from the
bottom.
Everything under a deleted row moves up one, so those rows should be ones
the code has already tested.

So,
ascertain the last block row number with a bit of code.


y=Range("A" & Rows.Count).End(xlUp).Row 'Assumes nothing beneath the
block of data you are dealing with.
Do while y<4
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
End If
y = y - 1

Loop





"Doug Van" wrote in message
...
This is similar to the "Is this Possible?" post.

I want to delete a record that meets a certain condition.
This works with the first condition, but anything after the "or" won't
delete.

y = 5
Do While Range("A" & y) < ""
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
End If
y = y + 1
Loop






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default Row Delete

Thanks for all the input.

I think it is better starting from the bottom, I did notice it was deleting
all the rows.



"Paulw2k" wrote in message
...
Hi Doug,

If you are going to use this method for row deletion then work up from the
bottom.
Everything under a deleted row moves up one, so those rows should be ones
the code has already tested.

So,
ascertain the last block row number with a bit of code.


y=Range("A" & Rows.Count).End(xlUp).Row 'Assumes nothing beneath the
block of data you are dealing with.
Do while y<4
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
End If
y = y - 1

Loop





"Doug Van" wrote in message
...
This is similar to the "Is this Possible?" post.

I want to delete a record that meets a certain condition.
This works with the first condition, but anything after the "or" won't
delete.

y = 5
Do While Range("A" & y) < ""
If Range("F" & y).Value = "957-TYPE" Or _
Range("F" & y).Value = "149-DUPLICATE" Then
Range("F" & y).EntireRow.Delete
End If
y = y + 1
Loop






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I delete a macro when the Delete button is not active? FCR Excel Worksheet Functions 0 March 9th 06 09:43 AM
How to Delete a Range in Closed Workbook (to Replace Delete Query) [email protected] Excel Discussion (Misc queries) 1 March 8th 06 10:10 AM
How do i delete a macro in Excel 2003 when delete isn't highlight Abel Excel Discussion (Misc queries) 2 September 13th 05 04:09 AM
How to delete rows when List toolbar's "delete" isnt highlighted? Linda Excel Worksheet Functions 1 May 26th 05 08:39 PM


All times are GMT +1. The time now is 02:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"