Inputbox不接受双数的VBA excel

我有一个声明像number= InputBox("Number for:", "Number:") ,数字被声明为Dim number As Double但是当我input一个双数字,例如5.4 ,进入Inputbox并将其传输到一个单元格,单元格显示我54 ,它删除了点。

我怎样才能解决这个问题?

谢谢

如果您想要检测Excel使用哪些设置作为十进制分隔符 ,请尝试下面的代码:

 MsgBox "Excel uses " & Chr(34) & Application.DecimalSeparator & Chr(34) & " as a decimal seperator" 

如果你想改变它. ,然后使用下面的行:

 Application.DecimalSeparator = "." 

不幸的是,VBA在处理十进制分组差异方面很糟糕。 在你的情况下,你应该使用逗号( , ),而不是标点/点( . )。

编辑:使用Application.DecimalSeparator方法,它现在工作,不pipe区域设置。 但请注意,如果您更改Excel的逗号分隔符设置(似乎VBA会忽略此设置),似乎会导致一些问题。 但是,如果你不改变这个例子,那么这个例子应该可以用在其他所有的CAS中

 Sub GetNumberFromInputBox() Dim val As String Dim num As Double 'Get input val = InputBox("Number for:", "Number:") Debug.Print Application.DecimalSeparator If IsNumeric(val) Then 'Try to convert to double as usual num = CDbl(val) 'If the dot is removed automatically, then 'you will se a difference in the length. In 'those cases, replace the dot with a comma, 'before converting to double If Len(val) <> Len(num) Then If Application.DecimalSeparator = "," Then num = CDbl(Replace(val, ".", ",")) Else num = CDbl(Replace(val, ",", ".")) End If End If 'Pring the number Debug.Print "You selected number: " & num Else 'If its not a number at all, throw an error Debug.Print "You typed " & val & ", which is not a number" End If End Sub