vb代码 ,为什么说next 没有for

2025-03-23 03:46:05
推荐回答(4个)
回答1:

因为For循环内的if没有end if。

改为:

For i = 1 To 100

If isnarc(a(i)) = True Then

sum = sum + a(i)

end if

Next i

或:

For i = 1 To 100

If isnarc(a(i)) = True Then sum = sum + a(i)

Next i

扩展资料:

注意事项

If与end If配对(除非它写在同一行上,否则不使用end If)。如果它没有配对(例如,有四个If,但只有三个endif),它将影响它周围的其他结构,例如for next循环。

1、 如果它是一行If,则结束If,例如,如果5>3,则msgbox“yes”

如果它是多行,则必须有结束符,例如:

if a=3 then

b=2

end if

2、加else跟上面一样,如果是单行的,可以是If 5 > 8 Then MsgBox "yes" Else MsgBox "no"

多行的if then else 类似这种

if a=3 then

b=2

else

b=1

end if

回答2:

原因:如果if....then...语句放在两行上,必须用end if 结尾,所以你缺了一个end if,像这种情况vb中只提示“for 缺少next”,似乎描述不清楚,但熟悉了就知道了。
——如果if....then...语句放在一行上,就不用end if 结尾了。

楼主可以这样解决:
1、在Picture1.Print i后面增加一个语句:end if,变成如下程序:
......
Next m
If j = True Then
Picture1.Print i
end if '这一句是新增加的
Next i
......
2、把If j = True Then和 Picture1.Print i放在一行上就可以,程序是这样子地:
......
Next m
If j = True Then Picture1.Print i '这一句必须放在一行上
Next i
......

OK?

回答3:

像一楼说的,你的第二个IF判断没有end if
————————————————
Private Sub Command1_Click()
Dim i%, m%, j As Boolean
For i = 3 To 100
For m = 2 To i - 1
If i Mod m = 0 Then
j = False
Exit For
End If
Next m
If j = True Then
Picture1.Print i
End If
Next i
End Sub
————————————————
你要注意一下代码的缩排格式,上面的代码添加了end if
下面的代码也可以,就是将 If j = True Then和 Picture1.Print i这两条语句放在一行写

————————————————
Private Sub Command1_Click()
Dim i%, m%, j As Boolean
For i = 3 To 100
For m = 2 To i - 1
If i Mod m = 0 Then
j = False
Exit For
End If
Next m
If j = True Then Picture1.Print i
Next i
End Sub
————————————————

回答4:

你最后一个if没有end if
If j = True Then
Picture1.Print i
end if