0% found this document useful (0 votes)
32 views6 pages

For Loop: Sub Example1

1. The document contains examples of For loops in VBA with explanations and exercises. 2. Example 1 uses a For loop to iterate from 1 to 4 and assign the counter variable i to cells on the same row. 3. Example 2 uses a For loop to iterate from 10 to 1 in decrements of 2 and assign the counter variable i to cells. 4. Example 3 uses a For loop to sum the numbers from 1 to 5 and store the result.

Uploaded by

Zahraa Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

For Loop: Sub Example1

1. The document contains examples of For loops in VBA with explanations and exercises. 2. Example 1 uses a For loop to iterate from 1 to 4 and assign the counter variable i to cells on the same row. 3. Example 2 uses a For loop to iterate from 10 to 1 in decrements of 2 and assign the counter variable i to cells. 4. Example 3 uses a For loop to sum the numbers from 1 to 5 and store the result.

Uploaded by

Zahraa Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

For Loop

Sub Example1()

For i = 1 To 4 1
2
5 3
Cells(i, i) = I
4

Next i

Range("A3")= i

End Sub
Sub Example2()

1 11
For i = 10 To 1 Step -2
3
Cells(i, 1) = i
5
Next i
7

Range("C1")= i
9

End Sub
Sub Example3()

15
Sum=0

For i = 1 To 5

Sum = Sum + i

Next i

Range("A1")= Sum

End Sub
Exercise: Use for loop solve the following questions and
place the result in one of the cells
1) Find the summation of the even numbers between 2
and 100.
(Sol= 2550)
2) Find the value of W from the Equation
𝑊 = 1 × 3 × 5 × 7 × 9 × 11 × 13
(Sol= 135135)
3) Find The value of W from the Equation
1 1 1 1 1 1 1
𝑆 =1+ + + + + + +
2 4 8 16 32 64 128
(Sol= 1.99)
1) Find the summation of the even numbers between 2 and 100.
(Sol= 2550)
Sub Ex1()
Sum=0
For i = 2 To 100 Step 2

Sum = Sum + i

Next i
Range("A1") = Sum
End Sub

2) Find the value of W from the Equation


𝑊 = 1 × 3 × 5 × 7 × 9 × 11 × 13
(Sol= 135135)
Sub Ex2()
W=1
For i = 1 To 13 Step 2
W=W*i
Next i
Range("A1") = W
End Sub
3) Find The value of W from the Equation
1 1 1 1 1 1 1
𝑆 = 1+ + + + + + +
2 4 8 16 32 64 128
(Sol= 1.99)

Sub Ex3_Sol1() Sub Ex3_Sol2() Sub Ex3_Sol3()


S=0 S=0
D=1 D=1 S=0

For i = 1 To 8 For i = 1 To 8 For i = 0 To 7

S = S + (1 / D) S=S+D S=S+1/2^i
D=D*2 D=D/2
Next i
Next I Next i
Range("A1") = S
Range("A1") = S Range("A1") = S
End Sub End Sub End Sub

You might also like