how to code factorial in Excel

H

houstoncity2004

I am trying to code the factorial function in VBA in Excel.
I know I can use the FACT() in the worksheet, but somehow I can't use
the same function when I am writing the VBA. It gave error and said
"sub or function not defined". I did macro recording and copy the
code to my sub-function and try to run it, it gave error again. Can
someone help? Thanks.
 
G

Guest

I am trying to code the factorial function in VBA in Excel.
I know I can use the FACT() in the worksheet, but somehow I can't use
the same function when I am writing the VBA. It gave error and said
"sub or function not defined". I did macro recording and copy the
code to my sub-function and try to run it, it gave error again. Can
someone help? Thanks.

If Leo's answer doesn't solve your problem, try this. Make sure you put in in a code module (as opposed to a sheet module).

Function Factorial(Num as integer)
Dim i as integer, answer as double
answer = 1
For i = 1 to Num
answer = answer * i
Next i
Factorial = answer
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

largest factorial that can be computed 3
Factorial Function 5
vba coding 3
VBA Factorial Help Please :) 0
Factorial function...whaaaaaa? 6
index function in vba 3
programme run help 2
Factorial question 5

Top