0

I am trying to achieve the Ramanujan Function by using VBA. The formula is in the picture below.

enter image description here

My code is:

Function ramanuian(n)

    left_part = (Application.sqrt(8) / 9801)
    
    Dim temp As Double
    
    temp = 0
    
    For i = 0 To n
    
        middle_part = Application.Fact(4 * i) / Application.Power(Application.Fact(i), 4)
        right_part = (1103 + 26930 * i) / Application.Power(396, 4 * i)
        ramanuian_reciprocal = middle_part * right_part
        temp = temp + ramanuian_reciprocal
        
    Next
    
    ramanuian = 1 / (left_part * temp)

End Function

However, when I run this formula in Excel, it shows me an #Value! error. What is wrong with my code?

Stefan Gruenwald
  • 2,582
  • 24
  • 30
LightOwlGG
  • 139
  • 10
  • 1
    Call your function from a VBA Sub, or from the Immediate pane in the VB editor – Tim Williams Nov 18 '19 at 20:19
  • 3
    `left_part = (8 ^ 0.5) / 9801` `ApplicationSqrt()` is not valid VBA – Tim Williams Nov 18 '19 at 20:22
  • 2
    @TimWilliams wow nice catch! `=SQRT(25)` works, but there's no corresponding `WorksheetFunction`. Of course VBA has its own square-root function, so `Math.Sqr(8)/9801` would do. – Mathieu Guindon Nov 18 '19 at 20:25
  • Thanks, guys. You are right, I found that Application.Fact() and Sqrt() are both not valid. Therefore, I define a new function manually. And it works right now. – LightOwlGG Nov 18 '19 at 20:47

0 Answers0