下一步没有错误VBA

use*_*591 7 excel vba for-loop

我有以下代码,当我肯定有两个时,VBA给我一个"Next Without For"错误.我知道VBA可以列出与它们所说的不完全相同的错误,但我找不到任何其他闭环.如果有人可以检查出来,那就太棒了!谢谢:

Option Explicit
Sub HW09()

    Dim ng As Integer
    Dim v As String
    Dim lg As String
    Dim ca As Integer
    Dim sd As Integer
    Dim c As Integer
    Dim r As Integer

    c = 2

    Do
        ng = InputBox("Please enter the student's numerical grade.")
        If ng < 0 Then
            ng = 0
        If ng > 100 Then
            ng = 100
        End If

        Cells(c, 2).Value (ng)
        c = c + 1

        v = InputBox("Would you like to enter another grade? Type 'Y' for yes and 'N' for no.")
        If v = "N" Then Exit Do
        End If

    Loop

    Cells(1, 2).Value ("Numerical Grade")
    Cells(1, 1).Value ("Letter Grade")

    For r = 1 To c
        If Cells(r, 2) >= 90 Then
            lg = "A"
            Cells(r, 1).Value (lg)
        If Cells(r, 2) >= 80 Then
            lg = "B"
            Cells(c, 1).Value (lg)
        If Cells(r, 2) >= 70 Then
            lg = "C"
            Cells(c, 1).Value (lg)
        If Cells(r, 2) >= 60 Then
            lg = "D"
            Cells(c, 1).Value (lg)
        Else
            lg = "F"
            Cells(c, 1).Value (lg)
        End If

        r = r + 1

    Next r

    c = c - 1

    ca = Application.WorksheetFunction.Average("(1,2):(1,c)")
    If ca >= 90 Then
        lg = "A"
    If ca >= 80 Then
        lg = "B"
    If ca >= 70 Then
        lg = "C"
    If ca >= 60 Then
        lg = "D"
    Else
        lg = "F"
    End If

    MsgBox ("The average letter grade for these " & (c) & " students is " & (lg) & ".")
    sd = c * (Application.WorksheetFunction.Sum("(1, 2)(1, c) ^ 2)")-Application.WorksheetFunction.Sum("(1, 2)(1, c)") ^ 2 / (c * (c - 1)))
    MsgBox ("The standard deviation for these grades is" & (sd) & ".")

End Sub
Run Code Online (Sandbox Code Playgroud)

Dre*_*pin 13

你的问题是你做的If... Then... If... Then...而不是If... Then... ElseIf... Then...

If Cells(r, 2) >= 90 Then
    lg = "A"
    Cells(r, 1).Value (lg)
ElseIf Cells(r, 2) >= 80 Then
    lg = "B"
    Cells(c, 1).Value (lg)
ElseIf Cells(r, 2) >= 70 Then
    lg = "C"
    Cells(c, 1).Value (lg)
ElseIf Cells(r, 2) >= 60 Then
    lg = "D"
    Cells(c, 1).Value (lg)
Else
    lg = "F"
    Cells(c, 1).Value (lg)
End If
Run Code Online (Sandbox Code Playgroud)


Gre*_*g J 7

每个人都IF statement需要终止ENDIF.
FOR/NEXT loop你有4个IFs,一个ELSE和一个ENDIF 这需要改为:

IF Condition1 THEN
  'code
 ELSEIF Condition2 THEN
  'code
 ELSEIF Condition3 THEN
  'code
 ELSEIF Condition4 THEN
  'code
 ELSE 'All other possibilities
  'code
ENDIF
Run Code Online (Sandbox Code Playgroud)


Dav*_*ens 5

我认为If里面的嵌套语句For r = 1 to c...没有正确关闭?通常,每个If也需要一个End If,而您只有一个End If声明.这导致编译器Next r在它仍在" If块"内部时到达语句,因此错误提升,并且有意义.

您可以查看使用Select Case开关而不是嵌套多个If/Then语句.根据我的经验,在调试时它们更容易解释.就像是:

For r = 1 To c
    Select Case Cells(r,2)
        Case >= 90
           lg = "A"

        Case >= 80
           lg = "B"

        Case >= 70
           lg = "C"

        Case >= 60
           lg = "D"
        Case Else
           lg = "F"
     End Select
     Cells(r,1).Value = lg


r = r + 1  '## You may want to omit this line, see my comments.


Next r
Run Code Online (Sandbox Code Playgroud)

注意:您可能希望省略r = r+1除非您打算跳过其他所有记录,除非另有说明,否则该Next语句会自动递增r一个值1.

如果你打算跳过其他所有记录,你应该这样做For r = 1 to c Step 2,同样省略r = r+1.