Excel is a powerful software that is pre-programmed to run over 450 predefined functions and thousands of other processes that can be combined into truly complex and efficient formulae.

But sometimes there is a specific function we want to use that Excel simply doesn’t have. After all, while Excel developers have done a great job at anticipating many needs of users, it’s impossible they could know every single thing every single user will ever do with Excel!

User Defined Functions (UDFs) are a way to solve that issue. This is an intermediate-to-advanced Excel skill, so unless you’re doing some really intricate Excel work, you may not need to know how to do these processes, but it never hurts to learn!

Read on to find out more about the when, why, and how of creating custom functions in Excel.

The best Computer Skills tutors available
Ana
5
5 (34 reviews)
Ana
£100
/h
Gift icon
1st lesson free!
Andrea
5
5 (48 reviews)
Andrea
£150
/h
Gift icon
1st lesson free!
Asst prof kritaphat phd
5
5 (46 reviews)
Asst prof kritaphat phd
£69
/h
Gift icon
1st lesson free!
Robert
5
5 (17 reviews)
Robert
£99
/h
Gift icon
1st lesson free!
Alan
4.8
4.8 (18 reviews)
Alan
£40
/h
Gift icon
1st lesson free!
Micheal
5
5 (17 reviews)
Micheal
£35
/h
Gift icon
1st lesson free!
Juneyt (ma, msc oxford)
5
5 (24 reviews)
Juneyt (ma, msc oxford)
£110
/h
Gift icon
1st lesson free!
Abubakar
5
5 (21 reviews)
Abubakar
£25
/h
Gift icon
1st lesson free!
Ana
5
5 (34 reviews)
Ana
£100
/h
Gift icon
1st lesson free!
Andrea
5
5 (48 reviews)
Andrea
£150
/h
Gift icon
1st lesson free!
Asst prof kritaphat phd
5
5 (46 reviews)
Asst prof kritaphat phd
£69
/h
Gift icon
1st lesson free!
Robert
5
5 (17 reviews)
Robert
£99
/h
Gift icon
1st lesson free!
Alan
4.8
4.8 (18 reviews)
Alan
£40
/h
Gift icon
1st lesson free!
Micheal
5
5 (17 reviews)
Micheal
£35
/h
Gift icon
1st lesson free!
Juneyt (ma, msc oxford)
5
5 (24 reviews)
Juneyt (ma, msc oxford)
£110
/h
Gift icon
1st lesson free!
Abubakar
5
5 (21 reviews)
Abubakar
£25
/h
Gift icon
1st lesson free!
Let's go

What is a User-Defined Function (UDF)?

A User Defined Function in Excel is exactly what it sounds like: a custom-made function that you create in Excel to perform an operation that you specify.

Just like how the SUM function prompts Excel to add up the values inside the parentheses, your UDF will prompt Excel to perform a computation or task as well.

How to Use Formulas in Excel

Excel functions and formulas make using the program much more efficient, effective, and overall easier.

Formulas are added to functions to create precise and useful series of actions that help you enter and make sense of your data. You should always use repeatable formulae and functions in Excel whenever possible to streamline your data and computations, and gain more experience with the hundreds of functions available to you as well.

For example, instead of entering the formula to calculate the average of a data set by hand, you should always use the AVERAGE function, because it makes everything so much simpler, using less of your time and less memory in the file, as well.

UDFs allow you to create your own functions that contain the necessary formulas to achieve your specific goal.

A person looking at a desktop screen showing an Excel spreadsheet and graphs and a laptop screen showing an invoice
VBA programming language can be tricky to pick up, as with all programming languages, but once you understand how to use it, you can use it to process data more effectively and efficiently.

Features of UDFs

UDFs differ from Excels preset functions because you can set them up to specifically work with your data. Preset functions in Excel are meant for generic usage, making them handy for a wider user base but not adequate for data that has very specific needs.

Some of the benefits of using UDFs include:

Custom Functionality

UDFs allow users to create their own custom functions to perform specific calculations or operations that are not covered by Excel's built-in functions.

Accessing Excel's Object Model

UDFs have access to Excel's Object Model, which represents various elements of Excel such as cells, ranges, worksheets, and workbooks. This enables users to manipulate Excel data and perform complex calculations within their custom functions.

Syntax and Usage

Once a UDF is defined, it can be used in Excel formulas just like any other built-in function. Users can enter the UDF into a cell, specifying the required arguments, and the function will return the calculated result.
Just like how to utilize the VLOOKUP function in Excel, you would type “=VLOOKUP()”, to use your custom function you will simply need to name it and type the name in the formula bar like any other function. You can name your function anything as long as it’s not already being used by any other functions.

Reusable Code

UDFs promote code reusability. Once a custom function is created, it can be used across multiple worksheets or workbooks, providing a consistent and efficient way to perform specific calculations.

add_to_queue
Note About Using VBA in Excel for Web

