What is a UDF?
The acronym UDF stands for User Defined Function. With the UDF, you can create a custom function (i.e. not a part of excel built-in functions). It comes in very handy when it requires nesting of multiple built-in excel functions.
No, you cannot record a UDF. You have to write code for UDF yourself in module area of visual basic editor (VBE).
Syntax
UDFs must begin with Function keyword, followed by function name and then define parameter with in parentheses (), followed by As keyword and functions's data type and end funtion with End Function.
Important Point : If you have no parameter, you still need to write parentheses ().
How to use
Suppose you are asked to write a custom function that converts Fahrenheit to Celsius.
Like the other excel built-in functions, enter your custom function with = followed by function name.
Example 2
If student's score is greater than or equal to 75, he/she will get A grade.
If student's score is less than 75 but greater than 50, he/she will get B grade.
If student's score is less than or equal to 50, he/she will get C grade.
Can we record a UDF?
Benefits of UDFs
- It comes in very handy when it requires nesting of multiple built-in excel functions. By creating a UDF one time, you can use it anywhere in the workbook. It will do what you want.
- You can use a UDF in the Sub procedure.
Limitations of UDFs
- No macro recording facility
- Cannot change the structure of a worksheet such as insert/delete/rename a workbook or sheet, formatting etc
- Cannot use many of built in excel features such as AdvancedFilters, Find- Replace, Pivot Table etc
Difference between Sub Procedures and UDFs
UDFs must begin with Function keyword and end with End Function, whereas Sub procedures begins with Sub keyword and ends with End Sub keyword
Function First()
'Your code
End Function
UDFs must begin with Function keyword, followed by function name and then define parameter with in parentheses (), followed by As keyword and functions's data type and end funtion with End Function.
Important Point : If you have no parameter, you still need to write parentheses ().
How to use
- Open an Excel Workbook
- Press Alt+F11 to open VBA Editor
- Go to Insert Menu >> Module
- In the module, write code for the function you want
- Save the file as Macro Enabled Workbook (xlsm) or Excel 97-2003 Workbook (xls)
- Insert user defined function by typing the function i.e. =first()
Example 1
Suppose you are asked to write a custom function that converts Fahrenheit to Celsius.
Function Celsius(Fahrenheit As Double ) As Double
Celsius = (Fahrenheit - 32) * 5 / 9
End Function
How to use
In this example, 100 is the Fahrenheit value.
If student's score is less than 75 but greater than 50, he/she will get B grade.
If student's score is less than or equal to 50, he/she will get C grade.
Function grade(score As Single) As String
If score >= 75 Then
grade = "A"
ElseIf score > 50 Then
grade = "B"
Else
grade = "C"
End If
End Function
Nicely explained! Keep up the good work :-)
ReplyDelete