Create a Custom Function in Excel - UDF

Add to Favorites

How to create a custom worksheet function in Excel. These are called User Defined Functions, or UDF, and they are amazing. You can create your very own function that does whatever you want it to, almost.

To make these functions we just need a little bit of VBA since they are basically macros.

Sections:

Steps to Create a UDF in Excel

Adding Arguments to the Function

Using VBA within the Function

The Final Result: Custom Excel Function

Notes

Steps to Create a UDF in Excel

  1. Hit Alt + F11 to go to the VBA Editor window.
    af2e67e0546ad3f93da52981a35c91c2.jpg
  2. Go to Insert > Module
    360791f5cb2179840611b7055f2701d5.jpg
    You should now see a window that looks like this:
    297b495641479e3077cc4cf23813da2a.jpg
  3. In that window type Function and the name of your function and then an open and closing parenthesis and then hit Enter. I named my function CountCharacters
    You can give your function basically any name that is not already used for functions.
    Once you do this, the window will automatically add End Function to the window and it should look something like this:
    cd4524226b585b35b421b83a04499ee8.jpg
  4. Put some code within the function to make it do something.
    Here, I will simply set a variable equal to some text that I would like to output.
    outputText = "This is output."

    82b2d356bfe7ba97c11e4a9109051ae6.jpg
  5. Now, we need to put the output of our function into a variable that has the SAME name as our function.
    To do this, I add this line of code to the bottom of the function's code:
    CountCharacters = outputText

    03e39b5d2bbbe20c290638a0a12a6127.jpg
  6. Hit Alt + F11 to go back to Excel and input the new function into a cell.
    05c8acb925ffde1d6966cf3de38c13a0.jpg
    Notice that when I start typing the name of the function it appears in the function list drop-down for Excel.
    When you finish entering the function hit Enter like you would with a regular function.
  7. That's it! Here is the final result.
    6a9cd8adeec1a68276cdc639ef645cee.jpg

In this example I created the simplest form of a UDF or User Defined Function.

Every UDF that you create will follow the basic structure outlined in this example, which is:

  1. Functions must start with Function and then the name you want to give that function and an open and closing parenthesis. Arguments can go into the parenthesis, as discussed below in another example.
  2. Functions must end with the text End Function, which the Excel VBA window will usually input for you.
  3. To generate the output for the function and send it back to Excel, you must assign the output of the function to a variable that has the exact same name as your function.

Adding Arguments to the Function

Let's now add an argument to the function in order to make it more useful.

Arguments are the parts of a function where you can input data, or select a cell that has data, that you want your function to use. Arguments are the way you get data into your function.

It's actually very simple; we just input text in-between the parenthesis after the name of the function, and that text will be the argument.

Let's start with the example we made above:

5a0679d037832795a87971fcafac7655.jpg

Now, add text in the parenthesis:

4ca58511de2277dcbc715ab04da7da66.jpg

I put input_value as the argument. Note that you can't use spaces for the names, instead, separate words using underscores.

Now that we have input_value as an argument, we can use it within the function. This is how we get data from the user into the function.

To make this function more dynamic, since it currently only outputs the hardcoded text "This is output.", I will set the variable outputText equal to the new argument input_value.

cacfd1fe759dd87b7a6f5f887d33f22b.jpg

Go back to Excel (hit Alt + F11) and let's try this function on another cell.

c303153117d7fdad93e8993374fe53be.jpg

Here is the result:

1395f8457ffa29b424a3ba02c7b11b5f.jpg

This simple function now outputs whatever text is given to it, in this case, the text from cell A1.

Multiple Arguments

You can have multiple arguments, just separate each one with a comma like this:

Function MyFunctionName(Argument_1, Argument_2, Etc.)

Using VBA within the Function

We are creating this function using VBA (Visual Basic for Applications), which is the same thing we use for regular macros. As such, we can do many interesting and powerful things. Now, functions can't do everything that a regular macro can do, because the goal of a function is to return a result back to the cell, but you can still do a lot.

Let's finish the function we started to create and make it count all of the characters that are in a cell.

To do this, we add the len VBA function, which is used to count the length of the value that you put inside of it.

I want to count the input to this function, which is provided through the input_value variable and so I need to put that inside the len function.

This:

outputText = input_value

Will become this:

outputText = len(input_value)

Since the rest of the function is already setup to output the value stored in the outputText variable, I don't need to change anything else.

Here is the final Function code:

14abbb60ba5833f3a7667db37521b53b.jpg

Go back to Excel and try it now.

32660693f922c97ef4530547ce256aaa.jpg

Outputs:

0c65f2f80313c3637c1d1be6ff442499.jpg

We now have a function that counts how many characters are in a cell and gives us the result.

The Final Result: Custom Excel Function

This was a very simple example using VBA to create a function but you can include many lines of code depending on the complexity of what you are trying to do. In this example, I tried to keep things simple to help give you an idea of how everything works so you can build upon it.

Here is the final version of the code that was created:

Function CountCharacters(input_value)

    outputText = Len(input_value)

    CountCharacters = outputText

End Function

4a25d811441c32865772fa501faa6354.jpg

Notes

Custom Excel functions, or UDFs, are simply awesome. They are one of my favorite aspects of Excel because it allows you to easily create a function that does almost whatever you could want it to do. When you have repetitive tasks that take many steps or require a complex formula that is hard to remember, you can create a custom function to do the work for you.

The example that I created above is not a useful custom function on its own since there is already a LEN() function in Excel, but it should help you to understand how custom functions are made and used.

UDFs are only available in the workbook in which you have them or when that workbook is open and you are referencing them using the correct cross-workbook method, with is rather annoying. In another tutorial I will show you how to make them available everywhere.

Make sure to download the attached workbook to see the final result and get the code.


Downloadable Files: Excel File