Solved

How to call Excel's function (mean, standard deviation) in VBA?

Posted on 2015-02-19
5
5,143 Views
Last Modified: 2016-02-10
Hi,

I know how to write a function in VBA to calculate mean. But since I know Excel has this function already so I don't want to write it myself. Besides, using the Excel inbedded function maybe faster in calculation. So I want to call the function in VBA. How to do that?

Thanks for any help?

Jennifer
0
Comment
Question by:jfz2004
[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
  • 2
  • 2
5 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40618798
HI,

pls try
a = array(3,4)
Res = Application.WorksheetFunction.Average(c)
Res = Application.WorksheetFunction.StDev(c)

' Shorter
Res = WorksheetFunction.Average(c)
Res = WorksheetFunction.StDev(c)

Open in new window

Regards
0
 

Author Comment

by:jfz2004
ID: 40618799
I will try. Thanks. but what is c in the functions?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40618800
Where VBA doesn't have an equivalent function built in, you can use either Application.WorksheetFunction.function_name or simply Application.function_name - for example:
Application.WorksheetFunction.Average(Range("A1:A10"))

Open in new window

or:
Application.Average(Range("A1:A10"))

Open in new window


The worksheetfunction version is typically a little faster if you are passing ranges rather than arrays to it, and the error handling is different. Application.WorksheetFunction.function_name raises a run-time error if it fails, which you have to handle with an On Error statement, whereas Application.function_name returns an error value that you can assign to a Variant variable and test using IsError(variable_name).
0
 

Author Comment

by:jfz2004
ID: 40618875
Let me try it with a brand new worksheet. I first try failed with this error:
sub or function not defined.
0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40618887
Sorry replace c with a

a = array(3,4)
Res = Application.WorksheetFunction.Average(a)
Res = Application.WorksheetFunction.StDev(a)

' Shorter
Res = WorksheetFunction.Average(a)
Res = WorksheetFunction.StDev(a)
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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