For...Next syntax

JimM

Well-known Member
Joined
Nov 11, 2003
Messages
752
Hi all

I'm having some trouble with a FOR.....NEXT loop that I've written. The code works fine at the moment but what I want to do is put an IF statement in, so that if true will move on to the NEXT without running the rest of the code within the loop.

A simplified version of what I'm trying to do is


FOR i = 1 to 100

code to assign a value to VAR1
code to assign a value to VAR2
more code
more code

IF VAR1 = 1 and VAR2 = 1 then next

more code
more code

NEXT



When I run this I keep getting a 'Next without For' error, what is the correct way to write the IF statement to do what I want

Thanks

Jim
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Something like this:
Code:
FOR i = 1 to 100

code to assign a value to VAR1
code to assign a value to VAR2
more code
more code

IF VAR1 <> 1 or VAR2 <> 1 then

more code
more code

End IF

NEXT
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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