0% found this document useful (0 votes)
2 views

Lab 4 - Correction

The document contains a series of exercises for writing VBA code in Excel, focusing on different types of loops such as For, While, and Do-While. Each exercise includes specific tasks like calculating sums, counting even numbers, verifying passwords, and finding multiples or squares. The document serves as a practical guide for learning and applying VBA programming concepts.

Uploaded by

khawla tadist
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 4 - Correction

The document contains a series of exercises for writing VBA code in Excel, focusing on different types of loops such as For, While, and Do-While. Each exercise includes specific tasks like calculating sums, counting even numbers, verifying passwords, and finding multiples or squares. The document serves as a practical guide for learning and applying VBA programming concepts.

Uploaded by

khawla tadist
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Université EUROMED de Fès

Euromed Business School


VBA for Excel

Lab 4
Loops

Exercise 1:

Write VBA code to calculate the sum of numbers from 1 to 100 using:

1. A For loop.
2. A While loop.
3. A Do-While loop.

Sub SumOfNumbers()
Dim i As Integer
Dim sumFor As Integer
Dim sumWhile As Integer
Dim sumDoWhile As Integer

' For loop


sumFor = 0
For i = 1 To 100
sumFor = sumFor + i
Next i
Range("A1").Value = sumFor

' While loop


sumWhile = 0
i = 1
While i <= 100
sumWhile = sumWhile + i
i = i + 1
Loop
Range("A2").Value = sumWhile

' Do-While loop


sumDoWhile = 0
i = 1
Do While i <= 100
sumDoWhile = sumDoWhile + i
i = i + 1
Loop
Range("A3").Value = sumDoWhile
End Sub

Prof. Khawla TADIST 1


Exercise 2:

Write VBA code to find the first multiple of 7 greater than a given number in A1 using:

1. A While loop.
2. A Do-While loop.

Sub FirstMultipleOf7()
Dim number As Integer
Dim result As Integer

number = Range("A1").Value

' While loop


result = number + 1
While result Mod 7 <> 0
result = result + 1
Loop
Range("B1").Value = result

' Do-While loop


result = number + 1
Do While result Mod 7 <> 0
result = result + 1
Loop
Range("B2").Value = result
End Sub

Prof. Khawla TADIST 2


Exercise 3:

Write VBA code to count the even numbers between 1 and a given number in A1 using:

1. A For loop.
2. A While loop.
3. A Do-While loop.

Sub CountEvenNumbers()
Dim i As Integer
Dim countFor As Integer
Dim countWhile As Integer
Dim countDoWhile As Integer
Dim limit As Integer

limit = Range("A1").Value

' For loop


countFor = 0
For i = 1 To limit
If i Mod 2 = 0 Then
countFor = countFor + 1
End If
Next i
Range("B1").Value = countFor

' While loop


countWhile = 0
i = 1
While i <= limit
If i Mod 2 = 0 Then
countWhile = countWhile + 1
End If
i = i + 1
Loop
Range("B2").Value = countWhile

' Do-While loop


countDoWhile = 0
i = 1
Do While i <= limit
If i Mod 2 = 0 Then
countDoWhile = countDoWhile + 1
End If
i = i + 1
Loop
Range("B3").Value = countDoWhile
End Sub

Prof. Khawla TADIST 3


Exercise 4:
Create a password verification system where the user must enter “12345”. Choose the best loop.
Sub PasswordVerification()
Dim password As String
Dim attempts As Integer

attempts = 0

Do
password = InputBox("Enter the password:")
attempts = attempts + 1

If password = "12345" Then


MsgBox "Access Granted"
Exit Sub
End If
Loop While attempts < 3

MsgBox "Access Denied"


End Sub

Exercise 5:

Write VBA code to calculate the factorial of a number in A1 using:

1. A For loop.
2. A While loop.

Sub Factorial()
Dim i As Integer
Dim factorialFor As Double
Dim factorialWhile As Double
Dim number As Integer

number = Range("A1").Value

' For loop


factorialFor = 1
For i = 1 To number
factorialFor = factorialFor * i
Next i
Range("B1").Value = factorialFor

' While loop


factorialWhile = 1
i = 1
While i <= number
factorialWhile = factorialWhile * i
i = i + 1
Loop
Range("B2").Value = factorialWhile
End Sub

Prof. Khawla TADIST 4


Exercise 6:

Write a VBA code to calculate the product of numbers from 1 to 10 using:

1. A For loop.
2. A While loop.

Sub ProductOfNumbers()
Dim i As Integer
Dim productFor As Double
Dim productWhile As Double

' For loop


productFor = 1
For i = 1 To 10
productFor = productFor * i
Next i
Range("A1").Value = productFor

' While loop


productWhile = 1
i = 1
While i <= 10
productWhile = productWhile * i
i = i + 1
Loop
Range("A2").Value = productWhile
End Sub

Prof. Khawla TADIST 5


Exercise 7:

Find the smallest square (e.g., 1², 2², etc.) greater than a given number in A1 using:

1. A While loop.
2. A Do-While loop.

Sub SmallestSquare()
Dim number As Double
Dim square As Integer
Dim i As Integer

number = Range("A1").Value

' While loop


i = 1
While i ^ 2 <= number
i = i + 1
Loop
Range("B1").Value = i ^ 2

' Do-While loop


i = 1
Do While i ^ 2 <= number
i = i + 1
Loop
Range("B2").Value = i ^ 2
End Sub

Prof. Khawla TADIST 6


Exercise 8:

Count how many numbers between 1 and a given number in A1 are divisible by 3 using:

1. A For loop.
2. A While loop.

Sub CountDivisibleBy3()
Dim i As Integer
Dim countFor As Integer
Dim countWhile As Integer
Dim limit As Integer

limit = Range("A1").Value

' For loop


countFor = 0
For i = 1 To limit
If i Mod 3 = 0 Then
countFor = countFor + 1
End If
Next i
Range("B1").Value = countFor

' While loop


countWhile = 0
i = 1
While i <= limit
If i Mod 3 = 0 Then
countWhile = countWhile + 1
End If
i = i + 1
Loop
Range("B2").Value = countWhile
End Sub

Exercise 9:

Write a VBA code to repeat a message “Hello, VBA!” in column A for a given number of times
specified in A1 using the appropriate loop.

Sub RepeatMessage()
Dim i As Integer
Dim count As Integer

count = Range("A1").Value

' For loop


For i = 1 To count
Range("B" & i).Value = "Hello, VBA!"
Next i
End Sub

Prof. Khawla TADIST 7


Exercise 10:

Write a VBA code to calculate the sum of all odd numbers between 1 and a given number in
A1 using:

1. A For loop.
2. A While loop.

Sub SumOddNumbers()
Dim i As Integer
Dim sumFor As Integer
Dim sumWhile As Integer
Dim limit As Integer

limit = Range("A1").Value

' For loop


sumFor = 0
For i = 1 To limit
If i Mod 2 <> 0 Then
sumFor = sumFor + i
End If
Next i
Range("B1").Value = sumFor

' While loop


sumWhile = 0
i = 1
While i <= limit
If i Mod 2 <> 0 Then
sumWhile = sumWhile + i
End If
i = i + 1
Loop
Range("B2").Value = sumWhile
End Sub

Prof. Khawla TADIST 8

You might also like