ExcelBanter

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

J.W. Aldridge

Delete all rows except
 
Was given this code, but doesnt work. It deletes everything.
The data on the worksheet was imported - i think this has something to
do with the reason it doesnt work (or filter) properly.

Sub DeleteRows()


Dim LastRow As Long
Dim rw As Long


Application.ScreenUpdating = False


LastRow = Cells(Rows.Count, "B").End(xlUp).Row


For rw = LastRow To 2 Step -1
If Cells(rw, "B").Value < "Apples" Or Cells(rw, "B").Value
<
"Oranges" Then
Rows(rw).Delete Shift:=xlUp
End If
Next rw


Application.ScreenUpdating = True


End Sub



Charabeuh[_4_]

Delete all rows except
 
Hi

If a cell is equal to "Apples" then it is not equal to "Oranges"
== the test with "Apples" return False
== the test with "Oranges" return True
False OR True return True

If a cell is equal to "Oranges" then it is not equal to "Apples"
== the test with "Apples" returns True
== the test with "Oranges" returns False
True OR False return True

If a cell is not equal to "Oranges" and is not equal to "Apples"
== the test with "Apples" returns True
== the test with "Oranges" returns True
True OR True returns True

Your formula returns always TRUE

? try to replace OR with AND




"J.W. Aldridge" a écrit dans le message de
groupe de discussion :
...
Was given this code, but doesnt work. It deletes everything.
The data on the worksheet was imported - i think this has something to
do with the reason it doesnt work (or filter) properly.

Sub DeleteRows()


Dim LastRow As Long
Dim rw As Long


Application.ScreenUpdating = False


LastRow = Cells(Rows.Count, "B").End(xlUp).Row


For rw = LastRow To 2 Step -1
If Cells(rw, "B").Value < "Apples" Or Cells(rw, "B").Value
<
"Oranges" Then
Rows(rw).Delete Shift:=xlUp
End If
Next rw


Application.ScreenUpdating = True


End Sub



J.W. Aldridge

Delete all rows except
 
you were SO right.

Thanx a million..... It works now!



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

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