?
Solved

Running an Excel formula in Access VBA

Posted on 2006-06-16
10
Medium Priority
?
1,032 Views
Last Modified: 2008-02-20
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.

0
Comment
Question by:Bicman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 27

Expert Comment

by:jjafferr
ID: 16921198
Hi Bicman,

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

jaffer
0
 
LVL 2

Author Comment

by:Bicman
ID: 16921375
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.
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 16922602
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Accepted Solution

by:
puppydogbuddy earned 1600 total points
ID: 16924201
Bicman,
see these two links:
   
          http://support.microsoft.com/?kbid=198571

           http://www.fabalou.com/Access/Queries/excel_functions.asp

0
 
LVL 58

Assisted Solution

by:harfang
harfang earned 400 total points
ID: 16924741
Hello Bicman

If you don't mind to have a shadow instance of Excel running in memory, you can simply use these (from the immediate pane):

    ? Excel.WorksheetFunction.Cosh(1.5)
    ? Excel.WorksheetFunction.Fv(0.05/12,24,-800)
    ? Excel.WorksheetFunction.Median(Array(10,12,14,100),1,20)

To make sure the hidden instance is closed when you close your database, a cleaner way would be to create a function like this:

Function XL() As Excel.Application
    Static MyXL As Excel.Application
    If MyXL Is Nothing Then Set MyXL = New Excel.Application
    Set XL = MyXL
End Function

Which will create the instance the first time its called, and then return the same one. The difference is that the instance is terminated when you reset your VB project or close the database... You would then go:

   ? XL.WorksheetFunction.Sum(1,2,3,4)
   ? XL.WorksheetFunction.Min(1,Array(4,5,6),Array(0,12,5),123)
   ? XL.WorksheetFunction.ChiInv(0.8,8)

Note that the functions requiring  a Range object (like SumIf()) need, well, a range object to work. However, most functions accept both ranges and arrays, wich make them much more useful in Access.

Cheers!
(°v°)
0
 
LVL 58

Expert Comment

by:harfang
ID: 16924766
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°)
0
 
LVL 2

Author Comment

by:Bicman
ID: 16934074
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
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 16934233
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.
0
 
LVL 58

Expert Comment

by:harfang
ID: 16934764
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°)
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 16934801
Harfang,
Good info!  Thanks for that explanation.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question