Инструкция Next без оператора For
Оператору Next должен предшествовать соответствующий оператор For. Эта ошибка имеет следующие причину и решение:
- Оператору Next не предшествует соответствующий оператор For. Проверьте структуры других элементов управления в структуре For. Next и убедитесь в том, что они правильно сопоставлены. Например, эта ошибка может быть вызвана тем, что оператор If без соответствующего оператора End If в структуре For. Next.
Для получения дополнительной информации выберите необходимый элемент и нажмите клавишу F1 (для Windows) или HELP (для Macintosh).
Поддержка и обратная связь
Есть вопросы или отзывы, касающиеся Office VBA или этой статьи? Руководство по другим способам получения поддержки и отправки отзывов см. в статье Поддержка Office VBA и обратная связь.
Next without For
A Next statement must have a preceding For statement that matches. This error has the following cause and solution:
- A Next statement is used without a corresponding For statement. Check other control structures within the For. Next structure and verify that they are correctly matched. For example, an If without a matching End If inside the For. Next structure generates this error.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Next Without For Error VBA
I have the following code and VBA is giving me a «Next Without For» Error when I definitely have both. I know that VBA can list errors that are not exactly the same as what it says they are, but I can’t find any other closed loops. If someone could check this out, that would be awesome! Thanks:
4 Answers 4
Your problem is you are doing If. Then. If. Then. instead of If. Then. ElseIf. Then.
Every IF statement needs to be terminated with an ENDIF .
Within the FOR/NEXT loop you have 4 IFs , one ELSE and one ENDIF this needs to be changed to:
I think the nested If statements inside For r = 1 to c. don’t close properly? Generally, each If also requires an End If , and you only have one End If statement. This is causing the compiler to reach the Next r statement while it’s still «inside» an If block, thus the error raises, and makes sense.
You may look in to using a Select Case switch instead of nesting several If/Then statements. In my experience, they’re more easy to interpret when you’re debugging. Something like:
Note: You may want to omit the r = r+1 unless you’re intending to skip every other record, the Next statement automatically increments r by a value of 1 unless otherwise specified.
If you do intend to skip every other record, you should do For r = 1 to c Step 2 and likewise omit the r = r+1 .