运行时错误13不匹配Microsoft Excel 2010 [英] Runtime Error 13 Mismatch Microsoft Excel 2010
问题描述
嘿伙计们,
我是VBA的新手,我对这个基本程序有疑问。 我希望能够为计算机提供2个数字,并告诉我更大的数字。 如果我输入的内容不是数字,我想要显示错误消息。 如果我输入数字
,程序运行正常。 但是,一旦我输入的内容不是,而不是我的错误消息,我得到运行时错误13不匹配。 任何帮助将不胜感激。谢谢!
I'm new to VBA and am having a problem with this basic program. I want to be able to give the computer 2 numbers and it tell me what the larger one is. If I type something that isn't a number I want an Error message to show up. If I type in numbers the program works fine. However once I type in something that's not, instead of my error message I get Runtime Error 13 Mismatch. Any help would be greatly appreciated. Thanks!
Sub Homework3()
Dim inputValue1 As Double
Dim inputValue2 As Double
inputValue1 = InputBox(" Input first value"," Value1"," 0")
inputValue2 = InputBox("输入第二个值","Value2")
如果不是IsNumeric(inputValue1)则为
MsgBox"错误,第一次输入不是数字。","vbExclamation","错误"和"b $ b   ;结束如果
如果不是IsNumeric(inputValue2)则为
MsgBox"错误,第二次输入不是数字。","vbExclamation","错误"和"b $ b   ;结束如果
如果inputValue1> = inputValue2则为
MsgBox"较大的数字是" &安培; inputValue1,vbInformation," LargerNumber"
否则为
MsgBox"较大的数字是" &安培; inputValue2,vbInformation," LargerNumber"
结束如果
$
结束子
Sub Homework3()
Dim inputValue1 As Double
Dim inputValue2 As Double
inputValue1 = InputBox("Input first value", "Value1", "0")
inputValue2 = InputBox("Input second value", "Value2")
If Not IsNumeric(inputValue1) Then
MsgBox "Error, first input was not a number.", vbExclamation, "Error"
End If
If Not IsNumeric(inputValue2) Then
MsgBox "Error, second input was not a number.", vbExclamation, "Error"
End If
If inputValue1 >= inputValue2 Then
MsgBox "The larger number is " & inputValue1, vbInformation, "LargerNumber"
Else
MsgBox "The larger number is " & inputValue2, vbInformation, "LargerNumber"
End If
End Sub
推荐答案
一种选择是声明inputValue1和inputValue2为Variant而不是Double:
One option is to declare inputValue1 and inputValue2 as Variant instead of as Double:
Dim inputValue1 As Variant
Dim inputValue2 As Variant
另一种选择是将变量声明为Double,并使用Excel的Application.InputBox函数强制输入为数字:
Another option is to leave the variables declared as Double, and to use Excel's Application.InputBox function to force the input to be numeric:
inputValue1 = Application.InputBox("Input first value", "Value1", 0, , , , , 1)
inputValue2 = Application.InputBox("Input second value", "Value2", , , , , , 1)
使用此选项,可以省略IsNumeric测试。
With this option, you can omit the IsNumeric tests.
这篇关于运行时错误13不匹配Microsoft Excel 2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!