Job Sheet No.
Aim: Working with numeric data types -I
Objectives:
1) Write and execute a simple program for VBA
Procedure:
Task 1: display integer variable.
Task 2: perform addition, subtraction, multiplication on variables.
Task 3: Perform division operation on variables
Sub printval()
Dim x, y As Integer
x = 15
y=5
Debug.Print "value of x=" & x
Debug.Print "value of y=" & y
Debug.Print "addition="; (x + y)
Debug.Print "subtraction="; (x - y)
Debug.Print "multipication="; (x * y)
Debug.Print "division="; (x / y)
End Sub
OUTPUT:
value of x=15
value of y=5
addition= 20
subtraction= 10
multipication= 75
division= 3
Job Sheet No. 2
Aim: Working with numeric data types -II
Objectives:
1) Create and use floating point variables
2) Use and compare single and double data types variables
3) Use the byte and Boolean data type variables
Procedure:
Task 1: create and use the floating point variables
Sub testfloat( )
Dim x As Single
Dim y As Single
x = 3.143
y = 22 / 7
Debug.Print "value of x is:" & x
Debug.Print "value of y is:" & y
End Sub
OUTPUT:
value of x is:3.143
value of y is:3.142857
Task 2: Use and compare nature of single and double data type variables
Sub test2( )
Dim a As Single
Dim b As Single
Dim c As Double
a = 3.143
b = 22 / 7
Debug.Print "value of single type variable a is:" & a
Debug.Print "value of single type variable b is:" & b
c = 22 / 7
Debug.Print "value of double type variable c is:" & c
End Sub
OUTPUT:
value of single type variable a is:3.143
value of single type variable b is:3.142857
value of double type variable c is:3.14285714285714
Task 3: Use the byte data type variables
Sub testbyte( )
Dim x As Byte
x = 145
Debug.Print "value of x is:" & x
End Sub
OUTPUT:
value of x is:145
Now, assign an out of range value to x by changing the value of x to 256 and
check the result.
Sub test1()
Dim a As Byte
x = 256
Debug.Print "value of x=" & x
End Sub
OUTPUT:
value of x=256
TASK 4: Use Boolean type variables
Sub testbool( )
Dim x As Boolean
Debug.Print "The default value of Boolean x is:" & x
x=0
Debug.Print "Now the value of Boolean x is:" & x
x = False
Debug.Print "once again the value of Boolean x is:" & x
x=1
Debug.Print "now again the value of Boolean x is:" & x
End Sub
OUTPUT:
The default value of Boolean x is:False
Now the value of Boolean x is:False
once again the value of Boolean x is:False
now again the value of Boolean x is:True
Job Sheet No. 3
Aim: Working with string, currency, date and variant data type
Objectives:
1) Create and use string data type variables
2) Concatenate string
3) Use the currency data type variables
4) Use the variant data type variables
Procedure:
Task 1: create and use the string data type variable
Sub test1()
Dim string1 As String
string1 = "Namasthe"
Debug.Print string1
End Sub
OUTPUT:
Namasthe
TASK 2: Concatenate strings
Sub teststring()
Dim fname As String
Dim lname As String
fname = "Vidya"
lname = "Raut"
Debug.Print "First name is:" & fname
Debug.Print "Last name is:" & lname
Debug.Print "Full name is:" & fname & " " & lname
End Sub
OUTPUT:
First name is:Vidya
Last name is:Raut
Full name is:Vidya Raut
TASK3: Use the currency data type
Sub test_cur()
Dim months As Integer
Dim profit, total_profit As Currency
months = 12
profit = 10000
Debug.Print "Profit per month is:" & profit
Debug.Print "Annual profit is:" & profit * months
End Sub
OUTPUT:
Profit per month is:10000
Annual profit is:120000
TASK4 : Use the Date type of data
Sub test_date()
Dim birthday As Date
birthday = #1/1/1990#
Debug.Print "My birthday is:" & birthday
End Sub
OUTPUT:
My birthday is:1/1/1990
TASK 5:Use the variant data type
Sub test_variant()
Dim name As Variant
Dim joiningday As Variant
Dim salary As Variant
name = "sachin"
joiningday = #1/1/2014#
salary = 10000
Debug.Print "Name is:" & name
Debug.Print "Joining date is:" & joiningday
Debug.Print "Salary is:" & salary
End Sub
OUTPUT:
Name is:sachin
Joining date is:1/1/2014
Salary is:10000
My birthday is:1/1/1990
Job Sheet No. 4
Aim: Supplying user input to VBA programs
Objectives:
1) Supply inputs to VBA program through input box
Procedure:
Task 1: supply numeric input through an input box
Sub test2( )
Dim name As String
name = InputBox("Your name please")
Debug.Print "Good morning," & name
End Sub
OUTPUT:
Good morning,suyash
Job Sheet No. 5
Aim: Decision making using the if and if.…then….else statements
Objectives:
1) Use the if statements
2) Use the if….then….else statements
3) Use the multiple if….then….else statements
Procedure:
Task 1: use the if statements
Sub test_if( )
Dim marks As Integer
marks = InputBox("Enter your marks")
Debug.Print "Your marks are :" & marks
If (marks < 45) Then
Debug.Print "Sorry, you have failed to clear the test!"
End If
End Sub
OUTPUT:
Your marks are :40
Sorry, you have failed to clear the test!
TASK2: use the if…then…else statement
Sub ifelse()
Dim marks As Integer
marks = InputBox("Enter your marks")
Debug.Print "Your marks are :" & marks
If (marks < 45) Then
Debug.Print "Sorry, try again!"
Else
Debug.Print "Congratulations,You have passed the test"
End If
End Sub
OUTPUT:
Your marks are :70
Congratulations,You have passed the test
TASK3: use the multiple if…then…else statements
Sub testif2( )
Dim marks As Integer
marks = InputBox("Enter your marks")
Debug.Print "Your marks are :" & marks
If (marks >= 80) Then
Debug.Print "You have passed with Distinction!"
ElseIf (marks >= 60) Then
Debug.Print "You have passed with A grade"
ElseIf (marks >= 40) Then
Debug.Print "You have passed with B grade"
Else
Debug.Print "Sorry,You have Failed"
End If
End Sub
OUTPUT:
Your marks are :80
You have passed with Distinction!
Job Sheet No. 6
Aim: Decision making using the nested if, select case statement
Objectives:
1) Use the nested if statement
2) Use the select case statement
Procedure:
Task 1: find the largest number among three numbers using nested if
statement
Sub nestedif()
Dim x, y, z As Integer
Dim large As Integer
x = 34
y = 55
z = 12
Debug.Print "Value of x is:" & x
Debug.Print "Value of y is:" & y
Debug.Print "Value of z is:" & z
If (x > y) Then
If (x > z) Then
large = x
Else
large = z
End If
ElseIf (y > z) Then
large = y
Else
large = z
End If
Debug.Print "Largest number is:" & large
End Sub
OUTPUT:
Value of x is:34
Value of y is:55
Value of z is:12
Largest number is:55
TASK2: Print the grade using select case statement
Sub Select_Case_Example()
'Enter the value for variables
grade_name = InputBox("Enter the grade name:")
' Evaluating the expression
Select Case grade_name
Case "A"
MsgBox "Excellent"
Case "B"
MsgBox "Very Good"
Case "C"
MsgBox "Good"
Case "D"
MsgBox "Pass"
Case Else
MsgBox "Sorry You are fail!"
End Select
End Sub
OUTPUT:
“A”
Excellent
TASK 3: Write and test the VBA program to find if the given number is even
or odd.
Sub EvenOdd()
For n = 1 To 10
If n Mod 2 = 0 Then
MsgBox n & " is Even Number"
Else
MsgBox n & " is Odd Number"
End If
Next n
End Sub
OUTPUT:
1 is Odd Number
2 is Even Number
TASK 4: Write and test the VBA program to find if the given year is leap year
or not.
Sub LeapYear()
If (Year(Date) Mod 4) Then
MsgBox "Not a Leap Year!"
Else
MsgBox "Leap Year!"
End If
End Sub
OUTPUT:
Not a Leap Year!