As of the time of writing this article (March 2024), OneDrive Excel Online does not allow users to enter Developer mode. You must use a desktop version of Excel to access Developer mode.

What is Visual Basic for Applications (VBA)?

Visual Basic for Application is a programming language used in Excel for making things like UDFs. VBA is edited in the VBE (Visual Basic Editor).

To successfully use VBA for making UDFs, there are some syntaxes you will need to know. Just like when using a coding language like CSS, there are certain words and commands that you need to know in order to instruct the code to perform the function you have in mind.

Check out this VBA cheat sheet for some of the most common Excel commands for VBA.

[table “us_101068” not found /]
The best Computer Skills tutors available
Ana
5
5 (34 reviews)
Ana
£100
/h
Gift icon
1st lesson free!
Andrea
5
5 (48 reviews)
Andrea
£150
/h
Gift icon
1st lesson free!
Asst prof kritaphat phd
5
5 (46 reviews)
Asst prof kritaphat phd
£69
/h
Gift icon
1st lesson free!
Robert
5
5 (17 reviews)
Robert
£99
/h
Gift icon
1st lesson free!
Alan
4.8
4.8 (18 reviews)
Alan
£40
/h
Gift icon
1st lesson free!
Micheal
5
5 (17 reviews)
Micheal
£35
/h
Gift icon
1st lesson free!
Juneyt (ma, msc oxford)
5
5 (24 reviews)
Juneyt (ma, msc oxford)
£110
/h
Gift icon
1st lesson free!
Abubakar
5
5 (21 reviews)
Abubakar
£25
/h
Gift icon
1st lesson free!
Ana
5
5 (34 reviews)
Ana
£100
/h
Gift icon
1st lesson free!
Andrea
5
5 (48 reviews)
Andrea
£150
/h
Gift icon
1st lesson free!
Asst prof kritaphat phd
5
5 (46 reviews)
Asst prof kritaphat phd
£69
/h
Gift icon
1st lesson free!
Robert
5
5 (17 reviews)
Robert
£99
/h
Gift icon
1st lesson free!
Alan
4.8
4.8 (18 reviews)
Alan
£40
/h
Gift icon
1st lesson free!
Micheal
5
5 (17 reviews)
Micheal
£35
/h
Gift icon
1st lesson free!
Juneyt (ma, msc oxford)
5
5 (24 reviews)
Juneyt (ma, msc oxford)
£110
/h
Gift icon
1st lesson free!
Abubakar
5
5 (21 reviews)
Abubakar
£25
/h
Gift icon
1st lesson free!
Let's go

How to Create an UDF Function in Excel

So, how can you actually create your own UDF in Excel?

Before you begin, you should be familiar with how to write a formula in Excel and the basics of VBA.

Step 1: Open the Visual Basic for Applications (VBA) Editor

  • Press Alt + F11 (Windows) or Opt + F11 or Fn + Opt + F11 (Mac) to open the VBA editor in Excel. Alternatively, you can go to the "Developer" tab and click on "Visual Basic."
  • If the “Developer” tab is not visible on your toolbar, go to File > Options > Customize Ribbon > check the box on the right for Developer.

Step 2: Insert a New Module

  • In the VBA editor, right-click on any item in the "Project Explorer" window (usually on the left), hover over "Insert," and choose "Module."

Step 3: Write Your UDF Code

  • In the module window, you can start writing your UDF with VBA coding language. Here are some ground rules:
    • You must start your code with “Function” for VBA to recognize that what you write after that is the function.
    • Then, name your function. It can be any name you want as long as it’s not already being used by another function, and use camel-case instead of spaces when naming your function.
    • Directly after your function’s name, enter the arguments you want in your formula within a set of parentheses.
    • Add notes to your code by beginning a line with a single quote/apostrophe. These notes will not influence the code, but act as a note to yourself.
    • Define any other variables and objects needed to make your code work.
    • End with “End Function” to close the section.
  • If, when you hit enter/return to go to the next line, your line of code turns red, that means there is a syntax error that needs to be remediated.
dashboard
The Building Blocks of VBA

A statement in Visual Basic is a complete instruction. It can contain keywords, operators, variables, constants, and expressions. There are three different types of statements in VBA: Declaration, Assignment, and Executable.

Step 4: Save Your Workbook

  • Save your Excel workbook with a macro-enabled file extension (e.g., .xlsm) to ensure that it can store the VBA code.

Step 5: Use Your UDF in Excel

  • Go back to your Excel workbook.
  • In any cell, you can now use your custom function like any other Excel function. For example:

Congratulations! You now have a User Defined Function that can handle your unique computational needs!

See a video tutorial of how to access the VBE for writing VBA in Excel.

Sample Custom Functions You Can Add to Your Excel Sheets

Why might you want to create a UDF? Examples of UDFs include complex financial calculations, statistical analyses, or specialized data manipulations. These are the types of calculations that Excel programmers could not have anticipated, so you need to create your own function to handle the data processing.

