永发信息网

EXCEL VBA中明明遇到不符合继续运行的条件时,为什么还能继续运行?

答案:3  悬赏:0  手机版
解决时间 2021-03-20 18:28
  • 提问者网友:不爱我么
  • 2021-03-19 18:18
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

当我明明有一个文本框内为空时,它虽然也弹出了提示窗口,但是它还是继续生成了计算结果的窗口。这是怎么回事呢?我想让它必须在三项都填有数据的情况下才能产生计算结果,应该怎么样修改程序呢?
最佳答案
  • 五星知识达人网友:行雁书
  • 2021-03-19 19:29
设置一个中间变量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
全部回答
  • 1楼网友:不如潦草
  • 2021-03-19 21:07
你想对三个输入强制非空,可用do while ......loop循环检查.do { while | until } condition [ statements ] [ exit do ] [ statements ]loop----------------------------------------------------do while textbox1 = "" or textbox3 = "" or textbox4 = "" msgbox "三项数据必须完整" if textbox1.value http://zhidao.baidu.com/question/= "" then textbox1.setfocus elseif textbox3.value http://zhidao.baidu.com/question/= "" then textbox3.setfocus elseif textbox4.value http://zhidao.baidu.com/question/= "" then textbox4.setfocus end ifloop
  • 2楼网友:撞了怀
  • 2021-03-19 20:16
你想对三个输入强制非空,可用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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