Creating Custom VBA Functions (UDFs)
Build your own Excel functions with User-Defined Functions (UDFs) in VBA
Quick Answer
Create a UDF: Use Function instead of Sub, add parameters, and return a value.
Use in Excel: Type =MyFunction(A1) in any cell
What are User-Defined Functions?
User-Defined Functions (UDFs) are custom Excel functions you create with VBA. They work just like built-in functions (SUM, VLOOKUP, IF) but perform calculations you design.
Built-in Functions:
=SUM(A1:A10)=VLOOKUP(A1, B:C, 2)=IF(A1>100, "High", "Low")Your Custom Functions:
=CalculateTax(A1)=GetFullName(B1, C1)=IsWeekend(D1)Benefits of UDFs:
- ✓ Reusable across worksheets and workbooks
- ✓ Simplify complex formulas
- ✓ Encapsulate business logic
- ✓ Auto-recalculate when inputs change
- ✓ Available in Formula Builder
Creating Your First UDF
Step 1: Open VBA Editor
Press Alt + F11 to open the VBA Editor
Step 2: Insert a Module
Insert → Module (functions must be in modules, not sheet code)
Step 3: Write the Function
Function name = return value assigns the result
Step 4: Use in Excel
In any cell, type: =DoubleIt(5)
Result: 10
Function Syntax
Key Differences from Subs:
Subroutines (Sub)
- • Perform actions
- • Don't return values
- • Can't use in formulas
- • Run with F5 or button
Functions
- • Calculate and return value
- • Return exactly one value
- • Use in worksheet formulas
- • Auto-recalculate
Working with Parameters
Single Parameter:
Excel: =AddTax(100) → 108
Multiple Parameters:
Excel: =GetFullName(A1, B1)
Optional Parameters:
=Greet("Smith") → "Mr. Smith"=Greet("Smith", "Dr.") → "Dr. Smith"
ParamArray for Variable Arguments:
Excel: =SumAll(1, 2, 3, 4, 5) → 15
Return Types
| Return Type | Use For | Example |
|---|---|---|
| Double | Numbers with decimals | 123.45 |
| Long | Whole numbers | 12345 |
| String | Text | "Hello" |
| Boolean | True/False | TRUE |
| Date | Dates and times | 12/3/2025 |
| Variant | Any type (flexible) | Anything |
Boolean Function Example:
Variant Return (Handles Errors):
Practical UDF Examples
1. Calculate Age from Birthdate:
2. Remove Extra Spaces:
3. Count Words in Text:
4. Reverse Text:
5. Extract Initials:
UDFs with Range Parameters
Functions can accept Excel ranges as parameters and process multiple cells.
Count Cells Above Threshold:
Excel: =CountAbove(A1:A10, 100)
Sum Positive Values Only:
Volatile Functions
By default, UDFs only recalculate when their input cells change. Make a function volatile to recalculate whenever Excel recalculates.
Warning: Volatile functions slow down Excel. Use sparingly!
UDF Best Practices
CalculateTax not CTUDF Limitations
UDFs Cannot:
- ❌ Modify other cells (read-only from worksheet)
- ❌ Change formatting or cell properties
- ❌ Insert/delete rows or columns
- ❌ Open/close workbooks
- ❌ Change Excel settings
- ❌ Show message boxes (use in Sub instead)
Solution: Use Sub routines for actions, Functions only for calculations
Sharing UDFs
Method 1: Personal Macro Workbook
Store UDFs in PERSONAL.XLSB - available in all workbooks
- Developer → Record Macro → Store in Personal Macro Workbook
- Stop recording immediately
- Alt+F11 → Find VBAProject (PERSONAL.XLSB)
- Add your functions to a module there
Method 2: Add-in (.xlam)
Create Excel Add-in for professional distribution
- Save workbook as Excel Add-in (.xlam)
- File → Options → Add-ins → Go (Excel Add-ins)
- Browse and select your .xlam file
- Functions available in all workbooks