在嵌套的Excel VBA For ... Next循环中,父Next没有找到它的For

Fel*_*lix 0 vba

For j = 0 to SpreadCount
    If TypeName(ChainsDC(j,0) = "String" 'testing for an error
        Goto EndLoop
    Else:
        For i = 0 to RscSct
            ...do amazing things here (no loops)
        Next i
EndLoop:
Next j '<<<PROBLEM
Run Code Online (Sandbox Code Playgroud)

编译器抱怨下一个j(第9行)是"Next without For".

Pow*_*ser 5

您的IF语句是问题所在.您需要添加一个Then和一个End If:

For j = 0 to SpreadCount
    If TypeName(ChainsDC(j,0) = "String" **Then**
        Goto EndLoop
    Else:
        For i = 0 to RscSct
            ...do amazing things here (no loops)
        Next i
    **End If**
EndLoop:
Next j '<<<PROBLEM
Run Code Online (Sandbox Code Playgroud)

编辑
您的Typename也缺少一个右括号.

  • +1我认为可以通过@MonteCarloGenerator的很少努力来避免goto (2认同)
  • 实际上,您可以用*nothing*替换goto并获得相同的效果! (2认同)