EXCEL VBA中明明遇到不符合继续运行的条件时,为什么还能继续运行?
PrivateSubCommandButton1_Click()IfTextBox1=""OrTextBox3=""OrTextBox4=""ThenMsgBox"三项数...
Private Sub CommandButton1_Click()
If TextBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Then
MsgBox "三项数据必须完整"
If TextBox1.Value = "" Then TextBox1.SetFocus
If TextBox3.Value = "" Then TextBox3.SetFocus
If TextBox4.Value = "" Then TextBox4.SetFocus
Else
End If
a = MsgBox("结果是:" & TextBox1.Value + TextBox3.Value + TextBox4.Value, vbOKCancel, "计算结果")
End Sub
当我明明有一个文本框内为空时,它虽然也弹出了提示窗口,但是它还是继续生成了计算结果的窗口。这是怎么回事呢?我想让它必须在三项都填有数据的情况下才能产生计算结果,应该怎么样修改程序呢? 展开
If TextBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Then
MsgBox "三项数据必须完整"
If TextBox1.Value = "" Then TextBox1.SetFocus
If TextBox3.Value = "" Then TextBox3.SetFocus
If TextBox4.Value = "" Then TextBox4.SetFocus
Else
End If
a = MsgBox("结果是:" & TextBox1.Value + TextBox3.Value + TextBox4.Value, vbOKCancel, "计算结果")
End Sub
当我明明有一个文本框内为空时,它虽然也弹出了提示窗口,但是它还是继续生成了计算结果的窗口。这是怎么回事呢?我想让它必须在三项都填有数据的情况下才能产生计算结果,应该怎么样修改程序呢? 展开
3个回答
展开全部
设置一个中间变量k来记录是否三项都填有数据,如果填了,k=0,如果没有填,k=1,通过判断k的值来决定是否继续生成计算结果的窗口
Private Sub CommandButton1_Click()
Dim k As Integer
k = 0
If TextBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Then
k = 1
MsgBox "三项数据必须完整"
If TextBox1.Value = "" Then TextBox1.SetFocus
If TextBox3.Value = "" Then TextBox3.SetFocus
If TextBox4.Value = "" Then TextBox4.SetFocus
Else
End If
If k = 0 Then
a = MsgBox("结果是:" & TextBox1.Value + TextBox3.Value + TextBox4.Value, vbOKCancel, "计算结果")
End If
End Sub
Private Sub CommandButton1_Click()
Dim k As Integer
k = 0
If TextBox1 = "" Or TextBox3 = "" Or TextBox4 = "" Then
k = 1
MsgBox "三项数据必须完整"
If TextBox1.Value = "" Then TextBox1.SetFocus
If TextBox3.Value = "" Then TextBox3.SetFocus
If TextBox4.Value = "" Then TextBox4.SetFocus
Else
End If
If k = 0 Then
a = MsgBox("结果是:" & TextBox1.Value + TextBox3.Value + TextBox4.Value, vbOKCancel, "计算结果")
End If
End Sub
展开全部
你想对三个输入强制非空,可用DO WHILE ......LOOP循环检查.
Do { While | Until } condition
[ statements ]
[ Exit Do ]
[ statements ]
Loop
----------------------------------------------------
Do While TextBox1 = "" Or TextBox3 = "" Or TextBox4 = ""
MsgBox "三项数据必须完整"
If TextBox1.Value = "" Then TextBox1.SetFocus
elseIf TextBox3.Value = "" Then TextBox3.SetFocus
elseIf TextBox4.Value = "" Then TextBox4.SetFocus
end if
Loop
Do { While | Until } condition
[ statements ]
[ Exit Do ]
[ statements ]
Loop
----------------------------------------------------
Do While TextBox1 = "" Or TextBox3 = "" Or TextBox4 = ""
MsgBox "三项数据必须完整"
If TextBox1.Value = "" Then TextBox1.SetFocus
elseIf TextBox3.Value = "" Then TextBox3.SetFocus
elseIf TextBox4.Value = "" Then TextBox4.SetFocus
end if
Loop
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
结果行 应该放到ELSE 下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |