谁能帮我解决这个问题吗?VB

来源:百度知道 编辑:UC知道 时间:2024/05/27 18:53:25
"密码正确"后面的for 我是不知道要放哪里,如果没有for ,又会提示我next 后面没有for 错误.
要怎么完成这个呢?
Option Explicit

Private Sub command1_click()
Const password = "48"
Dim intcounter As Integer
Dim strpassword As String

For intcounter = 1 To 3
strpassword = InputBox("问:", "几月几号~?")
If strpassword = password Then
MsgBox "亲爱的!", , "密码正确!"For

Next intcounter
If intcounter > 3 Then
MsgBox "你是谁?", vbOKOnly + vbCritical, "拒绝"
Unload Me
End If
End Sub
为什么按1楼说的改完,执行的时候没反应呢?

If strpassword = password Then
MsgBox "亲爱的!", , "密码正确!"
Next intcounter
----------------------------------------------------------------------
这里的If语句缺少End If结束,所以编译器会认为到这里If语句还没结束,自然就会在该If语句里找与Next intcounter匹配的For,很遗憾,肯定找不到罗。你自己那么想当然地添加一个For当然是错误的。

----------------------------------------------------------------------
修改后代码如下:

Option Explicit

Private Sub command1_click()
Const password = "48"
Dim intcounter As Integer
Dim strpassword As String

For intcounter = 1 To 3
strpassword = InputBox("问:", "几月几号~?")
If strpassword = password Then
MsgBox "亲爱的!", , "密码正确!"
Exit For
End If
Next intcounter
If intcounter > 3 Then
MsgBox "你是谁?", vbOKOnly + vbCritical, "拒绝"
Unload Me
End If
End Sub

Option Explicit

Private Sub command1_c