Table of Contents
A factorial function in VBA is a user-defined function that calculates the factorial of a given number. To create this function, first define the function and its parameters, then use a loop to multiply the number by its preceding numbers until it reaches 1. An example of a factorial function in VBA is as follows:
Function factorial(num As Integer) As Integer
Dim result As Integer
result = 1
For i = 1 To num
result = result * i
Next i
factorial = result
End Function
This function takes in a number as an argument and returns the factorial of that number. For example, if we input 5, the function will calculate 5 x 4 x 3 x 2 x 1 = 120 and return the value 120. This function can be used in various VBA programs to calculate factorials and perform other mathematical operations.
Create a Factorial Function in VBA (With Example)
A factorial is the product of all positive integers less than or equal to a given positive integer.
For example, 5 factorial (written as 5!) is calculated as:
- 5! = 5 * 4 * 3 * 2 * 1 = 120
You can use the following syntax to create a factorial function in VBA:
Function FindFactorial(N As Integer) As Double
Dim i As Integer, result As Long
result = 1
For i = 1 To N
result = result * i
Next
FindFactorial = result
End Function
Once you’ve created this function, you can then type something like =FindFactorial(A2) in a cell in Excel to find the factorial of the integer in cell A2.
The following example shows how to use this syntax in practice.
Example: Create a Factorial Function in VBA
Suppose we have the following list of numbers in Excel and we’d like to calculate the factorial of each number:

We can define the following function in VBA to do so:
Function FindFactorial(N As Integer) As Double
Dim i As Integer, result As Long
result = 1
For i = 1 To N
result = result * i
Next
FindFactorial = result
End FunctionOnce we’ve created this function, we can then type the following formula into cell B2 to calculate the factorial of the value in cell A2:
=FindFactorial(A2)We can then click and drag this formula down to each remaining cell in column B:

Notice that column B now displays the factorial of each integer in column A.
- 1! = 1
- 2! = 2 * 1 = 2
- 3! = 3 * 2 * 1 = 6
- 4! = 4 * 3 * 2 * 1 = 24
And so on.
Note: To calculate a factorial in Excel without using VBA, you can use the FACT function.
Cite this article
stats writer (2024). How can I create a factorial function in VBA with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-factorial-function-in-vba-with-an-example/
stats writer. "How can I create a factorial function in VBA with an example?." PSYCHOLOGICAL SCALES, 21 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-factorial-function-in-vba-with-an-example/.
stats writer. "How can I create a factorial function in VBA with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-factorial-function-in-vba-with-an-example/.
stats writer (2024) 'How can I create a factorial function in VBA with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-factorial-function-in-vba-with-an-example/.
[1] stats writer, "How can I create a factorial function in VBA with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I create a factorial function in VBA with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
