【问题标题】:sum of decimals in excel vbaexcel vba中的小数总和
【发布时间】:2018-11-10 01:27:19
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

    标签: excel integer decimal vba


    【解决方案1】:

    使用 Double 数据类型:

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

    对于您的数据:

    【讨论】:

      【解决方案2】:

      我从另一个帖子中找到的。选项 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多