6

我的代码有问题,出现错误,我不明白为什么。错误是:
“编译错误:Next without For”
我不明白为什么会这样。我是编码新手,因此非常欢迎任何帮助和评论。
这是代码,为没有 For 的 Next 提供了注释。

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  

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

4

2 回答 2

8

Every For statement with a body must have a matching Next, and every If-Then statement with a body must have a matching End If.

Example:

For i = 1 To 10 '<---- This is the header

    Hello(i) = "Blah"  '<---- This is the body

Next i  '<---- This is the closing statement

You have part of the body of your If statement inside your For i loop, and part of it outside. It has to be either ALL inside or ALL outside. Think through the logic and see what it is you want to do.

于 2013-08-14T13:49:31.463 回答
3

你有重叠的循环——也许

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
于 2013-08-14T13:26:11.547 回答