×

Excel VBA User-Defined Functions

User-Defined Functions

One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can create your own functions in VBA and then use them just like you use Excel’s built-in functions, such as IF, AVERAGE. Once the custom or user-defined function is created, one needs to know only the function name and its arguments.

Few practices which are followed in UDF are as follows:

• Giving data types of arguments is not always necessary, but it is considered a good practice
• You can look for the functions you have created by going to ‘User Defined Functions’ in the ‘Formula’ toolbar and change their description so that whenever you open this particular excel file, they are readily available for your use.


Steps to declare the Syntax

Function  Name (arguments,….)
Name= Functionality
End Function 


• Declaring a custom function starts with the keyword ‘Function’ and ends with ‘End Function’
• Following keyword Function, we write the name of the function – in our case we have named the function as Name
• Now, the functions will have some arguments which will be declared in the brackets. It is optional.

• In the next line, we show how to perform the calculation or the functionality of the function.

Sharing UDFs

The extension or location where you store a User Defined Function affects the sharing capability of the file. The commonly used storage extensions are as follows:

  • Personal.xlsb— Onestores a UDF in Personal.xlsb if it is just for their use and won’t be used in a workbook opened on another system.
  • Workbook— One stores a UDF in the workbook if it needs to be distributed to many people.
  • Add-in— Onedistributes a UDF via an add-in if the workbook is to be shared among a select group of people.
  • Template— One store a UDF in a template if it needs to be used to create several workbooks, and the workbooks are distributed to many people.

Example 1: Let’s suppose you want to calculate the area of a circle – now area of a circle is ?r2 where the value of ? is 3.14 and r is the radius of the circle –

• Click on ‘Visual Basics’ and then click on ‘Module’
• In the program editor, you will write –

'function name is Area
 'Here we just have one argument which is radius - double
 'if you may recall is one of the data types we 'have in VBA - so we are telling VBA that this value can be too large
 Function Area(Radius As Double)
     ' the area is going to be calculated by  ? r2 where the value of  ? is 3.14 and r is radius which we want     the end-user to fill in 
     Area = 3.14 * Radius * Radius
 End Function 

Output

Now go back to excel and call the function as you normally call any other function i.e., by using equal to the operator and the name of the function = AREA (Cell containing radius or value of radius can be entered manually).

Area of Circle

Click on enter. You will notice, as per the function, the area has been calculated.

Area of Circle

Example 2: Write a macro to define a UDF for adding two numbers in Excel.

• Click on ‘Visual Basics’ and then click on ‘Module’.
• In the program editor, you will write –

'function name is ADD .
 'Arguments are placed in parentheses after the name of the function. This example has two arguments: Number1
 'and Number2 .
 'Integer defines the variable type of the result as a whole number
 Function Add(Number1 As Integer, Number2 As Integer) As Integer
     'ADD = Number1 + Number2 is the result of the function that is returned
     Add = Number1 + Number2 
 End Function 

Output

In the Excel Sheet, call the function by using equal to the operator and the name of the function = ADD (Cell containing number 1 value that can be entered manually as well, Cell containing number 1 value).

Click on Enter, and you will get the sum of two numbers. This will work similarly, unlike Excel built-in SUM function.

sum of two numbers

Example 3: Write a macro using UDF to set the Current Workbook’s Name and File Path in a Cell

'No arguments are used in this function
 Function FullName() As String
     FullName = ThisWorkbook.FullName
 End Function 

Output

set the Current Workbook’s Name

Related Topics

Excel VBA Atn Function

VBA Atn Function: The Atn function in VBA returns the arctangent between quadrant -?/2 and +?/2 for the specified number, in radians. Syntax Atn (Number) Parameter Number (required) – This parameter represents the number that...

1 minute read.

Excel VBA CDbl Function

VBA CDbl Function: The CDbl function in VBA converts an expression into a Double data type. Syntax CDbl (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

Excel VBA StrConv Function

The StrConv function in VBA converts a string into a specified format. Syntax StrConv (String, Conversion, [LocaleID]) Parameter String (required) – This parameter represents the string to be converted. Conversion (required) – This parameter specifies the type of conversion. It can...

1 minute read.

Excel VBA Chr Function

VBA Chr Function: The Chr function in VBA returns the character equivalent to a supplied character code between 0 and 255. Syntax Chr (CharCode) Parameter CharCode (required) – This parameter represents the character code for...

1 minute read.

Excel VBA String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

Excel VBA IsNull Function

VBA IsNull Function: The IsNull function in VBA returns a Boolean, indicating whether a supplied expression is Null. Syntax IsNull (Expression) Parameter Expression (required)- This parameter represents the name of the argument that you want...

1 minute read.

Excel VBA Now Function

Excel VBA Now Function: The Now function in VBA returns the current date and time.  Syntax Now () Parameter NA Return This function returns the current date and time.  Example 1 Sub NowFunction_Example1() 'It will return...

1 minute read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

VBA Regex Pattern Validation

VBA Regex Pattern Validation The key purpose of using Regex in VBA was to validate the data and extract the same category of data. There are certain formats that are standard...

6 minutes read.

Excel VBA StrReverse Function

The StrReverse function returns a String after reversing the given String. Syntax StrReverse (Expression) Parameter Expression (required) – This parameter represents the String that you want to reverse. Return This function returns a String after reversing the given String. Example...

1 minute read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.

Excel VBA InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

Excel VBA Left Function

VBA Left Function: The Left function in VBA returns a substring from the start of the specified string. Syntax Left (Str, Length) Parameter Str (required) – This parameter represents the string that you want to extract...

1 minute read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Excel VBA- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position...

4 minutes read.