Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

vba coding

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
paul - 18 Sep 2007 10:50 GMT
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
Joel - 18 Sep 2007 11:14 GMT
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

> im using the following code
>
[quoted text clipped - 26 lines]
>
> thanks
Stefi - 18 Sep 2007 11:14 GMT
Change
> fact = fact * 1
to
> fact = fact * i

Regards,
Stefi

„paul” ezt írta:

> im using the following code
>
[quoted text clipped - 26 lines]
>
> thanks
Mike H - 18 Sep 2007 11:18 GMT
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

> im using the following code
>
[quoted text clipped - 26 lines]
>
> thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.