Microsoft KB Archive/26395

{| = Excel: Creating a Factorial Function =
 * width="100%"|

Last reviewed: November 2, 1994

Article ID: Q26395

SUMMARY
To compute the factorial of a number in versions 1.50, 2.20, or 3.00 of Microsoft Excel, the FACT function can be used.

To create a function that will compute factorials in versions 1.06 and earlier of Excel, do the following:

  Enter the following commands into a macro sheet: +---+---+ | | A | +---+---+ | 1 | Factorial | +---+---+ | 2 | =RESULT(1) | +---+---+ | 3 | =ARGUMENT(&quot;number&quot;,1) | +---+---+ | 4 | =SET.NAME(&quot;factorial.result&quot;,1) | +---+---+ | 5 | =IF(NOT(number>1),RETURN(factorial.result)) | +---+---+ | 6 | =SET.NAME(&quot;factorial.result&quot;,factorial.result*number) | +---+---+ | 7 | =SET.NAME(&quot;number&quot;,number-1) | +---+---+ | 8 | =GOTO(A5) | +---+---+  Select cell A1. From the Formula menu, choose Define Name. Under Macro, select Function. Click OK.

To use the function macro in a worksheet, do the following:
 * 1) Make sure the macro sheet is open.
 * 2) Select the cell to contain the function.
 * 3) From the Formula menu, choose Paste Function.
 * 4) Scroll to the bottom of the list of functions.
 * 5) Select the desired function [which will be of the form MacroSheetName!Factorial] and click OK.
 * 6) Enter the number to be factored between the parentheses and press RETURN.
 * }