为什么当我使用“Format(string_variable,”###。#“)”并且string_variable的值被设置为0.0时,VBA会引发“types不匹配”错误

我的股票交易代码格式的价格变化(单个variables)传输到我的手机,并限制无意义数字的数量。 当价格变化为零时,模块引发“types不匹配”错误。 所有非零值工作正常。 我写了一个简单的模块,展现了这种行为。

Sub test_format() Dim test_single As Single, output As String test_single = 0# output = Str(Format(test_single, "###.#")) Debug.Print test_single; output End Sub 

您需要删除Str()函数

 Sub test_format() Dim test_single As Single, output As String test_single = 112.25 output = Format(test_single, "##0.0") Debug.Print output End Sub 

我的输出:

 112.3 

编辑:更新格式为“## 0.0”。 现在,如果你得到的值为0.0,它不会只返回“。”,但实际上会返回0.0