Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Baltimore, MD, Maryland, USA
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Creating and calling functions (Office 2000)

    Hi Again:
    I created this function by copying the code from EXCEL VBA Help.
    Function Factorial(N)
    If N <= 1 Then ' Reached end of recursive calls.
    Factorial = 1 ' (N = 0) so climb back out of calls.
    Else ' Call Factorial again if N > 0.
    Factorial = Factorial(N - 1) * N
    End If
    End Function

    Then, I tried to call it and place the answer in a MsgBox.

    Sub callfactorial()
    Dim K As Integer
    Dim Answer As Integer
    K = Worksheets("MkFunction").Range("D1")
    Answer = Factorial(K)
    MsgBox ("This is the answer" & Answer)
    End Sub

    I have the number 10 entered in D1 and I run the callfactorial() sub and I get runtime error 6 and the debug highlights "Answer = Factorial(K)". I need another little push up the Learning Curve again.
    Anyone?

  2. #2
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating and calling functions (Office 2000)

    well your code looks a little strange, but perfectly okay, if you understand what I'm saying. However, I changed both the variable declarations to dim ... as double and it works fine. Better minds than mine will have to tell you why that is.

  3. #3
    Lounger
    Join Date
    Aug 2001
    Location
    Shropshire, UK
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating and calling functions (Office 2000)

    10 factorial is (according to windows calculator) 3,628,800 but the INTEGER data type is for values -32,768 to 32,767. Hence runtime error 6, which is overflow. See help on data types and trappable errors.
    Ewan

  4. #4
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Creating and calling functions (Office 2000)

    <img src=/S/doh.gif border=0 alt=doh width=15 height=15> thank you ewan - that was beginning to bug me. I just couldn't figure it. Guess it's time I went to bed.

Posting Permissions

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