简体   繁体   English

MS Excel。 VBA函数返回#value

[英]MS Excel. VBA function returns #value

It would be nice if someone could explain what causes function above return #value error. 如果有人可以解释是什么原因导致函数返回#value错误,那就太好了。

Public Function papild(x)
    Dim Sum As Double, A As Double, pi As Double,
    Sum = 0.5 - (x - pi / 4)
    A = -(x - pi / 4)
    pi = Application.WorksheetFunction.pi()
    Dim k As Integer, i As Integer
    k = 2
    i = 0
    Do While Abs(A) > 0.0001
        A = -A * 4 * A * A / (k + i) * (k + i + 1)
        Sum = Sum + A
        k = k + 1
        i = i + 1
    Loop
    paplid = Sum
End Function

Function takes x value from MS Excel cell and it's equal = -1.5708 (=-PI()/2 @Formula Bar) 函数从MS Excel单元格获取x值,它等于= -1.5708(= -PI()/ 2 @Formula Bar)

In lines 3 and 4 you work with variable pi before setting it in line 5... 在第3行和第4行中,在将变量pi设置在第5行中之前使用变量pi ...

Could there be some brackets missing in your formula. 您的公式中是否可能缺少括号。 It basically says: 它基本上说:

A = -4A^3 * (k+i+1)/(k+1)

This obviously drifts to +/- infinite so your loop cannot end. 这显然漂移到+/-无限,因此您的循环无法结束。

Also there is a comma too much in the second line and a spelling error in the last line (paplid instead of papild). 在第二行中也有一个逗号,在最后一行中有一个拼写错误(拼写而不是papild)。

Have you tried debugging the code? 您是否尝试过调试代码? When I run the code I get an overflow error @ the 6th iteration of the while loop starting with x = -1.5708. 当我运行代码时,我在@ x = -1.5708开始的while循环的第6次迭代中遇到溢出错误。 Number gets to large to fit inside variable .Other than that there are some minor things: missing As Double 数字变大以适合变量内部。除此之外,还有一些小问题:缺少为Double

Public Function papild(x) As Double

and unnecessary comma at the end 最后加上不必要的逗号

Dim Sum As Double, A As Double, pi As Double,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM