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:office:office" /><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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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:office:office" /><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,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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