编译错误:下一个没有For || VBA

我的代码有问题,出现错误,我不明白为什么。 错误是:
“编译错误:下一个没有For”
我不明白为什么这样。 我是新来的编码,所以任何帮助和意见都是值得欢迎的。
这是代码,下一个被指出为没有For的人提供了一个评论。

Sub CGT_Cost() startrow = Worksheets("GUTS").Cells(10, 1) 'Here I put 1 endrow = Worksheets("GUTS").Cells(11, 1) 'Here I put 1000 For x = endrow To startrow Step -1 If Cells(x, "Q").Value = "Sale" Then If Cells(x, "D").Value = "1" Then For i = 1 To 1000 If Cells(x - i, "R").Value <> "1" Then Next i Else Range("G" & x).FormulaR1C1 = "=R[-" & i & "]C/R[-" & i & "]C[-1]*RC[-1]" End If End If End If Next x End Sub 

提前谢谢大家,最好的问候,
阿图尔。

For Body的每个For语句都必须有一个匹配的Next ,并且每个具有Body的If-Then语句都必须有一个匹配的End If

例:

 For i = 1 To 10 '<---- This is the header Hello(i) = "Blah" '<---- This is the body Next i '<---- This is the closing statement 

你在For循环中有你的If语句的一部分,而且它的一部分在外面。 它必须是全部内部或全部外部。 通过逻辑思考,看看你想要做什么。

你有重叠的循环 – 也许

 Sub CGT_Cost() startrow = Worksheets("GUTS").Cells(10, 1) 'Here I put 1 endrow = Worksheets("GUTS").Cells(11, 1) 'Here I put 1000 For x = endrow To startrow Step -1 If Cells(x, "Q").Value = "Sale" Then If Cells(x, "D").Value = "1" Then For i = 1 To 1000 If Cells(x - i, "R").Value <> "1" Then ' Else Range("G" & x).FormulaR1C1 = "=R[-" & i & "]C/R[-" & i & "]C[-1]*RC[-1]" End If Next i End If End If Next x End Sub