Issue with For Next and If Then statement

needhelpplease

New Member
Joined
May 20, 2011
Messages
4
I want to delete every row that does not have 28168-59 or 19062-59 in column B, except of course the header.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
This worked (another than the header part) before I added an OR with another part#, but now that I have both, it’s deleting everything in the spreadsheet.<o:p></o:p>
<o:p></o:p>
Any ideas?<o:p></o:p>
<o:p></o:p>
Sub main()<o:p></o:p>
Dim ws As Worksheet<o:p></o:p>
<o:p></o:p>
Set ws = ActiveSheet<o:p></o:p>
<o:p></o:p>
For i = ws.Range("B65536").End(xlUp).Row To 1 Step -1<o:p></o:p>
If ws.Cells(i, 2) <> "28168-59" Or ws.Cells(i, 2) <> "19062-59" Then<o:p></o:p>
<o:p></o:p>
ws.Rows(i).Delete<o:p></o:p>
<o:p></o:p>
End If<o:p></o:p>
Next<o:p></o:p>
<o:p></o:p>
End Sub<o:p></o:p>
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this


Code:
Sub main()
Dim ws As Worksheet
Dim i As Long
Set ws = ActiveSheet
For i = ws.Range("B65536").End(xlUp).Row To 1 Step -1
If ws.Cells(i, 2) <> "28168-59" And ws.Cells(i, 2) <> "19062-59" Then
ws.Rows(i).Delete
End If
Next i
End Sub
 
Upvote 0
Thanks! That worked. Loops have never been my forte.

If I want the loop to stop before the header row, would I change the <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
For i = ws.Range("B65536").End(xlUp).Row To 1 Step -1<o:p></o:p>
<o:p> </o:p>
To….Row To 2 Step -1?<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,216,112
Messages
6,128,901
Members
449,477
Latest member
panjongshing

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top