我正在尝试使用InputBox函数来具有用户输入数据。但是,数据主要由十进制值组成。当我测试程序并去减去两个都从InputBox输入的值时,它不会执行数学运算。

thckmax = InputBox("What is the maximum nominal thickness?", "Enter Max Nominal Thickness Measurement")
thckmin = InputBox("What is the minimum nominal thickness?", "Enter Min Nominal Thickness Measurement")
thcknom = (thckmax + thckmin)


如果我分别输入.05和.04,它将显示前面的代码为:

thcknom is .05.04


我试图对这两个变量进行数学运算。但是,它只是并排“添加”两个字符串。如何解决此问题?

最佳答案

您需要根据预期输入将变量声明为DoubleSingle

Sub t()
Dim thckmax As Double, thckmin As Double, thcknom As Double
thckmax = InputBox("What is the maximum nominal thickness?", "Enter Max Nominal Thickness Measurement")
thckmin = InputBox("What is the minimum nominal thickness?", "Enter Min Nominal Thickness Measurement")
thcknom = (thckmax + thckmin)
Debug.Print thcknom
End Sub


有关更多信息,请参见VBA's data types

另外,我建议在模块的顶部添加Option Explicit,这将强制您声明所有变量。

关于excel - InputBox混淆带字符串的十进制条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55027629/

10-13 04:26