ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   how to code factorial in Excel (https://www.excelbanter.com/excel-programming/302489-how-code-factorial-excel.html)

[email protected]

how to code factorial in Excel
 
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.

Leo Heuser[_3_]

how to code factorial in Excel
 
This syntax works for me:

Application.WorksheetFunction.Fact(5)

--
Best Regards
Leo Heuser

Followup to newsgroup only please.

skrev i en meddelelse
m...
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.




Marcotte A

how to code factorial in Excel
 
" wrote:

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


All times are GMT +1. The time now is 11:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com