Excel Macro Custom Functions: How to create User Defined Functions

In Excel, an user defined function (UDF) is a function that is created by the user to perform a specific task. UDFs are written in Visual Basic for Applications (VBA), and can be used to perform tasks that are not possible with the built-in functions in Excel.

For example, if you have a UDF named “myFunction”, you can call it in your code like this: myFunction(argument1, argument2, …)

In this article, you will learn you how to create an user defined function in Excel.

What are User Defined Functions?

User defined functions are macros that can be used anywhere in an Excel workbook or VBA Module. They can be created from scratch or from existing VBA code.

How to create an User Defined Functions

Creating an user defined function is easy. First, open the Visual Basic Editor by pressing Alt+F11. Next, insert a new module by clicking Insert > Module.

In the module, enter your code: Function myFunction() End Function Replace “myFunction” with the name of your function. The code between the Function and End Function lines is where you will enter the code for your function. When you are finished, save the module and close the Visual Basic Editor.

Eg1. You create a function to type the entire website URL in a cell.

How to create a User Defined Functions
Function url() As String
    url= "https://pptexcel.com/"
End Function

Your function should start with the keyword Function and end with End Function.

You can insert the above code in VBA Module and call this custom function to return the website address.

Eg2: You create a function to return the given number multiply by 2

Function multiplyBy2(x) 
      multiplyBy2= x * 2 
End Function

This code defines a function called multiplyBy2 that takes one argument (x) and returns the value of x*2.

How to use an User Defined Function in Excel worksheet

To use your function, enter the function name in a cell followed by a set of parentheses. When you press Enter, the function will be executed.

For Eg1, Your function is named url, you would enter =url() in a cell.

How to use a User Defined Function in Excel
  1. Go to cell B5, you can select any cell
  2. Type your function in formula bar =url() – will return the complete URL address in cell B5

For Eg2, Your function is named multiplyBy2, you would enter =multiplyBy2(A1) in a cell.

  1. Go to cell B1, you can select any cell
  2. Type your function in the formula bar =multiplyBy2(A1) – where A1 is the cell that contains the value you want to multiply by 2.

How to use an User Defined Function in Excel VBA

Sub retFullAddress()
     MsgBox url
End Sub

The above code call the function inside VBA procedure and return the complete address in a message box

Conclusion

User defined functions are a great way to extend the functionality of Excel. They can be used to perform a wide variety of tasks, from simple calculations to complex data analysis. Creating an user defined function in Excel is easy and can be a great way to extend the functionality of the program. With a little bit of code, you can perform a wide variety of tasks.

So now you know how to create your custom functions in Excel and I hope that this will help you.

If you have any questions, please leave a comment below and I will get back to you as soon as possible.