excel vba中的小数位数之和

时间:2015-03-05 19:49:31

标签: excel-vba integer decimal vba excel

我想使用excel vba对一列的单元格求和。单元格中的值是deimals,但excel vba给出的总和始终是整数。例如:2.5 +2.1 = 4.6 excel将显示5.如何显示4.6值?

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

使用 Double 数据类型:

Sub SumTheValues()
    Dim zum As Double
    zum = Application.WorksheetFunction.Sum(Range("A:A"))
    MsgBox zum
End Sub

对于您的数据:

enter image description here

答案 1 :(得分:0)

我从另一篇文章中找到。选项4最好-https://www.exceltrick.com/how_to/sum-cells-based-on-background-color

然后我接受了上面的内容,并替换了一个单词-从“只要”改为“等于”,现在该功能可以完美地与颜色配合使用。

Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Double
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
  If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.sum(cl, cSum)
  End If
Next cl
SumByColor = cSum
End Function