0% found this document useful (0 votes)
11 views12 pages

Sample Basic Exe Activity

The document contains multiple VBA programs that demonstrate various programming concepts, including calculating factorials, displaying messages upon workbook opening, finding even numbers, calculating compound interest, total amounts, averages, string operations, multiplication tables, classifying marks, and calculating discounted prices. Each program includes user input and displays results using message boxes. The examples are structured to provide clear functionality and educational value for users learning VBA.

Uploaded by

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

Sample Basic Exe Activity

The document contains multiple VBA programs that demonstrate various programming concepts, including calculating factorials, displaying messages upon workbook opening, finding even numbers, calculating compound interest, total amounts, averages, string operations, multiplication tables, classifying marks, and calculating discounted prices. Each program includes user input and displays results using message boxes. The examples are structured to provide clear functionality and educational value for users learning VBA.

Uploaded by

Hari Haran
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Write VBA to display Factorial of a number using For Loop in the most simplest form.

Sub FindFactorial()

Dim num As Integer

Dim i As Integer

Dim fact As Long

' Get number from user

num = InputBox("Enter a number:")

fact = 1 ' Initialize factorial

' For Loop to calculate factorial

For i = 1 To num

fact = fact * i

Next i

' Display result

MsgBox "Factorial of " & num & " is " & fact

End Sub

Write VBA using Workbook Object to automatically display

“Welcome to Excel VBA Practical Examination” when the workbook opens.


Private Sub Workbook_Open()

MsgBox "Welcome to Excel VBA Practical Examination"

End Sub

Write a VBA Program using For Loop to display Even numbers from 1 to N (user input).

Sub DisplayEvenNumbers()

Dim N As Integer

Dim i As Integer

Dim result As String

' Get value from user

N = InputBox("Enter a number:")

' Loop to find even numbers

For i = 1 To N

If i Mod 2 = 0 Then

result = result & i & " "

End If

Next i

' Display even numbers


MsgBox "Even numbers from 1 to " & N & " are: " & result

End Sub

WITH comments

Sub DisplayEvenNumbers()

' Declare variable N to store the user input number

Dim N As Integer

' Declare variable i to use as loop counter

Dim i As Integer

' Declare variable result to store even numbers as text

Dim result As String

' Get value from user using InputBox and store in N

N = InputBox("Enter a number:")

' Start loop from 1 up to N

For i = 1 To N

' Check whether the number is even

' Mod operator returns remainder after division by 2

If i Mod 2 = 0 Then

' If remainder is 0, number is even

' Append the even number to result string


result = result & i & " "

End If

' Move to next number

Next i

' Display all even numbers in a single message box

MsgBox "Even numbers from 1 to " & N & " are: " & result

End Sub

(OR)

Sub EvenNumbers()

Dim N As Integer

Dim i As Integer

N = InputBox("Enter a number:")

For i = 2 To N Step 2

MsgBox i

Next i

End Sub
Write a VBA Program using Variables & Mathematical Operators to calculate Compound
Interest. Display result using MsgBox.

Sample Data (any 5 inputs):


Principal Rate (%) Time (Years)
500 3 2

Sub CalculateCompoundInterest()

Dim P As Double

Dim R As Double

Dim T As Double

Dim CI As Double

' Sample Inputs (You can change values)

P = 10000

R=5

T=2

' Compound Interest Calculation

CI = P * (1 + R / 100) ^ T

' Display Result

MsgBox "Principal: " & P & vbNewLine & _

"Rate: " & R & "%" & vbNewLine & _

"Time: " & T & " Years" & vbNewLine & _

"Compound Interest Amount: " & CI

End Sub
Write VBA using InputBox & Mathematical Operations to calculate
Total Amount = Quantity × Rate.

Sample Data:

Quantity Rate

500 20

Sub CalculateTotalAmount()

Dim Quantity As Double

Dim Rate As Double

Dim Total As Double

' Get Quantity from user

