我想用excel vba对一列的单元格求和。单元格中的值是小数,但是,excel vba给出的和始终是整数。例如: 2.5 +2.1 = 4.6 excel将显示5,如何显示4.6的值?
谢谢你的帮助。
发布于 2015-03-05 20:33:00
使用Double数据类型:
Sub SumTheValues()
Dim zum As Double
zum = Application.WorksheetFunction.Sum(Range("A:A"))
MsgBox zum
End Sub
对于您的数据:
发布于 2018-11-09 17:13:54
我是从另一个帖子里发现的。选项4成为最好的- https://www.exceltrick.com/how_to/sum-cells-based-on-background-color
然后我取了上面的内容,把一个单词-- "as long“替换为"as double”,现在这个函数可以完美地处理颜色了。
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
https://stackoverflow.com/questions/28886461
复制相似问题