下一个没有错误的VBA [英] Next Without For Error VBA
问题描述
我有下面的代码,当我肯定有两个时,VBA给了我一个Next Without For的错误。我知道VBA可以列出与它所说的不完全相同的错误,但是我找不到任何其他的闭环。如果有人可以检查,这将是很棒!感谢:
$ b $ pre $ 选项显式
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
ng = InputBox(请输入学生的数字等级)
如果ng < 0然后
ng = 0
如果ng> 100然后
ng = 100
结束如果
单元格(c,2)。值(ng)
c = c + 1
v = InputBox(你想输入另一个等级吗?输入'Y'为'yes','N'为'no')
如果v =N则退出
End If
Loop
单元格(1,2).Value(Numerical Grade)
单元格(1,1).Value(Letter Grade)
对于r = 1到c
如果单元格(r,2)> 90然后
lg =A
单元格(r,1).Value(lg)
如果单元(r,2)> 80然后
lg =B
单元(c,1)。值(lg)
如果单元(r,2) > = 70然后,
lg =C
单元格(c,1)。值(lg)
如果单元格(r,2)> 60那么
lg =D
单元格(c,1)。值(lg)
其他
lg =F
单元格(c,1).Value(lg)
结束如果
r = r + 1
下一个r
c = c - 1
ca = Application.WorksheetFunction.Average((1,2) c))
如果ca> = 90那么
lg =A
如果ca> = 80那么
lg =B
如果ca > = 70然后
lg =C
如果ca> = 60那么
lg =D
否则
lg =F
结束如果
MsgBox(这些平均字母等级& (c)及学生是和 (lg)& 。)
sd = c *(Application.WorksheetFunction.Sum((1,2)(1,c)^ 2)) - Application.WorksheetFunction.Sum((1,2) ,c))^ 2 /(c *(c-1)))
MsgBox(这些等级的标准偏差是&(sd)&。)
End Sub
code> If ... Then ... If ... Then ... 而不是 If ... Then ... ElseIf ... Then。如果单元格(r,2)> = 90那么
lg
=A
单元格(r,1)。值(lg)
其他单元格(r,2)> = 80然后
lg =B
单元格c,1).Value(lg)
ElseIf Cells(r,2)> 70 Then
lg =C
单元格(c,1).Value(lg)
其他单元格(r,2)> = 60然后
lg =D
单元格(c,1)。值(lg)
其他
lg = F
单元格(c,1)。值(lg)
结束如果
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
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
这篇关于下一个没有错误的VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!