UDF总和不返回小数位,只有一个整数

我有一个UDF,它总结了上面的单元格,直到它达到格式为百分比的单元格。 总和公式起作用,我可以有一个返回的数字。 …但是,似乎是四舍五入/截断了答案。

码:

Function sumAbove(cel As Range) As Variant Dim firstCell As Integer Dim i As Integer i = 1000 With cel For i = 1 To 1000 If cel.Offset(-i, 0).NumberFormat = "0.00%" Then firstCell = cel.Offset(-i, 0).Row Exit For End If Next i End With sumAbove = WorksheetFunction.Sum(Range(Cells(firstCell, cel.Column), Cells(cel.Row, cel.Column))) End Function 

我已经尝试Function sumAbove(cel as Range)作为IntegerDouble ,没有结果。 当我尝试将其设置为Float ,出现“用户定义的types未定义”错误。

有了这个清单:

 25.00% 5 5 5 5 5.5 

上面的代码将正确使用单元格5 + 5 + 5 + 5 + 5.5 – 但是,它不正确地获得总和。 如果我将UDF设置为一个整数,它只是计算为25.如果我将UDF更改为Double,则返回25.75(??)等。

我如何得到它返回25.50? 感谢您的任何提示或build议!

 firstCell = cel.Offset(-i, 0).Row 

这是包括你的SUM函数的百分比。
(25%= 0.25这就是为什么你得到.75)

使用类似这样的东西:

 Function SumAbove(cell As Range) As Double Dim firstCell As Long Dim i As Integer firstCell = cell.Row While Not cell.Offset((cell.Row - firstCell) * -1, 0).NumberFormat = "0.00%" firstCell = firstCell - 1 Wend SumAbove = WorksheetFunction.Sum(Range(Cells(firstCell + 1, cell.Column), cell)) End Function