VBA function?

This is going to be a quick tutorial on how would you go about creating a VBA function.

To begin with you must understand what a function is. Excel by default has created a lot of function in the software like Sum, vlookup etc.

Now, there are certain circumstances where you want to create your own functions which work exactly the same way as a normal function will do. But you would need to do some coding to get them working. The basic principle behind a Function is that it takes some arguments and returns a value. For example, Sum function would take the reference of the cells you want to sum up and then the function would return the Sum.

Creating a user defined VBA function in Excel VBA is very easy. All that you would need to do is, define a function in any of the module of your excel worksheet.

Just as below...

Public Function Area(Length As Double, Width As Double)

Area = Length * Width

End Function

In the example VBA function above, the Area is the name of the function which takes two arguments the Length and the Width. The VBA function then uses these two values, performs an operation and then returns a result which is stored in Area.

The VBA functions that you create can be used in excel worksheets just as you would use any default excel functions. See the screenshots attached herewith that aims to explain how to create and use VBA functions.

VBA Function

 

VBA Function

 

VBA Function final result

Would love to hear your comments about this post. Do let me know in case you have questions.