Vba
VBA Tutorial PDF: Excel Programming Course (FREE Download)
$20.20 $9.99 for today 4.6 (119 ratings) Key Highlights of VBA Tutorial PDF 85+ pages eBook...
A Subroutine in VBA is a piece of code that performs a specific task described in the code but does not return a result or a value. Subroutines are used to break down large pieces of code into small manageable parts. Subroutines can be recalled multiple times from anywhere in the program.
Let's say you have created a user interface with text boxes for accepting user input data. You can create a subroutine that clears the contents of the text boxes. A VBA Call Subroutine is appropriate in such a scenario because you do not want to return any results.
In this VBA tutorial, you will learn-
To use subroutines and functions, there are set of rules that one has to follow.
You will need to enable the Developer tab in Excel to follow along with this example. If you do not know how to enable the Developer tab then read the tutorial on VBA Operators
HERE in the syntax,
Private Sub mySubRoutine(ByVal arg1 As String, ByVal arg2 As String) 'do something End Sub
Syntax explanation
Code | Action |
---|---|
|
|
|
|
|
|
The following subroutine accepts the first and last name and displays them in a message box.
Now we are going to program and execute this Sub Procedure. Let see this.
Below is a step by step process on how to Call Sub in VBA:
Step 1) User Interface
Design the user interface as shown in the image below
Set the following properties. The properties that we are setting
S/N | Control | Property | Value |
---|---|---|---|
1 | CommandButton1 | Name | btnDisplayFullName |
2 | Caption | Fullname Subroutine |
Your interface should now look as follows
Step 2) Add subroutine
Private Sub displayFullName(ByVal firstName As String, ByVal lastName As String) MsgBox firstName & " " & lastName End Sub
HERE in the code,
Code | Actions |
---|---|
|
|
|
|
|
|
Step 3) Calling the subroutine from the command button click event.
Add the following code in code editor for the click event of btnDisplayFullName command button.
Private Sub btnDisplayFullName_Click() displayFullName "John", "Doe" End Sub
Your code window should now look as follows
Save the changes and close the code window.
Step 4) Testing the code
On the developer toolbar put the design mode 'off'. As shown below.
Step 5) Click on the command button 'FullName Subroutine'.
You will get the following results
Summary:
$20.20 $9.99 for today 4.6 (119 ratings) Key Highlights of VBA Tutorial PDF 85+ pages eBook...
What is Data Scraping? Data scraping is the technique that helps in the extraction of desired...
What is a Function? A function is a piece of code that performs a specific task and returns a...
Download PDF 1) Explain what is VBA or Visual Basic for Applications? VBA stands for Visual Basic...
What is an Excel Macro? Excel Macro is a record and playback tool that simply records your Excel...
What is VBA Range? The VBA Range Object represents a cell or multiple cells in your Excel...