用小数表示的文本框的总和

时间:2019-08-30 09:46:20

标签: excel vba

我有一个listbox1,以€计价价格(带小数)。价格的总和是我的TextBox1中的值。

With ThisWorkbook.Sheets("Sheet1")
   Me.ListBox1.AddItem .Range("B2").Value
   Me.ListBox1.Column(1, ListBox1.ListCount - 1) = Format(Val(.Range("C2").Value), "€#,##0.00")
   Me.TextBox1.Value = CDbl(Me.TextBox1.Value) + .Range("C2").Value
   Me.TextBox1.Value = Format(Me.TextBox1.Value, "#,##0.00")
End With

当我的总和是1.60 + 1.60时,我在文本框中的值为32,00。我希望输出为3.20。

1 个答案:

答案 0 :(得分:2)

VBA中的小数点符号取决于PC的区域设置。如果是点,那么它将是代码中的点。在此处进行更改-在Windows中搜索“自定义格式”并更改“十进制符号”:

enter image description here

作为一种“丑陋”的解决方法,请尝试使用Replace()公式:

Sub TestMe()
    MsgBox (Format(32, "#,##0.00"))
    MsgBox (Replace(Format(32, "##0.00"), ",", "."))
End Sub

这是“丑陋的”,因为该数字将被视为String,而不是数字。但是只显示它也可以。还要考虑千位分隔符。