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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With