如何在VBA中四舍五入到小数点后两位?

时间:2018-11-01 10:32:48

标签: excel vba

在单元格B2中,在进行计算之前,我的变量值为297.123。在VBA中,我想将此值舍入为297.12。有关我尝试过的内容,请参见下面的代码。双方都将燃油提高到297。我在做什么错了?

Dim fuel As Integer
Dim linecount As Long

linecount2 = 2

fuel = Format(ws.Cells(linecount2, 2), "0.00")
fuel = Round(ws.Cells(linecount2, 2), 2)

1 个答案:

答案 0 :(得分:4)

更改Dim fuel As Long-> Dim fuel As Double

这两个公式都会起作用。

Dim fuel As Double
Dim linecount2 As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")


linecount2 = 2

fuel = Format(ws.Cells(linecount2, 2), "0.00")
fuel = Round(ws.Cells(linecount2, 2), 2)
fuel = WorksheetFunction.Round(ws.Cells(linecount2, 2), 2)


MsgBox "try"

End Sub
  • Dim Long用于整数(整数)
  • Dim Double用于带小数点的数字(可以存储更多的小数 分[15])
  • Dim Single用于带小数点的数字(存储较少的小数 分[7])