The information in this article applies to:
SUMMARYIn Microsoft Excel, you can create custom Microsoft Visual Basic for Applications functions to perform calculations. By default, these custom functions are available to all worksheets in the workbook that contains the custom function. Therefore, you can call them just as you call built-in functions in Microsoft Excel. However, if you attempt to call a custom function from outside of the workbook that contains the code, you must first make a reference to the workbook that contains the custom function. MORE INFORMATIONMicrosoft 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 a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/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/overview/overview.aspThe following steps show you how to create a simple custom function, how to call the function from the workbook that contains the code, and how to call this custom function from another workbook. To Create a Custom Function
Excel 98 Macintosh EditionExcel 97 for WindowsExcel versions 5.0, 7.0 for Windows, version 5.0 for Macintosh
To Call the Function from the Workbook That Contains the Function CodeTo call this function from within Test.xls, type the following formula in cell A1 on Sheet1:=myfunction(3)Cell A1 should display the value 6, because the custom function doubled the value that was sent to it. To Call the Function from Another WorkbookIn order to call the function from a workbook, you must create a reference to the workbook that contains the code. To create a reference, follow these steps:
Excel 98 Macintosh EditionExcel 97 for WindowsExcel Versions 5.0, 7.0 for Windows, Version 5.0 for MacintoshExcel 98 Macintosh EditionExcel 97 for WindowsExcel Versions 5.0, 7.0 for Windows, Version 5.0 for MacintoshOn the Tools menu, click References. Click Test.xls, and then click OK.=myfunction(5)Cell A1 should display the value 10. NOTE: If the reference is not made as outlined in steps 1-4, cell A1 displays the #NAME? error value because Microsoft Excel does not know where to find the "myfunction" custom function. Naming the function and the project with the same name also results in the #NAME? error. Note also that you cannot create a reference to an unsaved workbook, so if you encounter an error when you try to create the reference, make sure the workbook that contains the custom function has been saved. REFERENCESExcel 97For more information about custom functions, from the Visual Basic Editor, click Contents and Index on the Help menu, click the Index tab in Help, type the following textFunction proceduresand then double-click the selected text to go to the "Writing a Function Procedure" topic. If you are unable to find the information you need, ask the Office Assistant. Excel 98 Macintosh EditionFor more information about custom functions, from the Visual Basic Editor, click the Office Assistant, type function, click Search, and then click to view "Writing a Function Procedure."NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, click the article number below to view the article in the Microsoft Knowledge Base: Q176476 OFF: Office Assistant Not Answering Visual Basic Questions Earlier Versions of ExcelFor more information about custom functions, click Answer Wizard on the Help menu and type:tell me how to create a user-defined function Additional query words: 5.00a 5.00c
Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA |
Last Reviewed: October 15, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |