使用VBA中的inputbox除以零

时间:2017-05-21 03:34:54

标签: excel vba division divide-by-zero inputbox

这是我在Excel中使用输入框进行输入的代码。对不起,我是编程新手。

当用户尝试输入零时,我会让用户在第一个数字上输入零,但在第二个数字上输入零,我想要一条消息"他/她不能输入零​​&# 34 ;.但是当我这样做时,我按下取消或[x]按钮会出现相同的信息。此外,当我按OK而不输入数字时,将显示相同的信息

希望我能得到你们的帮助。谢谢!

Sub Division()

num1 = Application.InputBox(" Enter your 1st number ", "DIVISION", Type:=2)

    If Not IsNumeric(num1) Then

    MsgBox "Invalid Input! Please Enter a Number!", vbCritical, "WARNING"

        Exit Sub

    ElseIf num1 = "False" Then

        MsgBox "You wished to exit. Thank You!", 0, "EXIT MESSAGE"

        Exit Sub

    Else

        num2 = Application.InputBox(" Enter your 2nd number ", "ADDITION")

    End If

    If Not IsNumeric(num2) Then

        MsgBox "Invalid Input! Please Enter a Number!", vbCritical, "WARNING"

        Exit Sub

    ElseIf num2 = 0 Then

     MsgBox "You can't enter 0. Thank You!", 0, "EXIT MESSAGE"

        Exit Sub

    ElseIf num2 = "False" Then

        MsgBox "You wished to exit. Thank You!", 0, "EXIT MESSAGE"

        Exit Sub

    Else
        num3 = CDec(num1) / CDec(num2)

        MsgBox "The answer is " & num3 & "!", vbOKOnly, "Result"

        Exit Sub

    End If

End Sub

0 个答案:

没有答案