Sub Procedure in Excel VBA
Last Updated :
16 Nov, 2022
A Sub Procedure is an action that is written in VBA code. It starts with a Sub statement and ends with an End Sub statement. It is used to manipulate the data in an Excel sheet which includes a table present in an Excel sheet or a pivot table present in the Excel sheet. The name of the procedure should be followed by parentheses. Let's learn why to use sub procedure:
- Converts large piece of codes into small parts so that the computer ignores all kind of complexities that arises because of large codes
- In a program, we have to access the database frequently. In this case, instead of writing the code again and again we can simply create a function to access the database. Reusability of code can be done.
Naming Rules of Sub Procedure
- It can start with a letter or an underscore but it cannot start with a number or a special character.
- It cannot contain any space in the name.
- The name of the subprocedure cannot be a keyword like Private, Sub, End, etc.
Syntax
Sub name_procedure ()
End Sub
Let's discuss different ways to create Sub Procedure:
Creating a Sub Procedure with Macro
Step 1: Select View in the Ribbon then click on Macros to select Record Macro
Step 2: Change the Macro name according to the user's need and then select where we want to store the macro, currently, we are storing it in This Workbook.
Step 3: To view or run the macro press Alt+F8 and then click on Edit to open the VBA to write Sub Procedure.
Then VBA Window will appear with a Sub Procedure name of Test_Procedure
Creating a Sub Procedure in VBA window
Step 1: Press Alt+F11 in the main Excel window to the VBE window.
Step 2: Select Insert and then click on Procedure.
Step 3: Add Procedure dialog box will appear.
- We can set the Sub Procedure name in the Name box, here the name of the Sub Procedure is Test_Procedure.
- We have to select Sub in the Type to create a Sub Procedure.
- We can select the scope of the procedure either Public or Private. The public will allow us to use a procedure in all the modules in the workbook whereas the private will not allow using a sub-procedure of one module in another module
- If we want to make our local variables public so they will be accessible to the entire module then select the check box of All Local variables as Statics
Step 4: Click on Ok to get Sub Procedure.
Creating a Button to Run Macro
Step 1: Select the developer tab and click Insert drop-down list from the control box.
Step 2: Choose the command button from the Form Controls Box.
Step 3: Draw the command button in the excel sheet then an Assign Macro dialog box will appear where we can select the macro to assign the Button.
Step 4: Click on the Button to run the macro.
Step 5: Press "Alt+F11" to get the sub-procedure of Button1_Click().
Step 6: Subroutine will be created inside the sub-procedure. The following code will insert a value in the Range "B1".
Types of VBA Subroutine
There are two types in Sub procedures:
- Private Sub procedure: The procedure can't be accessed by other modules in the workbook.
- Public Sub procedure: A procedure of one module can be accessed by the procedure of another module in the workbook. In the following code, a Sub procedure Act1() is present in Module 1.
Act1() can be called by another Sub procedure Act2() which is present in Module 2.
Similar Reads
VBA Strings in Excel In Excel's Visual Basic for Applications(VBA), strings are pivotal in handling and manipulating text-based data. Strings serve as a fundamental data type used to store a sequence of characters, enabling the representation of textual information, numbers, symbols, and more. Understanding how VBA hand
8 min read
MessageBox in Excel VBA Message Box is one of the most frequently used functions in VBA Macro. The message box is a dialogue box that appears on the screen as an extra window. The dialogue box that appeared asks input from the user and returns a constant/code/number accordingly or it can use to display messages. In this ar
8 min read
Basic Object Model in Excel VBA VBA stands for visual basic for application. Excel VBA is an object-based programming language, it is used while recording a macro i.e., it uses the idea of Encapsulation and stores the state (data) and operation (functions) inside the object. Excel objects are arranged in a hierarchy that governs t
3 min read
VBA Print Statement in Excel When there are many columns and rows in excel but we only want to save or print a few of them because that few rows and columns are important to us, at that case we will print them in Excel VBA so that we can fetch those details later and also to get the print out of that table because daily life ha
2 min read
Function and Sub in Excel VBA In Visual Basic, the functions and sub-procedures play similar roles but have different or unique characteristics. However, both perform a programmed task. They utilize a set or group of commands to deliver the required results. The key difference between the sub and the functions is that a sub-proc
4 min read