Example #1
Look at the VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more below.
Code1:
Sub Procedure1()
Dim k As Integer
k = 50
Procedure2 k
MsgBox k
End Sub
Code2:
In the first procedure, we have declared the variable “k” as “Integer.”
Then we have assigned the value to this variable as 50.
After that, we added a new line i.e.
Procedure2 k
Here I have assigned the value of the variable “k” as
k = k + 10
We will run the code step by step by pressing the F8 key.
Press the F8 key two more times and place a cursor on variable “k” to see the value of the variable “k.”
Since we have assigned the value as 50, it shows the value as 50. Now, it has highlighted the line Procedure2 k, which is the second procedure name.
If we press the F8 key now, it will jump out of the current procedure and go to the second procedure.
Press the F8 key twice. It will go back to the previous subprocedure. If you notice, in the second procedure, we have applied the formula as k = k + 10. i.e., the “k” value is 50, then adds 10 more to that, i.e., 60 in total.
Now, the code is running in the first procedure, and in this procedure, the variable “k” value is 50. But press the F8 key and see the result in a message box.
We got the result of 60 instead of the default value of 50 in this procedure.
Here, the first variable “k” value is 50, and in the second procedure, the variable “k” value is k + 10, i.e., 60, carried to the first procedure.
Example #2
Take a look at one more example.
Code 1:
Code 2:
It works the same as the previous code.
Initially, the value of the variable “MyNumber” is 1. Then we call the procedure below by its name.
In that procedure, the value of the variable is 14.
So, when it returns to the previous procedure, it will assign the new value to the variable as 14.
Recommended Articles
- VBA Exit SubExcel VBA OperatorsInteger Data Type in VBAVBA Loop