

{"id":100573,"date":"2021-10-04T09:00:08","date_gmt":"2021-10-04T03:30:08","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=100573"},"modified":"2021-10-04T12:43:31","modified_gmt":"2021-10-04T07:13:31","slug":"excel-vba-subroutine","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/","title":{"rendered":"Excel VBA Subroutine"},"content":{"rendered":"<h3>What is a Subroutine in VBA?<\/h3>\n<p>A code snippet which performs the specified task provided in the code and does not return a result. This is known as subroutine in vba. Subroutines are widely used to maintain the code simpler and to break the larger code into smaller modules for better management. There is also an advantage that the subroutines can be recalled anywhere in the program for multiple times also.<\/p>\n<p>For example, if the user has created an user interface for collecting the user input data, then the user can create a subroutine which clear the input value of the user in the text boxes. The call of subroutine is appropriate as the user does not want to return the results.<\/p>\n<h3>Why use Subroutines?<\/h3>\n<p><strong>1. Break code into small modules<\/strong><\/p>\n<p>A subroutine program can contain multiple thousand source code lines in it. The user can make use of subroutines and break down the program into smaller modules, which helps to decrease the complexity of the code.<\/p>\n<p><strong>2. Reusability of the code<\/strong><\/p>\n<p>Instead of writing the code every time, the user can call the subroutine program as per the requirement in the module. If the program contains the repetitive codes then the user can create a function or subroutine which helps in reusing the code at different modules.<\/p>\n<p><strong>3. Subroutines and functions are self-documenting<\/strong><\/p>\n<p>They are referred to as self documenting as the user can say what the program performs just by looking at the name of the sub routine.<\/p>\n<h3>Rules of naming Subroutines and Functions<\/h3>\n<p>Before naming the subroutines and functions, follow the rules:<\/p>\n<ul>\n<li>A letter or an underscore in the beginning of a name is a good start.<\/li>\n<li>Special characters or numerics such as 1, !, @, &amp;, ., # are not encouraged in the subroutine function name.<\/li>\n<li>The name of the sub routine should not be a reserved keyword as the compiler uses it for other specific tasks.<\/li>\n<li>There should not be blank spaces in the function name.<\/li>\n<\/ul>\n<p>The user will need to enable the Developer tab in Excel to work along in VBA.<\/p>\n<h3>How to Enable the Developer Tab?<\/h3>\n<p>To enable the developer tab in Excel:<\/p>\n<ul>\n<li>Under the file tab, choose the options from the left side of the panel.<\/li>\n<li>Select the customize ribbon label from the left side panel of the dialog box.<\/li>\n<li>Select the developer checkbox and finally press the OK button.<\/li>\n<\/ul>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/dev-tab.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102108\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/dev-tab.png\" alt=\"developer tab\" width=\"761\" height=\"605\" \/><\/a><\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Private Sub SubRoutine_Name (ByVal argument_1 As Integer, ByVal argument_2 As Integer)\r\n    \/\/code \r\nEnd Sub \r\n<\/pre>\n<p><strong>Syntax explanation<\/strong><\/p>\n<p><strong>Private Sub SubRoutine_Name(..):<\/strong><\/p>\n<p>Here the sub declares a subroutine named \u201cSubRoutine_Name\u201d and starts the body of the subroutine. To specify the scope of the subroutine, the keyword \u201cprivate\u201d is used.<\/p>\n<p><strong>ByVal argument_1 As Integer, ByVal argument_2 As Integer):<\/strong><\/p>\n<p>This declares two parameters of integer data type named argument_1 and argument_2<\/p>\n<p><strong>End Sub:<\/strong><\/p>\n<p>This line of code represents the end of the subroutine body.<br \/>\nNow lets see how to program and execute this Sub Procedure.<\/p>\n<h3>How to Call Sub in VBA?<\/h3>\n<p>The below steps are the processes on how to call sub in vba.<\/p>\n<p><strong>Step 1:<\/strong> Designing the user interface and setting up the properties as per the requirement.<\/p>\n<ul>\n<li>Design the user interface by setting the name properties to btnDisplayAddition and the caption to DataFlair_SubRoutine.<\/li>\n<\/ul>\n<p>Your GUI appears as follows<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/gui.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102109\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/gui.png\" alt=\"vba subroutine gui\" width=\"1060\" height=\"839\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/gui.png 1060w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/gui-768x608.png 768w\" sizes=\"auto, (max-width: 1060px) 100vw, 1060px\" \/><\/a><\/p>\n<p><strong>Step 2:<\/strong> Adding the subroutine module in the program<\/p>\n<ul>\n<li>By clicking on the view code option under the developer tab, the user can open the code window.<\/li>\n<li>Add the following subroutine code in the VBE editor<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Private Sub displayAddition(ByVal a As Integer, ByVal b As Integer)\r\nMsgBox a + b\r\nEnd Sub\r\n<\/pre>\n<p><strong>Code Explanation:<\/strong><br \/>\nPrivate Sub displayAddition &#8211; This declares a private subroutine \u201cDataFlair_SubRoutine\u201d that accepts two integer parameters.<\/p>\n<p>ByVal a As Integer, ByVal b As Integer &#8211; This declares two parameter variables and those are \u2018a\u2019 and \u2018b\u2019.<\/p>\n<p>MsgBox a + b &#8211; This calls the MsgBox built-in function to display the sum of the two variables which are passed as parameters.<\/p>\n<p><strong>Step 3:<\/strong> Address the client event code to button.<\/p>\n<ul>\n<li>By clicking on the view code option under the developer tab, the user can open the code window.<\/li>\n<\/ul>\n<p>Add the following code in the code editor for the click event of the btnDisplay_Addition command button.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Private Sub btnDisplay_Addition( )\r\n    displayAddition 10, 20\r\nEnd Sub\r\n<\/pre>\n<p>The first part of the code lines are subroutine code and the second part of the code lines are the click event code which calls the subroutine from the command button.<\/p>\n<p>Press the save button to save the changes and close the editor window.<\/p>\n<p><strong>Step 4:<\/strong> Application Testing for subroutine.<\/p>\n<p>To test the application, the user has to follow the steps:<\/p>\n<p>Step 1: Click on the Developer tab.<\/p>\n<p>Note: Ensure the design mode is off. If it is on, then the application will not respond.<\/p>\n<p>Step 2: Click on the Design Mode again to turn it off(if they were on).<\/p>\n<p>Step 3: Place the cursor on the newly created button.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/click-button.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102110\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/click-button.png\" alt=\"click button\" width=\"1160\" height=\"962\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/click-button.png 1160w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/click-button-768x637.png 768w\" sizes=\"auto, (max-width: 1160px) 100vw, 1160px\" \/><\/a><\/p>\n<p>The following output appears.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102111\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/output.png\" alt=\"test subroutine\" width=\"699\" height=\"443\" \/><\/a><\/p>\n<h3>Calling Procedures<\/h3>\n<p>The user can make a call from a function to invoke a procedure in the code. The user cannot do it in the same way they did it for function as subroutine does not return a value.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Function findSqr(Num As Double)\r\n  Num = 2\r\n  Sqr Num\r\n  MsgBox (Num)\r\nEnd Function\r\n\r\nPublic Sub Sqr(Num As Double)\r\nMsgBox (Num)\r\nEnd Sub\r\n<\/pre>\n<p><strong>Code Explanation:<\/strong> To calculate the square of a number, the \u201cSqr\u201d sub procedure is called.<\/p>\n<p>When the user runs the code, the following output appears.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/sqr-op.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102112\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/sqr-op.png\" alt=\"square output\" width=\"1920\" height=\"964\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/sqr-op.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/sqr-op-768x386.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/sqr-op-1536x771.png 1536w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<h3>VBA Call\/ Run Sub from another Subroutine:<\/h3>\n<p>Lets see how to call a sub procedure from within another sub procedure in VBA.<br \/>\nLet&#8217;s create two subroutines:<\/p>\n<h4>Subroutine-1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine1()\r\nDataFlair_Routine2\r\nEnd Sub\r\n<\/pre>\n<h4>Subroutine-2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine2()\r\n   MsgBox \"You have opened one of the new articles from the website!\"\r\nEnd Sub\r\n<\/pre>\n<p>Explanation: When we run the sub procedure of DataFlair_Routine1(), it calls the DataFlair_Routine2.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/routine-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102113\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/routine-output.png\" alt=\"vba routine output\" width=\"858\" height=\"439\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/routine-output.png 858w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/routine-output-768x393.png 768w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><\/a><\/p>\n<p>Hope you observed that the message box of the DataFlair_Routine2 function appears in the DataFlair_Routine1 function.<br \/>\nNote: The user can call multiple sub procedures from another sub procedure and there is no limit or restrictions for it.<\/p>\n<h3>Using the Call Statement:<\/h3>\n<p>The user can also use the \u2018Call\u2019 keyword in front of the procedure name. It makes the code easier to understand and it also has no effect on how the code runs and where the code is stored.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<h4>Subroutine-1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine1()\r\nCall DataFlair_Routine2\r\nEnd Sub\r\n<\/pre>\n<h4>Subroutine-2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine2()\r\n   MsgBox \"You have opened one of the new articles from the website!\"\r\nEnd Sub\r\n<\/pre>\n<p>Save and run the code, the following output appears:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/call-op.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102114\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/call-op.png\" alt=\"call output\" width=\"1396\" height=\"896\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/call-op.png 1396w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/call-op-768x493.png 768w\" sizes=\"auto, (max-width: 1396px) 100vw, 1396px\" \/><\/a><\/p>\n<p>Hope you observed that there is no effect even when we added the keyword \u2018Call\u2019 in front of the subroutine calling statement.<\/p>\n<h3>Calling a Subroutine with Arguments<\/h3>\n<p>The user can also call a subroutine with arguments.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<h4>Subroutine-1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine1()\r\nDataFlair_Routine2(\u201cAkkshaya\u201d)\r\nEnd Sub\r\n<\/pre>\n<h4>Subroutine-2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine2(username As String)\r\n   MsgBox \"You have opened one of the new articles from the website,\" &amp; \" &amp; username\"\r\nEnd Sub\r\n<\/pre>\n<p>Save and run the code, the following output appears:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/argument-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102115\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/argument-output.png\" alt=\"argument output\" width=\"1636\" height=\"925\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/argument-output.png 1636w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/argument-output-768x434.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/argument-output-1536x868.png 1536w\" sizes=\"auto, (max-width: 1636px) 100vw, 1636px\" \/><\/a><\/p>\n<p>Hope you observed that the user name is also shown in the message box.<\/p>\n<h3>Calling a Sub with Named Arguments<\/h3>\n<p>When the user names the arguments, then it\u2019s not necessary to pass the arguments in the same order to the subroutines.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<h4>SubRoutine &#8211; 1:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine1()\r\nDataFlair_Routine2 strInfo:=\"Have Fun reading it!\", username:=\"Akkshaya\"\r\nEnd Sub\r\n<\/pre>\n<h4>SubRoutine &#8211; 2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub DataFlair_Routine2(username As String, strInfo As String)\r\n   MsgBox \"You have opened one of the new articles from the website,\" &amp; \" &amp; username &amp; vbCrLf &amp; strInfo\"\r\nEnd Sub\r\n<\/pre>\n<p>Save and run the code, the following output appears:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/calling-a-sub-op.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102116\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/calling-a-sub-op.png\" alt=\"calling-a-sub-output\" width=\"815\" height=\"426\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/calling-a-sub-op.png 815w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/calling-a-sub-op-768x401.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><\/a><\/p>\n<p>Hope, you observed that the arguments were accepted even when they were not passed in order.<\/p>\n<h3>Sub in a Sub:<\/h3>\n<p>The user can use a sub to execute another sub also. Let\u2019s see a sample of it.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<p>A Sub can be used to execute another Sub:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">A Sub can be used to execute another Sub:\r\n   Sub sub_menu()\r\n        sub_def1\r\n        sub_def2\r\n    End Sub\r\n    Sub sub_def1()\r\n        MsgBox \"It is a sub which executes a sub\"\r\n    End Sub\r\n    Sub sub_def2()\r\n        MsgBox \"It is a sub executing this sub also\"\r\n    End Sub\r\n<\/pre>\n<p>Let\u2019s see a sample where the sub accepts arguments also.<br \/>\n<strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Sub sub_def()\r\n     sub_arg(100)\r\n End Sub\r\n Sub sub_arg(x as Integer)\r\n     MsgBox x\r\n End Sub\r\n<\/pre>\n<p>When the user runs the code, the following output appears.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/arg-sub.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102117\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/arg-sub.png\" alt=\"arg-sub\" width=\"961\" height=\"495\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/arg-sub.png 961w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/arg-sub-768x396.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><\/a><\/p>\n<h4>Multiple Arguments:<\/h4>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"> Sub sub_def()\r\n      Dim fruit As String\r\n      Dim cost As Integer\r\n\r\n      fruit= \u201cMango\u201d\r\n     Cost = 45\r\n     Call sub_argument (fruit, cost) \r\n  End Sub\r\n  Sub sub_argument(fruit as String, cost as Integer)\r\n      MsgBox(\u201cFruit cost is:\u201d,cost)\r\nEnd Sub\r\n<\/pre>\n<p><strong>Code Explanation:<\/strong> Here, the fruit and cost are two different arguments. When the user wants more than one argument into a sub, the user can use the call instruction before the name of the sub they want to call.<\/p>\n<p>When the user runs the code, the following output appears.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/fruit-cot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-102118\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/fruit-cot.png\" alt=\"fruit cost\" width=\"958\" height=\"496\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/fruit-cot.png 958w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/fruit-cot-768x398.png 768w\" sizes=\"auto, (max-width: 958px) 100vw, 958px\" \/><\/a><\/p>\n<p>Note: The user can execute a sub with multiple arguments by omitting the call instruction and the parentheses also.<\/p>\n<h3>Types of Sub<\/h3>\n<p>There are two types of sub available and they are Private and Public:<\/p>\n<ul>\n<li>Public Sub<\/li>\n<li>Private Sub<\/li>\n<\/ul>\n<h4>Public Sub:<\/h4>\n<p>The public sub creates a public procedure which an user can access by any procedure of any module and it is displayed in the macros.<\/p>\n<h4>Private Sub:<\/h4>\n<p>The private sub creates a private procedure which an user can access only by procedures of the same module and it is not displayed in the macros.<\/p>\n<p>Note: A sub which is not defined is considered as a public sub in VBA.<\/p>\n<h3>Key Differences between Excel sub and Function<\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Sub<\/b><\/td>\n<td><b>Function<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Sub performs a task but it does not provide an output value.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It performs a task and it also provides an output value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">It can be called anywhere and multiple times in the module.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">The functions in the module are referred to by a variable.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">We cannot use the sub as formulas in the spreadsheet directly.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">We can use the functions as formulas in the spreadsheet directly.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">The user must enter the values before to get the result of the sub.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">It can perform repetitive tasks and it can also return a value.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">VBA sub can be executed by the excel users.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">VBA functions cannot be executed by the excel users.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Pros of Excel VBA Call Sub<\/h3>\n<ul>\n<li>When the user uses subroutines in the code, it saves a lot of time by not repeating the same code over and over again.<\/li>\n<li>It stores in the same excel worksheet and thereby it reduces the size of the file as well.<\/li>\n<\/ul>\n<h3>Things to Remember<\/h3>\n<ul>\n<li>To see the sequential run of multiple modules of code, make use of the message box.<\/li>\n<li>Press F8 to compile the bigger codes as this may help the user to identify the error part.<\/li>\n<li>The file should be saved in the Macro Enabled Excel Format and this retains the written code.<\/li>\n<li>The Call keyword before the End statement will run the first code and the sub procedure will run the second code.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The user can execute the macro of another sub procedure without using the keyword \u201cCall\u201d also.<\/li>\n<li>Mentioning the name of the macro will be more than efficient.<\/li>\n<li>To make the code better and understandable, make use of the call keyword always.<\/li>\n<\/ul>\n<h3>Summary:<\/h3>\n<ul>\n<li>Subroutine performs a specific task without returning a value after execution of the code.<\/li>\n<li>Reusability of code and managing the code are easy if the user uses subroutine.<\/li>\n<li>It breaks down the larger code to smaller modules.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is a Subroutine in VBA? A code snippet which performs the specified task provided in the code and does not return a result. This is known as subroutine in vba. Subroutines are widely&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":102107,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24129],"tags":[25123,25121,25120,25122,25119],"class_list":["post-100573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ms-excel","tag-calling-subroutines-with-arguments","tag-types-of-sub-in-vba","tag-vba-sub","tag-vba-sub-vs-function","tag-vba-subroutine"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Subroutine - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is subroutine in Excel VBA, its need, Rules of naming Subroutines &amp; Functions, How to Call Sub, Types of Sub, Excel sub vs Function\" \/>\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\/excel-vba-subroutine\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Subroutine - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is subroutine in Excel VBA, its need, Rules of naming Subroutines &amp; Functions, How to Call Sub, Types of Sub, Excel sub vs Function\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/\" \/>\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-10-04T03:30:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-04T07:13:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/excel-vba-subroutine.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=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Subroutine - DataFlair","description":"Learn what is subroutine in Excel VBA, its need, Rules of naming Subroutines & Functions, How to Call Sub, Types of Sub, Excel sub vs Function","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\/excel-vba-subroutine\/","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Subroutine - DataFlair","og_description":"Learn what is subroutine in Excel VBA, its need, Rules of naming Subroutines & Functions, How to Call Sub, Types of Sub, Excel sub vs Function","og_url":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-10-04T03:30:08+00:00","article_modified_time":"2021-10-04T07:13:31+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/excel-vba-subroutine.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":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Excel VBA Subroutine","datePublished":"2021-10-04T03:30:08+00:00","dateModified":"2021-10-04T07:13:31+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/"},"wordCount":1641,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/excel-vba-subroutine.jpg","keywords":["calling subroutines with arguments","Types of Sub in VBA","VBA SUB","VBA Sub vs Function","VBA Subroutine"],"articleSection":["MS Excel Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/","url":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/","name":"Excel VBA Subroutine - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/excel-vba-subroutine.jpg","datePublished":"2021-10-04T03:30:08+00:00","dateModified":"2021-10-04T07:13:31+00:00","description":"Learn what is subroutine in Excel VBA, its need, Rules of naming Subroutines & Functions, How to Call Sub, Types of Sub, Excel sub vs Function","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/excel-vba-subroutine.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/excel-vba-subroutine.jpg","width":1200,"height":628,"caption":"excel vba subroutine"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/excel-vba-subroutine\/#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":"Excel VBA Subroutine"}]},{"@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\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100573","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=100573"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100573\/revisions"}],"predecessor-version":[{"id":102119,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100573\/revisions\/102119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/102107"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=100573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=100573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=100573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}