Using a Worksheet Function in a Visual Basic Macro

This article was previously published under Q161120
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Excel 2002 version of this article, see 291309.
For a Microsoft Excel 2000 version of this article, see 213765.
You can call a built-in Microsoft Excel worksheet function directly from aMicrosoft Visual Basic for Applications macro. This article describes howto use a built-in worksheet function in a macro.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. You can use a built-in worksheet function in a macro by calling thefunction as a method of the Application object (or the WorksheetFunctionobject in Microsoft Excel 97 or Microsoft Excel 98). For example, tosuccessfully call the ACOS worksheet function in a version of MicrosoftExcel listed at the beginning of this article, you can use the followingline of code in a macro:
    X = Application.Acos (-1)				
If you are using Microsoft Excel 97 or Microsoft Excel 98, use thefollowing line of code:
    X = WorksheetFunction.Acos (-1)				
NOTE: If you attempt to use a built-in worksheet function withoutqualifying it with the Application or WorksheetFunction object, you mayreceive the following error message:
Sub or Function Not Defined
You are not able to use all built-in worksheet functions with theApplication or WorksheetFunction object. Visual Basic for Applicationsprovides many functions that are equivalent to the built-in worksheetfunctions in Microsoft Excel. You cannot use a built-in worksheet functionsfor which there is an equivalent in Visual Basic with the Application orWorksheetFunction object.
For additional information about the worksheet functions that are notsupported with the Application or WorksheetFunction objects, please see thefollowing article in the Microsoft Knowledge Base:
107564 XL: Not All Worksheet Functions Supported as Application

Microsoft Excel 97

For more information about using Microsoft Excel Worksheet Functions inVisual Basic, click the Office Assistant in the Visual Basic Editor, typeusing built-in functions in a macro, click Search, and then click to view"Using Microsoft Excel Worksheet Functions in Visual Basic."

Microsoft Excel 7.0

For a complete list of the worksheet functions that you can call with theApplication object, type the following text
worksheet functions, using
and then double-click the selected text to go to the "List of WorksheetFunctions Available to Visual Basic" topic.

Microsoft Excel 5.0

For a complete list of the worksheet functions that can be called with theApplication object, choose the Search button in Visual Basic Help, andtype:
worksheet functions
Choose the Show Topics button, select the topic "Using Worksheet Functionsin Visual Basic", and choose Go To.
XL98 XL97 XL7 XL5 7.00a 5.00c 5.00a XL

Article ID: 161120 - Last Review: 12/04/2015 15:56:03 - Revision: 2.1

Microsoft Excel 97 Standard Edition, Microsoft Excel 95 Standard Edition, Microsoft Excel 5.0 Standard Edition, Microsoft Excel 98 for Macintosh

  • kbnosurvey kbarchive kbdtacode kbhowto kbprogramming KB161120