You are currently offline, waiting for your internet to reconnect

XL: How to Use a Custom Function in Another Workbook

This article was previously published under Q141288
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SUMMARY
In Microsoft Excel, you can create custom Microsoft Visual Basic forApplications functions to perform calculations. By default, these customfunctions are available to all worksheets in the workbook that contains thecustom function. Therefore, you can call them just as you call built-infunctions in Microsoft Excel. However, if you attempt to call a customfunction from outside of the workbook that contains the code, you mustfirst make a reference to the workbook that contains the custom function.
MORE INFORMATION
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. The following steps show you how to create a simple custom function, how tocall the function from the workbook that contains the code, and how to callthis custom function from another workbook.

To Create a Custom Function

  1. Open a new workbook.
  2. Insert a module sheet using the appropriate method for your version of Excel.

    Excel 98 Macintosh Edition

    1. Start the Visual Basic Editor (press OPTION+F11).
    2. On the Insert menu, click Module.

    Excel 97 for Windows

    1. Start the Visual Basic Editor (press ALT+F11).
    2. On the Insert menu, click Module.

    Excel versions 5.0, 7.0 for Windows, version 5.0 for Macintosh

    1. On the Insert menu, point to Macro, and then click Module.
  3. On the module sheet, type the following code:
    Function myfunction(x)   myfunction = x * 2End Function					
  4. Save this workbook as Test.xls.

To Call the Function from the Workbook That Contains the Function Code

To call this function from within Test.xls, type the following formula incell A1 on Sheet1:
=myfunction(3)
Cell A1 should display the value 6, because the custom function doubled thevalue that was sent to it.

To Call the Function from Another Workbook

In order to call the function from a workbook, you must create a referenceto the workbook that contains the code. To create a reference, follow thesesteps:
  1. Open another new workbook.
  2. Save this workbook as Test2.xls.
  3. Insert a module sheet using the appropriate method for your version of Excel.

    Excel 98 Macintosh Edition

    1. Start the Visual Basic Editor (press OPTION+F11).
    2. On the Insert menu, click Module.

    Excel 97 for Windows

    1. Start the Visual Basic Editor (press ALT+F11).
    2. On the Insert menu, click Module.

    Excel Versions 5.0, 7.0 for Windows, Version 5.0 for Macintosh

    1. On the Insert menu, point to Macro, and then click Module.
  4. Create a reference to Text.xls using the appropriate method for your version of Excel.

    Excel 98 Macintosh Edition

    1. Press COMMAND+R to activate the Project Explorer window.
    2. Select VBAProject (Test.xls), and then click VBAProject Properties on the View menu.
    3. In the Project Name box, type MyProject, and then click OK.
    4. Select VBAProject (Test2.xls) in the Project Explorer window.
    5. On the Tools menu, click References. Click Myfunction, and then click OK.

    Excel 97 for Windows

    1. Press CTRL+R to activate the Project Explorer window.
    2. Select VBAProject (Test.xls), and then click VBAProject Properties on the View menu.
    3. In the Project Name box, type MyProject, and then click OK.
    4. Select VBAProject (Test2.xls) in the Project Explorer window.
    5. On the Tools menu, click References. Click Myfunction, and then click OK.

    Excel Versions 5.0, 7.0 for Windows, Version 5.0 for Macintosh

    On the Tools menu, click References. Click Test.xls, and then click OK.
You should now have a reference to the workbook that contains the customfunction. You can now call this custom function the same way you would callit if the workbook contained the code. For example, in cell A1, type theformula:
=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 theproject with the same name also results in the #NAME? error. Note also that you cannot create a reference to an unsaved workbook, so ifyou encounter an error when you try to create the reference, make sure theworkbook that contains the custom function has been saved.
REFERENCES

Excel 97

For 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 text
function procedures
and 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 Edition

For more information about custom functions, from the Visual Basic Editor, click theOffice Assistant, type function, click Search, and then click to view "Writing a Function Procedure."

Earlier Versions of Excel

For more information about custom functions, click Answer Wizard on the Help menu and type:
tell me how to create a user-defined function
5.00a 5.00c
Properties

Article ID: 141288 - Last Review: 10/11/2006 13:58:10 - Revision: 2.3

  • Microsoft Excel 97 Standard Edition
  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Excel 5.0c
  • Microsoft Excel 98 for Macintosh
  • Microsoft Excel 5.0 for Macintosh
  • Microsoft Excel 5.0a for Macintosh
  • kbdtacode kbhowto kbprogramming KB141288
Feedback