XL: Using a Worksheet Function in a Visual Basic Macro

ID: Q161120

The information in this article applies to:

SUMMARY

You can call a built-in Microsoft Excel worksheet function directly from a Microsoft Visual Basic for Applications macro. This article describes how to use a built-in worksheet function in a macro.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

You can use a built-in worksheet function in a macro by calling the function as a method of the Application object (or the WorksheetFunction object in Microsoft Excel 97 or Microsoft Excel 98). For example, to successfully call the ACOS worksheet function in a version of Microsoft Excel listed at the beginning of this article, you can use the following line of code in a macro:

    X = Application.Acos (-1)

If you are using Microsoft Excel 97 or Microsoft Excel 98, use the following line of code:

    X = WorksheetFunction.Acos (-1)

NOTE: If you attempt to use a built-in worksheet function without qualifying it with the Application or WorksheetFunction object, you may receive the following error message:

    Sub or Function Not Defined

You are not able to use all built-in worksheet functions with the Application or WorksheetFunction object. Visual Basic for Applications provides many functions that are equivalent to the built-in worksheet functions in Microsoft Excel. You cannot use a built-in worksheet functions for which there is an equivalent in Visual Basic with the Application or WorksheetFunction object.

REFERENCES

For additional information about the worksheet functions that are not supported with the Application or WorksheetFunction objects, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q107564
   TITLE     : XL: Not All Worksheet Functions Supported as Application

Microsoft Excel 97

For more information about using Microsoft Excel Worksheet Functions in Visual Basic, click the Office Assistant in the Visual Basic Editor, type "using 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 the Application object, type the following text

   worksheet functions, using

and then double-click the selected text to go to the "List of Worksheet Functions Available to Visual Basic" topic.

Microsoft Excel 5.0

For a complete list of the worksheet functions that can be called with the Application object, choose the Search button in Visual Basic Help, and type:

   worksheet functions

Choose the Show Topics button, select the topic "Using Worksheet Functions in Visual Basic", and choose Go To.

Additional query words: XL98 XL97 XL7 XL5 7.00 7.00a 5.00c 5.00 5.00a

Keywords          : kbprg kbdta kbdtacode PgmOthr KbVBA 
Version           : WINDOWS:5.0,5.0c,7.0,7.0a,97; MACINTOSH:5.0,98
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: May 18, 1999