VBA Solved Jbsheets
VBA Solved Jbsheets
Sub printval()
Dim x, y As Integer
x = 15
y=5
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
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:
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:
Sub test1()
Dim string1 As String
string1 = "Namasthe"
Debug.Print string1
End Sub
OUTPUT:
Namasthe
My birthday is:1/1/1990
Name is:sachin
Salary is:10000
My birthday is:1/1/1990
Sub test2( )
Dim name As String
name = InputBox("Your name please")
Debug.Print "Good morning," & name
End Sub
OUTPUT:
Good morning,suyash
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:
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
Value of y is:55
Value of z is:12
“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: