Solved

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

Posted on 2015-02-19
5
3,385 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
  • 2
  • 2
5 Comments
 
LVL 49

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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now