Next Without For error (again)

steveh8204

Board Regular
Joined
Aug 20, 2018
Messages
143
Right, once again I'm struggling with the Next without For error. I've got a loop ("i") which if a certain condition is met (d2 > d) I want to terminate immediately and return to the beginning of the If statement.


If d2 isn't greater than I want the loop to continue until the Next command further down but I keep getting error command.


Am I missing something obvious as this isn't the first time I've had problems with this loop.


Code:
r = Cells(Rows.Count, 1).End(xlUp).Row - 2


For i = 3 To r + 2


' determine the cell to get lookup value from
v = Cells(i, 1).Value


Ptrue = Application.WorksheetFunction.VLookup(v, Sheets("SAP").Range("sapstock"), 4, False)


sapstock = Cells(i, 4)


lookup1 = 0 ' make sure variable is reset from previous loops


lookup1 = Application.VLookup(v, Range("allstock"), 4, False)


d = Now
d2 = Cells(i, 5) ' if the date is in the future I need the loop to go back to the beginning.


If d2 > d Then


Next


ElseIf lookup1 = sapstock Then
    Cells(i, 6).Value = "Quantity Is Correct"
    Cells(i, 7).Value = "" ' used to blank cells if re-running check
    Cells(i, 8).Value = ""


Else
 
Cells(i, 6).Value = "Incorrect Quantity"


Cells(i, 7).Value = lookup1 - sapstock


End If


Next i
 
I still get the Next without For error. Oh well the Goto command works a bit better but has thrown up another error 30 odd lines into the execution. Typical.

Will post more details when I get a chance as it's a different kind of error.

Thanks for all your replies.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,215,892
Messages
6,127,610
Members
449,389
Latest member
ChessManNaill

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