Quantity = InputBox("Enter Quantity:")

' Get Rate from user

Rate = InputBox("Enter Rate:")

' Calculate Total Amount

Total = Quantity * Rate

' Display Result

MsgBox "Quantity: " & Quantity & vbNewLine & _

"Rate: " & Rate & vbNewLine & _

"Total Amount = " & Total

End Sub
Write VBA using For Each Loop to read 5 numbers from cells (B1:B5) and display the
Average.

Sub CalculateAverage()

Dim cell As Range

Dim total As Double

Dim count As Integer

Dim avg As Double

' Initialize variables

total = 0

count = 0

' Loop through each cell in range B1:B5

For Each cell In Range("B1:B5")

total = total + [Link] ' Add each cell value

count = count + 1 ' Count number of cells

Next cell

' Calculate average

avg = total / count

' Display result

MsgBox "Average of numbers in B1:B5 is: " & avg

End Sub
Write a VBA Program using String Operations (UCase, LCase, Len, Concatenation).

Sample Data:

First Name Last Name

arun khan

divya devi

kiran kumar

meena rani

ravi shewag

Sub StringOperationsFromSheet()

Dim i As Integer

Dim firstName As String

Dim lastName As String

Dim fullName As String

Dim nameLength As Integer

' Loop through rows 2 to 6

For i = 2 To 6

' Read data from worksheet

firstName = Cells(i, 1).Value

lastName = Cells(i, 2).Value

' Convert first letter to uppercase and rest to lowercase

firstName = UCase(Left(firstName, 1)) & LCase(Mid(firstName, 2))

lastName = UCase(Left(lastName, 1)) & LCase(Mid(lastName, 2))


' Concatenate First Name and Last Name

fullName = firstName & " " & lastName

' Find length of full name

nameLength = Len(fullName)

' Display result

MsgBox "Full Name: " & fullName & vbNewLine & _

"Length: " & nameLength

Next i

End Sub

Write VBA to display multiplication table of a number.

Sub MultiplicationTable()

Dim num As Integer

Dim i As Integer

Dim result As String

' Get number from user

num = InputBox("Enter a number:")

' Generate multiplication table (1 to 10)

For i = 1 To 10
result = result & num & " x " & i & " = " & num * i & vbNewLine

Next i

' Display the table

MsgBox result

End Sub

Write VBA using If…Else to classify Marks (Distinction ≥75, Pass ≥50).

Name Marks
Arun 81
Divya 68
Kiran 46
Meena 73
Ravi 90

Sub ClassifyMarks()

Dim i As Integer

Dim marks As Integer

Dim result As String

' Loop through rows 2 to 6

For i = 2 To 6

' Read marks from column B

marks = Cells(i, 2).Value


' Classify using If...Else

If marks >= 75 Then

result = "Distinction"

ElseIf marks >= 50 Then

result = "Pass"

Else

result = "Fail"

End If

' Display result

MsgBox "Name: " & Cells(i, 1).Value & vbNewLine & _

"Marks: " & marks & vbNewLine & _

"Result: " & result

Next i

End Sub

Write VBA using InputBox & Mathematical Operations to calculate Discounted Price.

Sample Data:

Price Discount %

10000 9

Sub CalculateDiscountedPrice()

Dim Price As Double


Dim Discount As Double

Dim DiscountAmount As Double

Dim FinalPrice As Double

' Get Price from user

Price = InputBox("Enter Price:")

' Get Discount Percentage from user

Discount = InputBox("Enter Discount Percentage:")

' Calculate Discount Amount

DiscountAmount = Price * Discount / 100

' Calculate Final Discounted Price

FinalPrice = Price - DiscountAmount

' Display Result

MsgBox "Original Price: " & Price & vbNewLine & _

"Discount: " & Discount & "%" & vbNewLine & _

"Discount Amount: " & DiscountAmount & vbNewLine & _

"Discounted Price: " & FinalPrice

End Sub

You might also like