Home

How to Create Custom Excel Functions

|
|  Updated:  
2022-01-07 21:27:50
Excel Sales Forecasting For Dummies
Explore Book
Buy On Amazon
Despite all the functions provided by Excel, you may need one that you just don't see offered. Excel lets you create your own functions by using VBA programming code; your functions show up in the Insert Function dialog box.

Writing VBA code is not for everyone. But nonetheless, here is a short-and-sweet example. If you can conquer this, you may want to find out more about programming VBA. Who knows — maybe one day you'll be churning out sophisticated functions of your own! Make sure you are working in a macro-enabled workbook (one of the Excel file types).

Follow along to create custom functions:

  1. Press Alt + F11.

    This gets you to the Visual Basic Editor, where VBA is written.

    You can also click the Visual Basic button on the Developer tab of the Ribbon. The Developer tab is visible only if the Developer checkbox is checked on the Customize Ribbon tab of the Excel Options dialog box.

  2. Choose Insert→Module in the editor.

    You have an empty code module sitting in front of you. Now it's time to create your very own function!

  3. Type this programming code, shown in the following figure:

    Writing your own function.

    Writing your own function
    Public Function Add(number1 As Double, number2 As Double)
    Add = number1 + number2
    End Function
  4. Save the function.

    Macros and VBA programming can be saved only in a macro-enabled workbook.

    After you type the first line and press Enter, the last one appears automatically. This example function adds two numbers, and the word Public lists the function in the Insert Function dialog box. You may have to find the Excel workbook on the Windows taskbar because the Visual Basic Editor runs as a separate program. Or press Alt+ F11 to toggle back to the Workbook.

  5. Return to Excel.

  6. Click the Insert Function button on the Formulas tab to display the Insert Function dialog box.

    Finding the function in the User Defined category.

    Finding the function in the User Defined category
  7. Click OK.

    The Function Arguments dialog box opens, ready to receive the arguments. Isn't this incredible? It's as though you are creating an extension to Excel, and in essence, you are.

    Using the custom Add function.

    Using the custom Add function
This is a very basic example of what you can do by writing your own function. The possibilities are endless, but of course, you need to know how to program VBA.

Macro-enabled workbooks have the file extension .xlsm.

About This Article

This article is from the book: 

About the book author:

Ken Bluttman is a veteran software and web developer specializing in Excel/VBA and database-centric web applications. He has written articles and books on topics like Office/VBA development, XML, SQL Server, and InfoPath. Ken is the author of Excel Charts For Dummies and all previous editions of Excel Formulas & Functions For Dummies.