

{"id":96490,"date":"2021-06-28T09:00:38","date_gmt":"2021-06-28T03:30:38","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=96490"},"modified":"2021-06-07T21:08:56","modified_gmt":"2021-06-07T15:38:56","slug":"vba-in-excel","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/","title":{"rendered":"VBA in Excel | How to create Visual Basic for Applications"},"content":{"rendered":"<h3>What is VBA in Excel?<\/h3>\n<p><span style=\"font-weight: 400;\">Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance office applications. VBA helps to develop automation processes and user-defined functions in Microsoft excel. It also helps in manipulating the user interface features of the applications.<\/span><\/p>\n<h3>What is a layman&#8217;s language in a computer?<\/h3>\n<p><span style=\"font-weight: 400;\">To perform an activity on the computer, you have to tell the commands to the computer and these commands are said in layman\u2019s language. Layman\u2019s language is very simple and easy to understand.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Computer programming is a set of actions we tell the computer to do. You can also use english statements to tell the computer what to do and these statements fall in the category of high level languages. VBA is a high-level language that is used to develop powerful excel programming.<\/span><\/p>\n<h3>Why VBA?<\/h3>\n<p><span style=\"font-weight: 400;\">In VBA, we can write the instructions in English for creating the applications. VBA is one of the programming languages which is easy to learn and it also has an user interface which is user friendly in such a way that the user can drag and drop the interface controls.\u00a0<\/span><\/p>\n<h3>Applications of VBA<\/h3>\n<p><span style=\"font-weight: 400;\">MS-Excel itself provides a lot of inbuilt functions but they provide only the essential and common functions. Those functions would not be the solution to our complex calculations. To overcome this, we use VBA as it is one of the most transparent solutions.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For example, saving each worksheet as a pdf seems to be a tiresome task. But, it\u2019s easy to do if we use VBA.<\/span><\/p>\n<h3>VBA Terminologies<\/h3>\n<h4>1. Modules<\/h4>\n<p><span style=\"font-weight: 400;\">The place where the user writes the code is called modules.<\/span><\/p>\n<p><b>To insert a module, follow the step:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Go to insert and click on the module. Once this is done, the window creates\u00a0 \u2018module1\u2019. Inside a module, a user can write the VBA code with procedures followed. The procedure is the series of VBA statements instructing what to do.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96919\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules.png\" alt=\"Insert Module in VBA\" width=\"1088\" height=\"724\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules.png 1088w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-300x200.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-1024x681.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-150x100.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-768x511.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-720x479.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-520x346.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-320x213.png 320w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/modules-272x182.png 272w\" sizes=\"auto, (max-width: 1088px) 100vw, 1088px\" \/><\/a><\/p>\n<h4>2. Procedure<\/h4>\n<p><span style=\"font-weight: 400;\">Group of statements which instructs Excel, what and how to perform are procedures.\u00a0 The task can either be simple or complex and it all depends on the procedure. The good practise is to keep the code simple and small. Sub and Function are two main types of procedures.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96920\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure.png\" alt=\"procedure in VBA\" width=\"1585\" height=\"409\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure.png 1585w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-300x77.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-1024x264.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-150x39.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-768x198.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-1536x396.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-720x186.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-520x134.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/procedure-320x83.png 320w\" sizes=\"auto, (max-width: 1585px) 100vw, 1585px\" \/><\/a><\/p>\n<h5>a. Function<\/h5>\n<p><span style=\"font-weight: 400;\">A group of statements which is reusable and can be called anywhere in the program is known as function. This completely saves the time by eliminating the need of writing the same logic as code again and again. This widely helps the programmers to split the big programs into small and simpler functions. The user can write user-defined functions and those statements are segmented between Function and End Function.<\/span><\/p>\n<h5>b. Sub-Procedures<\/h5>\n<p><span style=\"font-weight: 400;\">Sub-procedures perform the work more like functions. The function can or cannot return a value and the sub-procedures do not return a value. They are also called even without a call keyword and they are written between the sub and end substatements in the code editor.\u00a0<\/span><\/p>\n<h4>3. Comments<\/h4>\n<p><span style=\"font-weight: 400;\">The user comments in the program for user information and better understanding. It includes information about the programming and the author of the program. Comments are ignored while executing the program.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">There are two ways to insert comments in the code:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Any statement which starts with a single quote(\u2018) <\/span><\/li>\n<li>Any statement which starts with the keyword \u201cREM\u201d<\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96921\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts.png\" alt=\"Comments in VBA\" width=\"872\" height=\"568\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts.png 872w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts-300x195.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts-150x98.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts-768x500.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts-720x469.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts-520x339.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/cmnts-320x208.png 320w\" sizes=\"auto, (max-width: 872px) 100vw, 872px\" \/><\/a><\/p>\n<h3>Introduction to Visual Basic for Applications<\/h3>\n<p><span style=\"font-weight: 400;\">Let&#8217;s get into the basics of VBA:<\/span><\/p>\n<h4><span style=\"font-weight: 400;\">1. Variable<\/span><\/h4>\n<p><span style=\"font-weight: 400;\">Declaration of variables are required for memory locations.\u00a0<\/span><\/p>\n<p><b>Rules for creating variables:<\/b><\/p>\n<p><span style=\"font-weight: 400;\">a. No spaces between the variable name:\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">A variable name could not contain space in between the words. So, if you want to create a variable such as cost price, then you have to type the variable name as cost_price.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">b. Use a meaningful name:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Meaningful names makes your code easy to read and it looks more professional.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">c. Avoid using reserved words:\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Reserved words have special meaning in excel and hence it is not accessible as variable names. Example: lecturer<\/span><\/p>\n<h4>2. Arithmetic operators in VBA<\/h4>\n<p><span style=\"font-weight: 400;\">The BODMAS rule applies here also, so consider it while working with the expressions using multiple different arithmetic operators. The types of operators are:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">+<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Addition<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Subtraction<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">*<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Multiplication<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Division<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3. Logical operators in VBA<\/h4>\n<p><span style=\"font-weight: 400;\">Logical operators are also considered and they can also be included in the VBA code. They are:<\/span><\/p>\n<p>a. If condition<\/p>\n<p>b. OR condition<\/p>\n<p>c. NOT condition<\/p>\n<p>d. AND condition<\/p>\n<p>e. TRUE condition<\/p>\n<p>f. FALSE condition<\/p>\n<h3>VBA Editor<\/h3>\n<p><span style=\"font-weight: 400;\">To work with VBA in Excel, the user should know about the Visual Basic Editor also. The Visual Basic Editor is also known as VBA Editor, VB Editor, VBE EDITOR.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Visual Basic for Applications editor is a powerful tool and it is a separate application. It opens up whenever the user opens the workbook and by default, it\u2019s hidden and so the user has to activate it to access it. VBA Editor is an interface for writing scripts.<\/span><\/p>\n<h4>Accessing the VBA Editor:<\/h4>\n<p><span style=\"font-weight: 400;\">We can access the VBA editor using 3 methods.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">a. Using a Keyboard Shortcut<\/span><\/p>\n<p><span style=\"font-weight: 400;\">b. Enabling the developer tab<\/span><\/p>\n<p><span style=\"font-weight: 400;\">c. Enabling from worksheet tab<\/span><\/p>\n<h5>a. Using keyboard shortcut<\/h5>\n<p><span style=\"font-weight: 400;\">It is one of the easiest and simplest methods to access the VBA editor.<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Press Alt+F11 key to open a separate window for the VBA editor.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">When you press again, it takes you back to the spreadsheet.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\"><strong>NOTE<\/strong>: The shortcut key for mac workbooks is either Opt + F11 or Fn + Opt + F11.<\/span><\/p>\n<h5>b. Enabling the Developer Tab<\/h5>\n<p><span style=\"font-weight: 400;\">To enable the developer tab in Excel, follow the below steps:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Click on the file tab and choose options from the left side of the panel.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Click on the customize ribbon label from the left side panel of the dialog box.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Select the developer checkbox and finally click the OK button.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96922\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box.png\" alt=\"developer box in VBA\" width=\"1085\" height=\"864\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box.png 1085w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-300x239.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-1024x815.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-150x119.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-768x612.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-720x573.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-520x414.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/developer-box-320x255.png 320w\" sizes=\"auto, (max-width: 1085px) 100vw, 1085px\" \/><\/a><\/p>\n<h5>c. Enabling from worksheet tab<\/h5>\n<p><span style=\"font-weight: 400;\">We can view the worksheet\u2019s code from the sheet tab also. In order to do that, right-click on the worksheet and choose the view code option.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96923\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab.png\" alt=\"view code from worksheet tab in VBA\" width=\"654\" height=\"365\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab.png 654w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab-300x167.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab-150x84.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab-520x290.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/view-code-from-worksheet-tab-320x179.png 320w\" sizes=\"auto, (max-width: 654px) 100vw, 654px\" \/><\/a><\/p>\n<h3>VBA \u2013 My First Program<\/h3>\n<p><span style=\"font-weight: 400;\">Now we will see how to program in VBA programming language. All the programs in VBA start with &#8220;Sub&#8221; and end with &#8220;End sub&#8221;. Here, the sub stands for a subroutine and the name is what you have to assign to your program.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub name()\r\n**write your program **\r\nEnd Sub<\/pre>\n<h4>VBA from Excel:<\/h4>\n<p><span style=\"font-weight: 400;\"><strong>Step &#8211; 1<\/strong>: Go to the Developer tab and click on the visual basic icon.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96924\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic.png\" alt=\"visual basic\" width=\"1029\" height=\"148\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic.png 1029w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-300x43.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-1024x147.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-150x22.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-768x110.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-720x104.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-520x75.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/visual-basic-320x46.png 320w\" sizes=\"auto, (max-width: 1029px) 100vw, 1029px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step &#8211; 2<\/strong>: The VBA editor opens up and from there you can select the Excel sheet where you want to run the code. To open a particular sheet VBA editor, double click on that worksheet.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It will open a blank VBA editor on the right side of the dialog window.<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96925\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor.png\" alt=\"vba editor\" width=\"931\" height=\"365\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor.png 931w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor-300x118.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor-150x59.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor-768x301.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor-720x282.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor-520x204.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/vba-editor-320x125.png 320w\" sizes=\"auto, (max-width: 931px) 100vw, 931px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step &#8211; 3<\/strong>: To read and display a program, we need an object in a messagebox.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">a. Write &#8220;Sub&#8221; and then the &#8220;program name&#8221; i.e., DataFlair here.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">b. Write a message to display it in the MsgBox\u00a0 i.e., Welcome to DataFlair!<\/span><\/p>\n<p><span style=\"font-weight: 400;\">c. End the program by End Sub.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step &#8211; 4<\/strong>: Run this code by clicking on the run button on the top of the editor menu.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96926\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button.png\" alt=\"run button in VBA\" width=\"1326\" height=\"261\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button.png 1326w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-300x59.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-1024x202.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-150x30.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-768x151.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-1320x261.png 1320w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-720x142.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-520x102.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/run-button-320x63.png 320w\" sizes=\"auto, (max-width: 1326px) 100vw, 1326px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step &#8211; 5:<\/strong> When you run the code, another window pops out. Here, select the worksheet where you want to display the message and click on the &#8220;Run&#8221; button from the dialog box.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96927\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair.png\" alt=\"execution of dataflair\" width=\"864\" height=\"551\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair.png 864w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair-300x191.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair-150x96.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair-768x490.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair-720x459.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair-520x332.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/execution-of-dataflair-320x204.png 320w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step &#8211; 6:<\/strong> The program will get executed and the message displays in a messagebox.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-in-excel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96929\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-in-excel.png\" alt=\"VBA in Excel\" width=\"512\" height=\"375\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-in-excel.png 512w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-in-excel-300x220.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-in-excel-150x110.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-in-excel-320x234.png 320w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>Input Box<\/h4>\n<p><span style=\"font-weight: 400;\">The user can enter the values in the input box. After entering the values, if the user clicks on the OK button, the text in the text box will be returned. The user can also press enter to return the value in the text box or if the user clicks on the cancel button, then the function returns an empty string(\u201c\u201d).\u00a0\u00a0<\/span><\/p>\n<p><b>Syntax<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">InputBox( prompt[, title][, default][, xposition][,yposition][, helpfile, context])<\/pre>\n<p><b>Parameter Description<\/b><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Prompt<\/strong> \u2212This is one of the required parameters. This is actually a string which is displayed as a message in the message dialog box. The maximum length is approximately 1024 characters. If a message extends more than a line, then it can be separated by a carriage return character (Chr(13)) or a linefeed character (Chr(10)) .<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Title<\/strong> \u2212 This is one of the optional parameters. The title is a string expression and this expression appears as the title bar of the dialog box. The title considers the application name as the title if it is not provided.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Default<\/strong> \u2212 This is one of the optional parameters. This parameter helps to display the default text in the text box.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>xposition<\/strong> \u2212 This is one of the optional parameters. This represents the input box distance from the left side of the screen horizontally. If this parameter is not provided, the input box appears at the center.\u00a0\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>yposition<\/strong> \u2212 This is one of the optional parameters. This represents the input box distance from the left side of the screen vertically. If this parameter is not provided, the input box appears at the center.\u00a0\u00a0\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Helpfile<\/strong> \u2212 This is one of the optional parameters. The helpfile is a string expression which identifies the Help file to use for the dialog box.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>context<\/strong> \u2212 This is one of the optional parameters. The context is a numeric expression which identifies the assigned help context number to the appropriate help topic. If this parameter is provided then a helpfile must also be provided.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We will now see how to create a basic VBA program that displays an input box to ask for the user&#8217;s name and\u00a0 display a message<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Once, you have enabled the DEVELOPER tab in excel:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">O<\/span><span style=\"font-weight: 400;\">pen a new workbook.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Save it as excel macro enabled worksheet format *.xlsm<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Click on the DEVELOPER tab.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Click on INSERT from the ribbon.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Select a command button from the drop down menu.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96930\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1.png\" alt=\"Insert command in VBA\" width=\"991\" height=\"297\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1.png 991w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1-300x90.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1-150x45.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1-768x230.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1-720x216.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1-520x156.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/insert-1-320x96.png 320w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><\/a><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">\u00a0<\/span><span style=\"font-weight: 400;\">Draw the command button anywhere on the worksheet, a dialogue window appears.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Rename the macro name to DataFlair.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Click on the new button.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96931\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button.png\" alt=\"Assign Macro in VBA\" width=\"650\" height=\"511\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button.png 650w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button-300x236.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button-150x118.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button-520x409.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataflair-button-320x252.png 320w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/a><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">A VBA code window appears.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96932\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba.png\" alt=\"VBA Code Window\" width=\"1700\" height=\"821\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba.png 1700w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-300x145.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-1024x495.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-150x72.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-768x371.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-1536x742.png 1536w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-720x348.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-520x251.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/dataFlair-vba-320x155.png 320w\" sizes=\"auto, (max-width: 1700px) 100vw, 1700px\" \/><\/a><\/p>\n<p>Enter the following instruction codes:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Dim username As String\r\nusername = InputBox(\"Enter your name\")\r\nMsgBox \"Welcome to DataFlair,\u00a0 \" + username<\/pre>\n<p><span style=\"font-weight: 400;\">Here,<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">&#8220;Dim username as String&#8221; creates a variable called username.\u00a0<\/span><\/li>\n<li><span style=\"font-weight: 400;\">&#8220;username = InputBox(&#8220;Enter your name&#8221;)&#8221; . This builts an InputBox function which displays a window with the caption \u201cEnter your name\u201d.\u00a0<\/span><\/li>\n<li><span style=\"font-weight: 400;\">&#8220;MsgBox&#8221; holds the message, Welcome to DataFlair,\u00a0 &#8221; + username&#8221;\u00a0 and this function displays a message box which says Welcome to DataFlair, username.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Close the code window.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Edit the text by right clicking on the button.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96933\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey.png\" alt=\"renaming to hey\" width=\"558\" height=\"234\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey.png 558w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey-300x126.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey-150x63.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey-520x218.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/renaming-to-hey-320x134.png 320w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">The button is now renamed to hey and now, click on the button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">An input box appears and there enter your name\u00a0 i.e. Lexi here.<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96934\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi.png\" alt=\"Lexi in VBA\" width=\"877\" height=\"268\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi.png 877w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi-300x92.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi-150x46.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi-768x235.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi-720x220.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi-520x159.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/lexi-320x98.png 320w\" sizes=\"auto, (max-width: 877px) 100vw, 877px\" \/><\/a><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">You will get the following message box<\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-msg.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96928\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-msg.png\" alt=\"VBA \" width=\"412\" height=\"233\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-msg.png 412w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-msg-300x170.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-msg-150x85.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/welcome-msg-320x181.png 320w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/a><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Finally, now we know how to create a program in excel using vba.<\/span><\/li>\n<\/ul>\n<h4>VBA MsgBox<\/h4>\n<p><span style=\"font-weight: 400;\">A message box appears in the excel workbook and this function helps in creating a message box. The message box is created in such a way that it waits for the user to click on a button and the activity is performed according to the button the user clicks.\u00a0\u00a0<\/span><\/p>\n<p><b>Syntax<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">MsgBox (prompt [, buttons ][, title ][, helpfile, context ])<\/pre>\n<p><b>Parameter Description<\/b><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Prompt<\/strong> \u2212 This is one of the required parameters. This is actually a string which is displayed as a message in the message dialog box. The maximum length is approximately 1024 characters. If a message extends more than a line, then it can be separated by a carriage return character (Chr(13)) or a linefeed character (Chr(10)) .<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Buttons<\/strong> \u2212 This is one of the optional parameters. The buttons are created by the numeric expression and these expressions specify the type of buttons to display in the MsgBox, the icon style and the default button identity of the message box. The default value for button is 0.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Title<\/strong> \u2212 This is one of the optional parameters. The title is a string expression and this expression appears as the title bar of the dialog box. The title considers the application name as the title if it is not provided.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Helpfile<\/strong> \u2212\u00a0 This is one of the optional parameters. The helpfile is a string expression which identifies the Help file to use for the dialog box.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Context<\/strong> \u2212 This is one of the optional parameters. The context is a numeric expression which identifies the assigned help context number to the appropriate help topic. If this parameter is provided then a helpfile must also be provided.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The Buttons parameter can take any of the following values \u2212<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Button\u00a0<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbOKOnly\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This displays the \u2018OK\u2019 button.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbOKCancel<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This displays the \u2018Cancel\u2019 button.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbAbortRetryIgnore<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the \u2018Abort\u2019,\u00a0 \u2018Retry\u2019 and \u2018Ignore\u2019 button in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbYesNoCancel<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the \u2018Yes\u2019, \u2018No\u2019 and \u2018Cancel\u2019 buttons in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbYesNo<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the \u2018Yes\u2019 and \u2018No\u2019 buttons in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbRetryCancel<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the \u2018retry\u2019 and \u2018cancel\u2019 buttons in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbCritical<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the critical icon in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbQuestion<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the query icon in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbExclamation<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the exclamation icon in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbInformation<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command displays the information icon in the message box.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbDefaultButton1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command makes the first button as the default button.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbDefaultButton2<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command makes the second button as the default button.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbDefaultButton3<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command makes the third button as the default button.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbDefaultButton4<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This command makes the fourth button as the default button.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbApplicationModal Application modal<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This button function will not make the current application work until the user clicks on any of the buttons in the message box.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">vbSystemModal System modal<\/span><\/td>\n<td><span style=\"font-weight: 400;\">This button function will not make any other application work until the user clicks on any of the buttons in the message box.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Return Values in VBA<\/h3>\n<p><span style=\"font-weight: 400;\">To know which button the user has clicked, the msg box function returns few values and those values are:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\"><strong>vbOK<\/strong> &#8211; The user clicked the \u2018OK\u2019 button.\u00a0<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>vbCancel<\/strong> &#8211; The user clicked the \u2018Cancel\u2019 button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>vbAbort<\/strong> &#8211; The user clicked the \u2018Abort\u2019 button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>vbRetry<\/strong> &#8211; The user clicked the \u2018Retry\u2019 button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>vbIgnore<\/strong> &#8211; The user clicked the \u2018Ignore\u2019 button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>vbYes<\/strong> &#8211; The user clicked the \u2018Yes\u2019 button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\"><strong>vbNo<\/strong> &#8211; The user clicked the \u2018No\u2019 button.<\/span><\/li>\n<\/ul>\n<p><b>To create a message box, follow the steps:<\/b><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 1<\/strong>: Open an excel workbook and save it as an excel macro-enabled workbook.\u00a0<\/span><\/p>\n<p><strong>Step 2: <\/strong><b>\u00a0<\/b><span style=\"font-weight: 400;\">Add a button using macros.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In order to create a macro,\u00a0<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Go to the developer tab and click on insert from the ribbon.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Choose the command button.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Assign a macro by providing a name and click on the new button.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\"><strong>Step 3:<\/strong> Finally, press ok\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 4:<\/strong> Once the code window appears, type the following code.\u00a0\u00a0<\/span><\/p>\n<p><b>Message Box Code:<\/b><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Click()\r\n\u00a0\u00a0\u00a0MsgBox (\"Welcome to DataFlair\")\r\nEnd Sub<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 5<\/strong>: Save the code.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Once you click on the button, the following output appears.<\/span><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-96935\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox.png\" alt=\"Message box in VBA\" width=\"847\" height=\"356\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox.png 847w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox-300x126.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox-150x63.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox-768x323.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox-720x303.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox-520x219.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/Mesagebox-320x134.png 320w\" sizes=\"auto, (max-width: 847px) 100vw, 847px\" \/><\/a><\/p>\n<p><span style=\"font-weight: 400;\">A message box displays a message, &#8220;Welcome to DataFlair &#8221; with an &#8220;OK&#8221; Button.\u00a0<\/span><\/p>\n<h3>Personal &amp; Business Applications of VBA in Excel<\/h3>\n<p><span style=\"font-weight: 400;\">Macros will automate most of the routine tasks and it makes the work much simpler. Macros are widely used in personal and as well as business applications. The user can create powerful programs in excel using VBA for business purposes.<\/span><\/p>\n<h3>Summary<\/h3>\n<p><span style=\"font-weight: 400;\">1. VBA stands for Visual Basic for Application.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2. It is a programming language used to create applications in excel.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3. VBA makes your excel sheet more powerful and consumes less amount of time if you have repetitive tasks to be done everyday.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4. VBA can be used for personal and as well as business purposes.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is VBA in Excel? Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance office applications. VBA helps to develop automation processes and user-defined functions in Microsoft excel. It&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":96917,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24129],"tags":[24559,24560],"class_list":["post-96490","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ms-excel","tag-vba-in-excel","tag-visual-basic-applications-in-excel"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>VBA in Excel | How to create Visual Basic for Applications - DataFlair<\/title>\n<meta name=\"description\" content=\"Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance the office applications. Learn more about it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/vba-in-excel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VBA in Excel | How to create Visual Basic for Applications - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance the office applications. Learn more about it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/vba-in-excel\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-28T03:30:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/How-to-Create-Visual-Basic-for-Applications-VBA-in-Excel.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"VBA in Excel | How to create Visual Basic for Applications - DataFlair","description":"Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance the office applications. Learn more about it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/","og_locale":"en_US","og_type":"article","og_title":"VBA in Excel | How to create Visual Basic for Applications - DataFlair","og_description":"Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance the office applications. Learn more about it.","og_url":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-06-28T03:30:38+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/How-to-Create-Visual-Basic-for-Applications-VBA-in-Excel.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"VBA in Excel | How to create Visual Basic for Applications","datePublished":"2021-06-28T03:30:38+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/"},"wordCount":2509,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/How-to-Create-Visual-Basic-for-Applications-VBA-in-Excel.jpg","keywords":["VBA in Excel","Visual Basic Applications in Excel"],"articleSection":["MS Excel Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/vba-in-excel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/","url":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/","name":"VBA in Excel | How to create Visual Basic for Applications - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/How-to-Create-Visual-Basic-for-Applications-VBA-in-Excel.jpg","datePublished":"2021-06-28T03:30:38+00:00","description":"Visual Basic for Applications (VBA) is a programming language developed by Microsoft to enhance the office applications. Learn more about it.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/vba-in-excel\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/How-to-Create-Visual-Basic-for-Applications-VBA-in-Excel.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/06\/How-to-Create-Visual-Basic-for-Applications-VBA-in-Excel.jpg","width":1200,"height":628,"caption":"VBA in Excel"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/vba-in-excel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"MS Excel Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/ms-excel\/"},{"@type":"ListItem","position":3,"name":"VBA in Excel | How to create Visual Basic for Applications"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=96490"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96490\/revisions"}],"predecessor-version":[{"id":96936,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/96490\/revisions\/96936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/96917"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=96490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=96490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=96490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}