#值!用户编写的时间加权平均函数返回的错误

时间:2019-01-16 14:07:00

标签: excel vba

我试图在VBA中编写一个计算时间加权平均值的函数,但是当我尝试在工作表中使用该函数时出现#VALUE!错误。

在尝试调试时,我可以通过迭代CurRPM并使用CurOffset来验证更改CurRng = rng.Offset(CurOffset, 0)的方法。更改CurRng的作用是迭代RPM列,检查CurRng下的单元格是否为空,与CurRPM相同或为非空且与{{1 }}。

CurRPM

如果用户为以下单元格输入Public Function TIMEAVERAGE(rng As Range) As Long 'Initializing Variables Dim CurTime As Integer Dim CurRPM As Integer Dim CurSum As Integer Dim CurOffset As Integer CurOffset = 0 CurSum = 0 'The input cell is the first RPM 'The cell to the left of the input cell is the second RPM CurTime = rng.Offset(0, -1) CurRPM = rng 'Keep calculating as long as the cell below CurRng isn't empty While IsEmpty(CurRng.Offset(1, 0)) = False 'If the cell below has the same RPM, look at next cell If CurRng.Offset(1, 0) = CurRPM Then CurOffset = CurOffset + 1 CurRng = rng.Offset(CurOffset, 0) Else: 'Otherwise, add the previous RPM's contribution to the average 'CurTime, as shown, is the start of the previous RPM CurSum = CurSum + CurRPM * (CurRng.Offset(0, -1) - CurTime) 'Change CurRPM and CurTime for the new RPM CurRPM = CurRng.Offset(1, 0) CurTime = CurRng.Offset(0, -1) CurOffset = CurOffset + 1 CurRng = rng.Offset(CurOffset, 0) End If Wend 'Add the contributions of the final RPM CurSum = CurSum + CurRPM * (CurRng.Offset(0, -1) - CurTime) 'Return the final TIMEAVERAGE TIMEAVERAGE = CurSum / CurRng.Offset(0, -1) End Function ,则此程序的期望结果将返回150。而是返回=TIMEAVERAGE(B2)错误。

#VALUE!

0 个答案:

没有答案