Vba
VBA Logical Operators: AND, OR, NOT, IF NOT in Excel VBA
VBA Logical Operators: AND, OR, NOT Excel VBA Logical Operators Let's say you want to process a...
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 SubSyntax 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:
VBA Logical Operators: AND, OR, NOT Excel VBA Logical Operators Let's say you want to process a...
VBA String Operators String data is used to hold data that is made up of numbers, characters, and...
Creating VBA Form/GUI controls in Excel GUI is the acronym for Graphical User Interface. The GUI...
Variables are used in almost all computer program and VBA is no different. It's a good practice to...
What is Data Scraping? Data scraping is the technique that helps in the extraction of desired...
Download PDF Following are frequently asked questions in interviews for freshers as well...