EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Tips VBA Function in Excel
Secondary Sidebar
VBA Tips
  • VBA Tips
    • VBA RGB
    • VBA Web Scraping
    • VBA AutoFill
    • VBA GoTo
    • VBA Color Index
    • VBA Wait
    • VBA Paste
    • VBA Copy Paste
    • VBA Remove Duplicates
    • VBA Sleep
    • VBA Font Color
    • VBA PowerPoint
    • VBA Borders
    • VBA Pivot Table
    • VBA Save As
    • VBA Tutorial For Beginners
    • VBA Charts
    • VBA Dictionary
    • VBA Conditional Formatting
    • VBA Paste Values
    • VBA Pause
    • VBA Refresh Pivot Table
    • VBA Macros
    • VBA Examples
    • Programming in Excel
    • VBA SendKeys
    • VBA Save Workbook
    • VBA PasteSpecial
    • VBA Function in Excel
    • VBA Visual Basic Applications
    • VBA Return

VBA Function in Excel

By Karthikeyan SubburamanKarthikeyan Subburaman

VBA Function in Excel

VBA Function in Excel (Table of Contents)

  • VBA Function in Excel
  • How to Use VBA Function in Excel?
  • Examples of VBA Function in Excel

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

VBA Function Step 1

  • Once we click on the option we will get the below dialogue box as follows.

VBA Function Step 2

  • Select Customize Ribbon option and we will get the below dialogue box as follows

VBA Function Step 3

  • 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

VBA Function Step 4

  • Once you check mark the “DEVELOPER” Tab it will get added in excel menu which is displayed as follows

VBA Function Step 5

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

VBA Function Step 6

  • 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

VBA Function Step 7

  • Now we can see that new module has been added as MODULE 1 on the left-hand side of the panel.

VBA Function Step 8

  • 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.

You can download this VBA Function Excel Template here – VBA Function Excel Template

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

VBA Example 1-1

  • Go to Insert Menu and select module

VBA Example 1-2

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.

VBA Example 1-3

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:

VBA Example 1-4

Now in the excel sheet, we can use the “addtwo” function to add the new number as:

VBA Example 1-5

The Result is:

VBA Example 1-6

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:

VBA Example 2-1

Hit the RUN button or Press F5 to execute the code

VBA Example 2-2

This coding will only return the current date in the mentioned cell name A1 and once we execute, the result will be shown as:

VBA Example 2-3

Example #3

VBA Message Box

Visual basic normally has two message box function

  1. The Msg Box method.
  2. 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.

VBA Example 3-1

Hit the RUN button or Press F5 to execute the code.

VBA Example 3-2

We will get the below output.

VBA Example 3-3

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.

VBA Example 4-1

Hit the RUN button or Press F5 to execute the code

VBA Example 4-2

After execution, we will get the below MSGBOX as follow

VBA Example 4-3

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 –

  1. VBA RGB
  2. VBA CDEC
  3. VBA XML
  4. VBA Month
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more