+ Reply to Thread
Results 1 to 3 of 3

Factorial Function

Hybrid View

Microsoft Excel Factorial Function 06-15-2013, 12:42 PM
patel45 Re: Factorial Function 06-15-2013, 01:14 PM
mikerickson Re: Factorial Function 06-15-2013, 02:00 PM
  1. #1
    Registered User
    Join Date
    05-26-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    10

    Factorial Function

    Hello,

    I am creating a factorial function in VBA Excel for a calculator button. I am not allowed to use built in functions. The code that I am currently using is as follows:
    If Range("B2") = "!" Then
        Number_1 = Range("D3")
            For Count = 1 To Number_1
            Number_2 = Number_2 + 1
            Number_3 = Number_2 * (Number_2 + 1)
            Next Count
        Sheet1.Range("B3") = Number_3
    End If
    I am not quite sure why it will not work. I am very new to VBA. Please reply as soon as possible if you have any solutions to my problem.

    Thank you so much in advance!

    ~ Microsoft Excel ~
    Last edited by Leith Ross; 06-15-2013 at 01:37 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Factorial Function

    Sub fact()
    Dim n As Integer, factorial As Integer
    factorial = 1
    n = Range("D3")
    Do While n > 1
      factorial = factorial * n
      n = n - 1
    Loop
    Sheet1.Range("B3") = n
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Factorial Function

    I like recursion
    Sub test()
        With Sheet1.Range("B3")
            .Value = myFactorial(Int(Val(CStr(.Offset(0,2).Value))))
        End With
    End Sub
    
    Function myFactorial(n As Long) As Long
        If n<=1 Then
            myFactorial = 1
        Else
            myFactorial = n * myFactorial(n - 1)
        End If
    End Function
    Last edited by mikerickson; 06-15-2013 at 02:03 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1