VBA : Math: Two functions to calculate a factorium (n!) 
Options: Save as PDF | Save attached file | Toggle line numbers
| Type: |
function |
| Added By: |
Rembo |
| Short Description: |
These are two functions to calculate a factorium. The first functions works with a loop, the second is a recursive function (calls itself). |
| Notes: |
It's easier to use a worksheet function in VBA (Application.WorksheetFunction...) but this is just a programming solution. |
| Added: |
Jul 15 2005 at 4:01 PM |
| Modified: |
Sep 22 2005 at 2:23 PM |
| Related URLs |
http://www.puremis.net/excel/cgi-bin/yabb/YaBB.pl?num=1121313698
|
Call the function as a User Defined Function (UDF) and supply a integer as
parameter. E.g. =Factorum(3) of Factorium2(3) (will give you answer '6')
Function Factorial(n As Integer) As Double
' Function with loop to calculate a factorial
Factorial = 1
For i = 1 To n
Factorial = Factorial * i
Next i
End Function
Function Factorial2(n As Integer) As Double
' Recursive function to calculate a factorial
If n < 1 Then
Factorial2 = 1
Else
Factorial2 = n * Factorial2(n - 1)
End If
End Function