Why Create a UDF?

There are many instances in which a UDF would be a helpful tool. Here are just some of the reasons why you may find it beneficial to learn how to build and program a UDF with VBA in Excel.

Specialized Calculations

Calculate a unique financial ratio or a custom statistical measure to meet a specific parameter that is not covered by standard Excel functions. For instance, you may need to compute a specific profitability index or a custom risk assessment metric tailored to a particular industry.

Data Transformation and Cleansing

Cleaning and transforming data in a specific way that is not easily achievable with built-in functions. A UDF could be designed to handle complex data cleansing tasks, such as removing duplicates, formatting strings, or performing specialized text manipulations.

Industry-Specific Formulas

In industries like engineering or science, there may be a need for specialized calculations that are unique to that field. You can create a UDF to compute complex engineering formulas or scientific equations that are not readily available in standard Excel functions.

Automating Complex Tasks

Automating a series of intricate tasks or analyses that require multiple steps. A UDF can be created to streamline and automate these tasks, saving time and reducing the likelihood of errors. This might include complex financial modeling, scenario analyses, or data processing workflows.

Custom Reporting and Analysis

Generating customized reports with specific formatting, layouts, or content. A UDF can be part of a larger system that facilitates the creation of tailored reports based on user-defined parameters.

A person making an Excel spreadsheet and graphs with papers of data nearby
Making UDFs in Excel can help you accomplish specific functions that Excel doesn't have in its pre-existing library.

What a UDF Looks Like in VBA

Here is one example of a UDF written in VBA. This code is meant to add up all the dollar amounts in a range of data and then divide them by 12 to get the monthly payment needed to pay it all off in one year. Keep in mind this is an extremely rudimentary example meant to be relatable and easy-to-understand for beginners. You likely wouldn’t need this exact function for any of your financial needs, but the concept is easy to follow.

Example:

Function CalculateMonthlyPayment(rng As Range) As Double

' This UDF adds a range of numbers and divides the sum by 12 to get the monthly payment amount

' Check if the input range is not empty

If Not rng Is Nothing Then

Dim cell As Range

Dim totalAmount As Double

' Loop through each cell in the range and sum the values

For Each cell In rng

If IsNumeric(cell.Value) Then

totalAmount = totalAmount + cell.Value

End If

Next cell

' Divide the total amount by 12 to get the monthly payment

CalculateMonthlyPayment = totalAmount / 12

Else

' Return 0 if the input range is empty

CalculateMonthlyPayment = 0

End If

End Function

A person using Excel on a laptop making a spreadsheet and graphs
Make UDFs to compile complex data sets into simple data that can be used to make easy-to-understand grids and graphs.

Helpful UDFs in Excel

Since UDFs are, by definition, extremely individualized, there is a wide variety of UDFs that others have created for specific purposes. One great resource is to go on forums like Reddit, VBA Express Forum, and OzGrid Forums to see what other VBA users are doing with their codes.

Here are just a few interesting applications other Excel users have come up with:

  • Get the name of a sheet used in the referenced cell
    • Function SheetName(CellReference As Range)
      SheetName = CellReference.Parent.Name
      End Function
  • Check whether a file already exists with a certain name
    • Function filesExist(ByVal fileArr As Variant, Optional checkAll As Boolean = True) As Boolean
      'Determines whether files (fileArr) exist
      'Each value in fileArr must be a full file path
      'If checkAll = True, then all files in fileArr must exist to return True
      'If checkAll = False, then at least one file in fileArr must exist to return True
      filesExist = thingsExist("", fileArr, "File", checkAll)
      End Function
  • Check if a specific sheet already exists
    • Function CheckSheetExists(ByRef wb As Workbook, ByVal shName As String) As Boolean
      Dim ws As Worksheet
      'loops through all the sheets in the workbook
      For Each ws In wb.Worksheets
      'if the sheet exists among the workbooks sheets the functions returns 'True' and ends
      If ws.Name = shName Then
      CheckSheetExists = True
      Exit Function
      End If
      Next ws
      'in case the sheet hasn't been found the function returns 'False'
      CheckSheetExists = False
      End Function
devices
The Limitless Uses of UDFs

Since you can define objects in your VBA code, you can create millions of different UDFs.

Discover More UDFs with Superprof

Learning any coding language comes with a huge learning curve, and VBA is no different. If you’re struggling to wrap your head around it, you’re not alone! You can learn how to master VBA and create UDFs with ease with the help of a tutor from Superprof!

A few Excel classes with a qualified and experienced tutor will have you creating competent code in no time.

Creating UDFs in Excel can be a real game-changer for your Excel usage. Start learning more about this complex but effective technique to accelerate your Excel experiences!

Enjoyed this article? Leave a rating!

5.00 (1 rating(s))
Loading...

Emma Cowan

I am passionate about traveling and currently live and work in Paris. I like to spend my time reading, gardening, running, learning languages, and exploring new places.