LOOPS IN VBA
INTRODUCTION
If you have programmed ever, you must know the importance of the loops.
If you are new to programming, this information is for you. Looping is the process of repeating a particular action to a fixed number of times.
This is a way of controlling the execution. In programming the code is compiled line by line but if we want to control it such as , leading the control to some specific part of the program, or executing any statement multiple times. VBA provide us many such options to control our execution as per our need.
Loops are one of the component which is going to be used a lot while doing any check or filling any values.
Whenever we check or compare any value, we need to use the loop so that a Checking statement compares a particular value with the other present values. After finding the correct value, we need to put the value through the function.
Now if we need only one value, we can end the loop or if we need other values or all the values fulfilling the conditions, we can again start the loop. This particular operation we can’t do in Excel directly.
Thanks to VBA for that.
Let us have a look at the different types of loops available for us in VBA.
TYPES OF LOOPS IN VBA
Mainly VBA supports many types of loops. Here is the list
- For-Next Loop
- Do-While Loop
- Do-Until Loop
FOR-NEXT LOOP
FOR variable=starting_value TO final_value
INSTRUCTIONS/STATEMENTS
EXIT FOR 'If some condition is given for exit
INSTRUCTIONS/STATEMENTS
NEXT variable
EXPLANATION OF THE SYNTAX
starting_value is the starting value given to the variable
SIMPLE EXAMPLE FOR-NEXT LOOP
Let us create a very simple FOR NEXT loop which will sum the digits upto the given number of terms.
WE’LL FIND THE SUM OF SERIES 1+2+…+12 USING THE LOOPS
CODE:
'gyankosh.net
'EXAMPLE 1 SIMPLE LOOP
' PROCEDURE TO FIND THE SUM OF 1+2+....+12
Sub Example1()
Dim i As Integer 'variable for counter
Dim temp As Integer 'temp variable for holding cumulative value
temp = 0 'counter started
For i = 1 To 12
temp = temp + i
Next i 'incrementing the counter
MsgBox ("The sum of the series 1+2+...+12 is " & temp) 'output
End Sub
EXPLANATION-FOR NEXT LOOP EXAMPLE 1
The comments are already given for the simple understanding. Here is a brief description.
The example starts by defining i as integer and one more integer TEMP. We need temp for keeping the cumulative sum of the loop.
The loop starts with i=1 value , goes inside, transfers its value to temp and increments to 2. Again goes inside the loop transfers its value to temp. Now remember that temp already had 1 last time. So this time it’ll add 2 to 1 i.e. temp at this time is 3 and similarly the loop will work up to i=12
Message box will show the result as 78. Look below at the running program.
PUTTIN AN CUSTOM INCREMENT in for next loop
We saw in the last example that when we typed NEXT , the counter went up by 1.
But its not that we can’t change the increment level. A keyword named STEP can be used to fix the increment in the FOR LOOP. This example will describe the FOR LOOP WITH STEP.
Let us create a FOR LOOP WITH STEP to find the sum of this series.
WE’LL FIND THE SUM OF SERIES 2+…+14 USING THE LOOP.
CODE:
'gyankosh.net
'EXAMPLE 2 SIMPLE LOOP WITH CUSTOM INCREMENT
' PROCEDURE TO FIND THE SUM OF 2+4+....+14
Sub Example2()
Dim i As Integer 'variable for counter
Dim temp As Integer 'temp variable for holding cumulative value
temp = 0 'counter started
For i = 2 To 14 Step 2 'increment has been set to 2. It can be negative also.
temp = temp + i
Next i 'incrementing the counter
MsgBox ("The sum of the series 2+...+14 is " & temp) 'output
End Sub
EXPLANATION-FOR NEXT LOOP EXAMPLE 2
The comments are already given for the simple understanding. Here is a brief description.
USE OF EXIT DURING FOR NEXT LOOP
You must remember that in the standard format of FOR NEXT loop , we had mentioned EXIT statement too. So now let us test the exit statement. We’ll apply this to the same series of Example 2. The condition is if the value of temp comes out to be 12 at any instant, it should break the loop immediately.
Let us create a loop and put IF STATEMENT in between for the loop to exit.
WE’LL FIND THE SUM OF SERIES 2+…+14 USING THE LOOP BUT THE LOOP SHOULD EXIT IF THE SUM OF SERIES COMES TO BE 14.
CODE:
'gyankosh.net
'EXAMPLE 2 SIMPLE LOOP WITH CUSTOM INCREMENT
' PROCEDURE TO FIND THE SUM OF 2+4+....+14
Sub Example2()
Dim i As Integer 'variable for counter
Dim temp As Integer 'temp variable for holding cumulative value
temp = 0 'counter started
For i = 2 To 14 Step 2 'increment has been set to 2. It can be negative also.
temp = temp + i
Next i 'incrementing the counter
MsgBox ("The sum of the series 2+...+14 is " & temp) 'output
End Sub
EXPLANATION-FOR NEXT LOOP EXAMPLE 3
The comments are already given for the simple understanding. Here is a brief description.
The two variables i and temp (for holding the value) are declared in program.
The loop starts with a STEP of 2 to increment the value of i at the interval of 2.
An instruction has been put inside the loop
IF TEMP=12 THEN EXIT FOR
This instruction will compare the value of temp with 12 and if it comes out to be 12, it’ll go for EXIT FOR and immediately terminates the loop and go to the statement after NEXT statement.
The result comes out to be 12 as after that the code didn’t let the loop to play.
DO-WHILE LOOP
Do [While condition]
[instructions]
[Exit Do]
[instructions]
Loop
-OR-
Do
[instructions]
[Exit Do]
[instructions]
Loop [While condition]
*Exit loop is only needed if there is any condition .
EXPLANATION OF THE SYNTAX
SIMPLE EXAMPLE FOR-NEXT LOOP
Let us create an example to find out the previous character of the first space.
Suppose we take the text “what else” .
We need the information till which letter, the loop did perform and when did it come out of the loop. Simply the previous character of the first space.
'gyankosh.net
'Do While Exmple
Sub DoWhileExample1()
Dim a As String
a = "what else"
i = 1
Do While Mid(a, i, 1) <> " "
i = i + 1
Loop
MsgBox ("The loop executed till " & Mid(a, i - 1, 1))
End Sub
EXPLANATION-DO-WHILE LOOP EXAMPLE 1
The comments are already given for the simple understanding. Here is a brief description.
DO-UNTIL LOOP
Do [UNTIL condition]
[instructions]
[Exit Do]
[instructions]
Loop
-OR-
Do
[instructions]
[Exit Do]
[instructions]
Loop [UNTIL condition]
*Exit loop is only needed if there is any condition .
EXPLANATION OF THE SYNTAX
SIMPLE EXAMPLE DO-UNTIL LOOP
'gyankosh.net
'Do Until Exmple
Sub DoUntilExample1()
Dim a As String 'variable declarations
a = "Hi Welcome to Gyankosh"
i = 1
j = 0
Do Until i > Len(a)
If Mid(a, i, 1) <> " " Then
j = j + 1
End If
i = i + 1
Loop
MsgBox (" The exact number of characters in the given text is " & j)
End Sub