Link to home
Start Free TrialLog in
Avatar of Bicman
BicmanFlag for United States of America

asked on

Running an Excel formula in Access VBA

I am trying to run an Excel statistical formula in Access using VBA.  Help in VBA says you can do this, but they do not give the syntax for running a formula.  I already have the formula working properly in Excel, so I know how to feed the formula, just not how to run an Excel formula in VBA.  Any thoughts?

Yes, I am referencing the Excel Object Library.

Avatar of jjafferr
jjafferr
Flag of Oman image

Hi Bicman,

If you show us the Formula, then we can help you adopt it in Access VBA

jaffer
Avatar of Bicman

ASKER

You are getting distracted by the formula.  I am talking about all of them.  For example, type "Median" in your code area.  Press F1.  You get "Using Microsoft Excel Worksheet Functions in Visual Basic".  Fine, but "MyVar = Application.WorksheetFunction.Median (etc.)" gets an error about WorksheetFunction.  That's the part I need: the code to run the formula.

Do I have to declare applications, worksheets, etc.?  I have been trying that, but NoJoy so far.
You can only use the Excel functions in relation to an Excel worksheet. The following is copied from the Visual Basic Help for the WorksheetFunction Object:

Used as a container for Microsoft Excel worksheet functions that can be called from Visual Basic.

Using the WorksheetFunction Object

Use the WorksheetFunction property to return the WorksheetFunction object. The following example displays the result of applying the Min worksheet function to the range A1:A10.

Set myRange = Worksheets("Sheet1").Range("A1:C10")
answer = Application.WorksheetFunction.Min(myRange)
MsgBox answer
ASKER CERTIFIED SOLUTION
Avatar of puppydogbuddy
puppydogbuddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologies, PDB. I has skipped over your links too quickly. They give the explicit difference of calling the Excel library directly vs using an object variable... So the only original content is the Array vs Range tip.
(°v°)
Avatar of Bicman

ASKER

I am going to give you (and Harfang) points for your answer because it appears to be the correct one.  I even get a list of worksheet functions when I type a period after the word “worksheetfunction”.  Sadly, the one I select, NormSInv, does not work.  Instead, I get an error: “Unable to get the NormSInv property of the WorksheetFunction class”.

Since other functions, like Average and Median do work, I know you have answered the question I asked.  And so it goes.  Thanks for your help, Bicman
Avatar of puppydogbuddy
puppydogbuddy

Thanks for the points and grade.  Don't understand why your function did not work....did you assign correct data type?,etc. as shown in the Fabalou web link I gave you.
Bicman

The following works for me in the immediate pane:

    ? Excel.WorksheetFunction.NormSInv(0.99)
     2.32634192798287

However, I do get your error message when the argument is invalid (<= 0 or >=1). The function expects a probability (a number between 0 and 1) and overflows if it's too close to 0 or 1 (the answer would be -infinity or +infinity). The meaningful range seems to be 0.000001—0.999999.

I hope it works for you as well. It seems to be a bug in the error message, not in the function itself.

(°v°)
Harfang,
Good info!  Thanks for that explanation.