Excel - Factorial function...whaaaaaa?

Asked By Geoff
27-Jan-10 01:54 AM
I have the following equation in my code:

TotalCalc = Fact(n + 4) / (Fact(4) * Fact(n))

When I compile, it highlights the first Fact and tells me "Sub or Function
not defined".

TIA
WorksheetFunction
(1)
Excel
(1)
WorksheetFunction.Combin
(1)
Worksheet
(1)
FACT
(1)
VBA
(1)
VB
(1)
WorksheetFunctionTotalCalc
(1)
  Jacob Skaria replied to Geoff
27-Jan-10 01:59 AM
FACT() is a worksheetfunction. So you can try something like the below

TotalCalc = WorksheetFunction.Fact(n + 4) / (WorksheetFunction.Fact(4) * _
WorksheetFunction.Fact(n))

--
Jacob
  Rick Rothstein replied to Jacob Skaria
27-Jan-10 02:36 AM
Or, to lessen the amount of typing...

With WorksheetFunction
TotalCalc = .Fact(n + 4) / (.Fact(4) * .Fact(n))
End With

To the OP... be sure to note the "dot" in front of each of the Fact function
names.

--
Rick (MVP - Excel)
  Joe User replied to Geoff
27-Jan-10 03:10 AM
For n>166, Fact(n+4) exceeds the computation limits of the Double data type.

And for n>14, Fact(n+4) exceeds 2^53-1, the largest integer that can be
represented exactly with the Double data type.  So the result of your
expression is infinitesimally different from the correct result, which can
lead to anomalies in some expressions.

It would behoove you to reduce the formula algebraically, namely:

TotalCalc = (n+4)*(n+3)*(n+2)*(n+1) / 24

Not only is the result more accurate for a wider range of n, but also it is
more efficient.

Even if 4 in Fact(n+4) and Fact(4) is actually variable, I think an
algorithm that reduces the formula algebraically would be more reliable,
albeit perhaps less efficient.


----- original message -----
  Dana DeLouis replied to Geoff
27-Jan-10 12:58 PM
Another option might be:

TotalCalc = WorksheetFunction.Combin(n+4,4)

= = =
HTH  :>)
Dana DeLouis
  Chip Pearson replied to Geoff
27-Jan-10 04:07 PM
As others have indicated, you can use Excel's built in FACT worksheet
function. However, you can roll your own with a recursive function in
VBA.   Recursive functions are those function that call themselves. In
the code below, the Fact function calls itself decrementing the input
value until it is 1.

See http://www.cpearson.com/Excel/RecursiveProgramming.aspx for an
introduction to recursion.

Function Fact(N As Long) As Long
If N = 1 Then
Fact = 1
Else
Fact = N * Fact(N - 1)
End If
End Function

Usage:

Sub AA()
Dim L As Long
L = Fact(6)
Debug.Print CStr(L) ' displays 120
End Sub

Then your original code will work with no modification.



Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
  Rick Rothstein replied to Chip Pearson
27-Jan-10 06:06 PM
Okay, as long as you posted a recursive Factorial function in VB, I figured
others might find this non-recursive function of some interest. The
following function will calculate factorials up to 29 accurate digits of
display before reverting to exponential display (but note the caution after
the function code)...

Function BigFactorial(N As Long) As Variant
Dim X As Long
If N < 28 Then
BigFactorial = CDec(1)
Else
BigFactorial = CDbl(1)
End If
For X = 1 To N
BigFactorial = X * BigFactorial
Next
End Function

However, you have to watch out for overflows with Decimal data types (that
is what the CDec function produces) when used in calculations... once an
expression using a Decimal data type calculates to more than 28/29 digits
(depending of if it contains a decimal point or not), it will produce an
overflow error. So, if you tried to use the above function like this

MsgBox 10 * BigFactorial(27)

you would get an overflow error but

MsgBox 10 * BigFactorial(28)

would work fine (the difference being in the first case BigFactorial returns
a Variant with a Decimal subtype whereas in the second case the Variant's
subtype is a Double).

--
Rick (MVP - Excel)
Create New Account
help
Excel Win7 VirtualStore Weirdness with Excel 2002 and earlier I thought I'd share a weird experience I had today in installer for it - - which registers it as an add-in for all installed versions of Excel - - and tested it. Fine in Windows 2000 in all installed versions of Excel. Boot to Windows XP, fine there too. Boot to Windows 7. Fine in Excel 2010, 2007, and 2003. But Excel 2002 and 2000 were still loading the old version of the add-in. I uninstalled the add-in, made sure it was gone, and reinstalled it. Same problem in Excel 2002 and earlier; still works fine in Excel 2003 and later. I opened the VB editor in Excel 2003 and in Excel 2002
charset = "iso-8859-1" Content-Transfer-Encoding: quoted-printable I Googled for hours looking for Excel save as and activesync problems = before I posted here. The file on the PC is directly regarding issues. Cris, I just noticed that the Save As on the phone is Excel 97 / 2000 and = not even a 2003 version. On my workstation, I saved it as "Microsoft = Excel 97- Excel 2003 & 5.0 / 95 Workbook" before I synced to the phone. I guess I answered your own risk.* * Gregg I have a WM6 device as well, Just created a new Excel Spreadsheet, = File> Save As Only option is Excel 97-2003 format Not real sure what the issue might be - - = 20 Cris Hanna [SBS it and ActiveSync works great for Exchange = stuff, = 20 but I have a problem with Excel files. I have a spreadsheet for my = mileage = 20 that is in Excel 2003 format. I used to use it with my ancient = Palm m130 and = 20 Dataviz
Excel How to strore exact double from macro into Excel cell? I know this is "stoopid" because I am sure I have done this before I make it work using a macro, other than storing a formula into the cell? - -- -- Excel keeps track of 15 significant digits when you treat the entry as a number. VBA a2") End Function Sub doit2() Range("a4") = Range("a2") End Sub Enter the following in Excel: A1: 0.28 A2: = 0.28 + 2^-54 A3: = doit1() A4: (to be filled in by macro doit2) Execute the doit2() macro, then enter the following in Excel: B1: = A1-A2 = 0 (FALSE) B2: = A3-A4 = 0 (FALSE!) B3: = A2-A3 = 0 (TRUE different as intended, even though we cannot see that when formatting to 15 decimal places, Excel's conversion limit. The FALSE value of B2 demonstrates that function doit1 returns a different am using the latter only for this example to rule out discussions of differences between Excel and VBA, I hope.) Ostensibly, this has nothing to do with Excel's 15-significant-digit conversion limit. Unless. . . . Are you confirming my WAG, to wit: in
Excel Very Slow reading excel data into an array (while opened in new xl I have a VBA application that reads in many excel files (sometimes in the 100's), one at a time, scans them for key info 40k), the macro started coming to a crawl and I noticed in Task Manager that Excel was using more and more memory (pushing 100MB). Excel was not releasing the files out of VBA Project. In a post from Tom Ogilvy of the memory issue but it dramatically increased the time it took to import the excel data worksheet into an array I could use. In the original code, I read the excel file into an array using this: arrMaster(r, c - LeftIndent) = ActiveSheet.Cells(r, c) I