Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

excel - 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:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem is you are doing If... Then... If... Then... instead of 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...