#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default vba coding

im using the following code

Sub test()
Dim x As Single, n As Integer, i As Integer, fact As Integer
n = Range("b3")
x = Range("a3")
'calculate n factorial
'checking B3
If Range("B3") <= 0 Or Int(Range("B3")) < Range("B3") Then
MsgBox " integer in b3 must be greater then zero"
Exit Sub
End If
fact = 1
For i = 1 To n
fact = fact * 1
Next i
'calculate result x ^ n/fact n!
result = x ^ n / fact
Range("c3") = result

where n =5 and x = 2 but is running into cell c3 32, which is 2 to the
power 5 factorial

but i want to run 2 to the power 5 divided by n factorial, does anyone
no anything i can put into this vba code to make it return this.

also i want to add some error checking, does anyone know how to add
that to the vba code

thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default vba coding

Use the worksheetfunction from VBA. SeeCode below.

Sub test()
Dim x As Single, n As Integer, i As Integer, fact As Integer
n = Range("b3")
x = Range("a3")
'calculate n factorial
'checking B3
If Range("B3") <= 0 Or Int(Range("B3")) < Range("B3") Then
MsgBox " integer in b3 must be greater then zero"
Exit Sub
End If

'calculate result x ^ n/fact n!
result = x ^ n / WorksheetFunction.fact(n)
Range("c3") = result

End Sub


"paul" wrote:

im using the following code

Sub test()
Dim x As Single, n As Integer, i As Integer, fact As Integer
n = Range("b3")
x = Range("a3")
'calculate n factorial
'checking B3
If Range("B3") <= 0 Or Int(Range("B3")) < Range("B3") Then
MsgBox " integer in b3 must be greater then zero"
Exit Sub
End If
fact = 1
For i = 1 To n
fact = fact * 1
Next i
'calculate result x ^ n/fact n!
result = x ^ n / fact
Range("c3") = result

where n =5 and x = 2 but is running into cell c3 32, which is 2 to the
power 5 factorial

but i want to run 2 to the power 5 divided by n factorial, does anyone
no anything i can put into this vba code to make it return this.

also i want to add some error checking, does anyone know how to add
that to the vba code

thanks


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,646
Default vba coding

Change
fact = fact * 1

to
fact = fact * i


Regards,
Stefi

€˛paul€¯ ezt Ć*rta:

im using the following code

Sub test()
Dim x As Single, n As Integer, i As Integer, fact As Integer
n = Range("b3")
x = Range("a3")
'calculate n factorial
'checking B3
If Range("B3") <= 0 Or Int(Range("B3")) < Range("B3") Then
MsgBox " integer in b3 must be greater then zero"
Exit Sub
End If
fact = 1
For i = 1 To n
fact = fact * 1
Next i
'calculate result x ^ n/fact n!
result = x ^ n / fact
Range("c3") = result

where n =5 and x = 2 but is running into cell c3 32, which is 2 to the
power 5 factorial

but i want to run 2 to the power 5 divided by n factorial, does anyone
no anything i can put into this vba code to make it return this.

also i want to add some error checking, does anyone know how to add
that to the vba code

thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default vba coding

Paul
Sub seenthisbefore()
Dim x As Single, n As Integer, i As Integer, fact As Integer
n = Range("b3")
x = Range("a3")
'calculate n factorial
'checking B3
If Range("B3") <= 0 Or Int(Range("B3")) < Range("B3") Then
MsgBox " integer in b3 must be greater then zero"
Exit Sub
End If
factorial = Application.WorksheetFunction.fact(n)
'calculate result x ^ n/fact n!
result = x ^ n / factorial
Range("c3") = result

End Sub

Mike


"paul" wrote:

im using the following code

Sub test()
Dim x As Single, n As Integer, i As Integer, fact As Integer
n = Range("b3")
x = Range("a3")
'calculate n factorial
'checking B3
If Range("B3") <= 0 Or Int(Range("B3")) < Range("B3") Then
MsgBox " integer in b3 must be greater then zero"
Exit Sub
End If
fact = 1
For i = 1 To n
fact = fact * 1
Next i
'calculate result x ^ n/fact n!
result = x ^ n / fact
Range("c3") = result

where n =5 and x = 2 but is running into cell c3 32, which is 2 to the
power 5 factorial

but i want to run 2 to the power 5 divided by n factorial, does anyone
no anything i can put into this vba code to make it return this.

also i want to add some error checking, does anyone know how to add
that to the vba code

thanks


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with coding anna New Users to Excel 4 April 24th 08 05:06 AM
"=ROW()-1" type of coding doesn't appear in a filter / is there coding that does? StargateFan[_3_] Excel Programming 10 October 6th 05 01:18 PM
Implant macro coding into ASP coding Sam yong Excel Programming 5 September 15th 05 10:37 AM
Need coding help. Neddy[_4_] Excel Programming 6 June 17th 05 12:21 PM
VBA Coding (?) Larry G. Excel Programming 1 May 18th 05 05:48 PM


All times are GMT +1. The time now is 07:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"