VBA Function in Excel (Table of Contents)
VBA Function in Excel
We also have functions in VBA as well which are completely inbuilt in it. Almost all the excel functions can be used VBA as well with almost the same syntax. And if there is any change in the syntax, we will be able to see that also when we are about to use VBA function. To declare a function in VBA, first, open a Private-End procedure and select the data type as per functional need.
Visual Basic
VBA stands for Visual Basic for an application. VBA is normally a programming language of excel which is embedded in an individual Microsoft application i.e. Excel or Access where we can do the automation of several tasks which is also called as “MACRO” in excel. The Visual Basic application comes with several command buttons and other function which makes it easier for the user for automation.
VBA User Defined Function (UDF)
A User-defined function is just a function where we can create our self in VBA for automation purpose.
This user-defined function is nothing but a code module attached to the worksheet where the user will actually type the coding.
How to Use VBA Function in Excel?
In excel we can find the VBA function under “DEVELOPER” tab by default excel does not have this menu. So in order to display it, we need to add this “DEVELOPER” menu manually by following the below steps.
- Go to File menu. Click on Option menu which has highlighted in Yellow Color.
- Once we click on the option we will get the below dialogue box as follows.
- Select Customize Ribbon option and we will get the below dialogue box as follows
- In customize ribbon option we can see add or remove the ribbon manually. Here we can see that on the “Right-hand side” of the customize ribbon “DEVELOPER” Tab is not checked. Checkmark that “DEVELOPER” Tab and click OK
- Once you check mark the “DEVELOPER” Tab it will get added in excel menu which is displayed as follows
Now we can see that the “Developer” Tab is added and it has the features like VISUAL BASIC, MACROS, etc.
Adding VBA in Excel
In Excel, VBA workbook can be added easily by following the below steps.
- Open Excel workbook. Use the shortcut key ALT+F11 to open Visual Basic Editor which is also called as VBE in excel shown in the below screenshot
- Right-click on your workbook name in the ” Project VBAProject” pane (at the top left corner of the editor window) and select Insert -> Module from the context menu as shown below
- Now we can see that new module has been added as MODULE 1 on the left-hand side of the panel.
- In the Right-hand side where we can use the UDF function using VBA.
The function of Visual Basic
In Visual Basic, we will use the various function. A function is nothing but a collection of visual basic statements enclosed by start and an end function. This function normally performs a task and then returns controls to the calling code. If it returns the control it will also return a value to the specific code.
VBA Function
Visual basic contains a large number of built-in function like Message Function, String and Text Function, Information Function, a Conversion function, Date and Time, Math and Trig Functions, Financial function
We will see the mostly used VBA function which is listed as follows.
- Input Box- Which displays an input dialogue box
- Msg Box- Which displays Message box i.e. alert message box
- Format- Which applies format for the specific string.
- Len- Which returns a length of the string
- Replace – Which will replace the specific string with another string.
- Is Array- Which will check for the supplied value is an array.
- Is Date- Which will check for the supplied expression is date format.
- Is Empty – Which will check for the specific variant is empty or not
- Is Null – Which will check for the supplied value is null or not
- Date – Returns the current date.
- Month – Returns the month number.
- Now – Returns the current date and time.
- Year – Returns the current year.
Examples of VBA Function in Excel
VBA Function is very simple to use. Let us now see how to use VBA function in Excel with the help of some examples.
Example #1
Addition of Two Numbers
In this example, we will see how to add two numbers using VBA function by using simple programming language step by step as follows.
- Open excel workbook. Use shortcut key ALT+F11 to open VBE editor. We get the VBE editor as follows
- Go to Insert Menu and select module
Once we click on the module a new module will be added in a worksheet Where we will write the VBA function coding on the right-hand side of General page.
Now to get the result of adding two numbers we have to use the below coding as follows
Function addtwo(arg1, arg2)
addtwo = arg1 + arg2
End Function
Here in the above coding, we have named the function as “addtwo” and the coding will be displayed as:
Now in the excel sheet, we can use the “addtwo” function to add the new number as:
The Result is:
Example #2
Date Function
In VBA, date function is a built-in function DATE() which return date and time we will now use the date function to get the date in the particular sheet. In VBA we can use UDF to get the result.
Insert a new Module as we have discussed above in Example 1.
Once we click on the module a new module will be added in the worksheet and on the right-hand side of General page, we will write the VBA function coding.
Sub todayDate()
Range("A1").Value = Date
End Sub
Here in the above coding, we have named the function as “todayDate” and the coding will be displayed as:
Hit the RUN button or Press F5 to execute the code
This coding will only return the current date in the mentioned cell name A1 and once we execute, the result will be shown as:
Example #3
VBA Message Box
Visual basic normally has two message box function
- The Msg Box method.
- The Msg Box Function.
The MsgBox Method
The MSGBOX method is one of the VBA languages where it displays a msgbox for the given string, it normally has the ok button, so that user can see the MSGBOX and continue with the specified task.
The MsgBox Function
The MSGBOX Function which returns value normally this MSGBOX function will be a question type like Yes or No or Abort or Continue.
We will see both the MSG box with the below examples as follows.
Msg Box method Example:
Insert a new Module as we have discussed above in Example 1.
Once we click on the module a new module will be added in the worksheet and on the right-hand side of General page, we will write the VBA function coding.
Sub Firstmsg()
MsgBox "Hello"
End Sub
In the below screenshot, we have written coding to display the MSG BOX as Hello.
Hit the RUN button or Press F5 to execute the code.
We will get the below output.
We can see the HELLO message which comes from the MSG box.
Msg Box Function Example:
In this example, we will see how to use MSG BOX function using vbYesNoCancel
Insert a new Module as we have discussed above in Example 1.
Once we click on the module, a new module will be added in the worksheet and on the right-hand side of General page, where we will write the VBA function coding.
Sub Firstmsg()
MsgBox "Welcome User or Guest?", vbYesNoCancel, "Login?"
End Sub
In the below screenshot, we have written coding to display the MSG BOX. Here in the MSG BOX function, we have given YES or NO option to check the end user is User or Guest.
Hit the RUN button or Press F5 to execute the code
After execution, we will get the below MSGBOX as follow
Things to Remember About VBA Function
- In VBA we have many built-in functions where it can be used in the automation process.
- While applying multiple MACROS function make sure that we have assigned a specific key to run it.
- Name each module in VBA so that to avoid confusion in coding.
Recommended Articles
This has been a guide to Excel VBA Function. Here we discuss how to use VBA Function in Excel along with practical examples and downloadable excel template. You can also go through our other suggested articles –