Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default Conditional Row Deletion

I'm having trouble with this code:

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Cells(i, "D").Value < "MN*" Or Cells(i, "D").Value < "MP107*" Then
Rows(i).Delete
End If
Next i

End Sub

I'm trying to delete all rows where the text contained in column D DOES NOT
equal MN or MP107. When I run this procedure, it deletes ALL rows. Help!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default Conditional Row Deletion

Hi Kirk

by your code try

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Left(Cells(i, "D").Value, 2) < "MN" Or Left(Cells(i,
"D").Value, 5) < "MP107" Then
Rows(i).Delete
End If
Next i

End Sub

"Kirk P." wrote in message
...
I'm having trouble with this code:

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Cells(i, "D").Value < "MN*" Or Cells(i, "D").Value < "MP107*" Then
Rows(i).Delete
End If
Next i

End Sub

I'm trying to delete all rows where the text contained in column D DOES
NOT
equal MN or MP107. When I run this procedure, it deletes ALL rows. Help!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Conditional Row Deletion

Two 'Nots' make an 'And', your conditions not equal OR not equal will
always be meet. Try using

If Cells(i, "D").Value < "MN*" AND Cells(i, "D").Value < "MP107*" Then

You also cannot use a wildcard in your conditions this clause eg "MN*" -
are you looking for the strings anywhere within the cell(s) ? - if so just
use MN or MP107.


--
Cheers
Nigel



"Kirk P." wrote in message
...
I'm having trouble with this code:

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Cells(i, "D").Value < "MN*" Or Cells(i, "D").Value < "MP107*" Then
Rows(i).Delete
End If
Next i

End Sub

I'm trying to delete all rows where the text contained in column D DOES

NOT
equal MN or MP107. When I run this procedure, it deletes ALL rows. Help!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 59
Default Conditional Row Deletion

Kirk,

Try

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Not (Cells(i, "D").Value = "MN" Or Cells(i, "D").Value = "MP107")
Then
Rows(i).Delete
End If
Next i
End Sub


You used wild cards where you specified either MN or MP107.

I also made the logic a little easier to understand.

HTH


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

You want AND instead of OR

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Not(Cells(i, "D").Value Like "MN*") AND _
Not(Cells(i, "D").Value Like "MP107*") Then
Rows(i).Delete
End If
Next i

End Sub


or


Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Not(Cells(i, "D").Value Like "MN*" Or _
Cells(i, "D").Value Like "MP107*") Then
Rows(i).Delete
End If
Next i

End Sub


--
Regards,
Tom Ogilvy


"Kirk P." wrote in message
...
I'm having trouble with this code:

Sub RowDelete()

Dim i As Long
Dim iLastRow As Long

iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If Cells(i, "D").Value < "MN*" Or Cells(i, "D").Value < "MP107*" Then
Rows(i).Delete
End If
Next i

End Sub

I'm trying to delete all rows where the text contained in column D DOES

NOT
equal MN or MP107. When I run this procedure, it deletes ALL rows. Help!



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
#Ref! after row deletion Rick Excel Discussion (Misc queries) 3 March 19th 10 04:39 PM
name deletion rk0909 Excel Discussion (Misc queries) 2 January 4th 08 11:03 PM
Conditional deletion of cell contents Colin Hayes Excel Worksheet Functions 2 August 9th 07 10:31 PM
Problem with Conditional format deletion [email protected] Excel Discussion (Misc queries) 3 December 13th 04 05:10 PM
Row Deletion Dan Excel Programming 3 September 1st 04 10:40 PM


All times are GMT +1. The time now is 01:41 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"