Solved

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

Posted on 2015-02-19
5
5,563 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 52

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 52

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

630 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