Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 952
user2829591 Avatar asked Sep 30 '13 01:09

user2829591


People also ask

What is next without for error in VBA?

The “Next Without For” Compile Error is a very common compile-time error in Excel VBA. It implies that a Next statement must always have a preceding For statement that matches it. If a Next statement is used without a corresponding For statement, this error is generated.

What does next mean in VBA?

In Excel VBA the “For Next” loop is used to go through a block of code a specific number of times. Syntax: For counter = start to end [step count] statement 1 statement 2 statement 3 . . statement n Next [counter] Here we can use the counter or any other variable to run them as many times as we need.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.

What is block if without end if?

The Compile Error “End If without Block If: This is a simple compile time error that's thrown when the code containing any If blocks do not comply with the syntax (or) such a statement does not exist.


1 Answers

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
like image 131
Drew Chapin Avatar answered Oct 02 '22 06:10

Drew Chapin