XL: How to Use a Custom Function in Another Workbook

Last reviewed: February 3, 1998
Article ID: Q141288
The information in this article applies to:
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a

SUMMARY

In 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 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 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 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/default.asp

The 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

  1. Open a new workbook.

  2. Insert a module sheet. Follow the appropriate directions below:

    Excel 98 Macintosh Edition --------------------------

    a. Start the Visual Basic Editor (press OPTION+F11).

    b. On the Insert menu, click Module.

    Excel 97 for Windows ---------------------

    a. Start the Visual Basic Editor (press ALT+F11).

    b. On the Insert menu, click Module.

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

    On the Insert menu, point to Macro, and then click Module.

  3. On the module sheet, enter the following code:

          Function myfunction(x)
    
             myfunction = x * 2
          End 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 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 Workbook

In 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:

  1. Open another new workbook.

  2. Save this workbook as Test2.xls.

  3. Insert a module sheet. Follow the appropriate directions below:

    Excel 98 Macintosh Edition --------------------------

    a. Start the Visual Basic Editor (press OPTION+F11).

    b. On the Insert menu, click Module.

    Excel 97 for Windows --------------------

    a. Start the Visual Basic Editor (press ALT+F11).

    b. On the Insert menu, click Module.

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

    On the Insert menu, point to Macro, and then click Module.

  4. Create a reference to Text.xls. Follow the appropriate directions below:

    Excel 98 Macintosh Edition --------------------------

    a. Press COMMAND+R to activate the Project Explorer window.

    b. Select "VBAProject (Test.xls)," and then click VBAProject Properties

          on the View menu.
    

    c. In the Project Name box, type "Myfunction" (without the quotation

          marks), and then click OK.
    

    d. Select "VBAProject (Test2.xls)" in the Project Explorer window. e. On the Tools menu, click References. Click Myfunction, and then click

          OK.
    

    Excel 97 for Windows --------------------

    a. Press CTRL+R to activate the Project Explorer window.

    b. Select "VBAProject (Test.xls)," and then click VBAProject Properties

          on the View menu.
    

    c. In the Project Name box, type "Myfunction" (without the quotation

          marks), and then click OK.
    

    d. Select "VBAProject (Test2.xls)" in the Project Explorer window. e. 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 custom function. You can now call this custom function the same way you would call it if the workbook contained the code. For example, in cell A1, type the formula:

   =myfunction(5)

Cell A1 should display a value 10.

NOTE: If the reference is not made as outlined in steps 1-4, cell A1 will display the #NAME? error value because Microsoft Excel does not know where to find the "myfunction" custom function. 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.

REFERENCES

For 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.00 5.00a 5.00c 7.00
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.