Ejemplos Vba 28
Ejemplos Vba 28
Ejemplos Vba 28
PRACTICAL NO.-01
AIM- Write a Simple program in VBA(Visual Basic Application) to say Hello CSA.
PROGRAM CODE-
Sub csa()
Dim str As String
Str=”Hello CSA”
MsgBox(str)
End Sub
OUTPUT-
PRACTICAL NO.-02
AIM- Write a program in VBA to add two numbers.
PROGRAM CODE-
Sub add1()
Dim a, b, c, r As Integer
a = 20
b = 10
r=a+b
MsgBox (r)
End Sub
OUTPUT:-
PRACTICAL NO.-03
AIM- Write a program in VBA to find area of circle.
PROGRAM CODE-
Sub area()
Dim pi, r, result As Double
pi = 3.14
r=5
result = pi * r * r
MsgBox (result)
End Sub
OUTPUT-
PRACTICAL NO.-04
AIM- Write a program in VBA to find calculate Simple Interest.
PROGRAM CODE-
Sub si()
Dim p, r, t, result As Double
p = 2000
r=5
t=2
result = (p * r * t) / 100
MsgBox (result)
End Sub
OUTPUT-
PRACTICAL NO.-05
AIM- Write a program in VBA to find calculate Percentage of Five Subject.
PROGRAM CODE-
Sub mal()
Dim a, b, c, d, e, percentage As Double
a = 90
b = 80
c = 75
d = 60
e = 100
percentage = (a + b + c + d + e) / 500 * 100
MsgBox (percentage)
End Sub
OUTPUT-
PRACTICAL NO.-06
AIM- Write a program in VBA to find given number is odd or even.
PROGRAM CODE-
Sub even()
Dim a As Double
a = 31
If (a Mod 2 = 0) Then
MsgBox ("a is even number")
End If
If (a Mod 2 <> 0) Then
MsgBox ("a is odd number")
End If
End Sub
OUTPUT-
PRACTICAL NO.-07
AIM- Write a program in VBA to find leap year(year is leap or not).
PROGRAM CODE-
Sub leap()
Dim a As Integer
a = 2024
If (a Mod 4 = 0) Then
MsgBox ("a is leap year")
Else
MsgBox ("a is not leap year")
End If
End Sub
OUTPUT-
PRACTICAL NO.-08
AIM- Write a program in VBA to find greatest number among three numbers.
PROGRAM CODE-
Sub greatest()
Dim a, b, c As Double
a = 40
b = 50
c = 30
If (a > b And a > c) Then
MsgBox ("a is greatest number")
End If
If (b > a And b > c) Then
MsgBox ("b is greatest number")
End If
If (c > a And c > b) Then
MsgBox ("c is greatest number")
End If
End Sub
OUTPUT-
PRACTICAL NO.-09
AIM- Write a program in VBA to find out triangle is Equialateral,Isosceles or Scelene.
PROGRAM CODE-
Sub triangle()
Dim a,b,c AS Double
A=3
B=4
C=4
If (a = b And a = c) Then
MsgBox ("this is a equaliteral triangle")
ElseIf (a <> b And b <> c And a <> c) Then
MsgBox ("this is a scelene triangle")
Else
MsgBox ("this is a isosceles triangle")
End If
End Function
OUTPUT-
PRACTICAL NO.-10
AIM- Write a program in VBA to find entered number is Positive number or Negative
number.
PROGRAM CODE-
Sub dinesh()
Dim a As Variant
a = InputBox("enter the number")
If (a > 0) Then
MsgBox ("positive number")
Else
MsgBox ("negative number")
End If
End Sub
OUTPUT-
PRACTICAL NO.-11
AIM- Write a program in VBA to calculate addition, substraction, multiplication and
division using switch statement .
PROGRAM CODE-
Sub maths()
Dim a, b As Double
Dim c As Variant
a = InputBox("enter the value of a")
b = InputBox("enter the value of b")
c = InputBox("enter the sign")
Select Case c
Case "+"
c=a+b
MsgBox ("addition of given values=" & c)
Case "-"
c=a-b
MsgBox ("substraction of given values=" & c)
Case "*"
c=a*b
MsgBox ("multiplication of given values=" & c)
Case "/"
c=a/b
MsgBox ("division of given values=" & c)
End Select
End Sub
OUTPUT-
PRACTICAL NO.-12
AIM- Write a program in VBA to find total, percentage, result and class as per the
given conditions.
PROGRAM CODE-
Sub dinesh()
Dim tm, ss, pm, sm, sum, total As Integer
Dim percentage As Double
Dim result, class As String
tm = 30
ss = 50
pm = 80
sm = 10
total = 200
sum = tm + ss + pm + sm
MsgBox ("total=" & sum)
percentage = sum / total * 100
MsgBox ("percentage=" & percentage)
If (sm >= 12 And ss >= 17 And pm >= 60) Then
result = "pass"
MsgBox ("result=" & result)
Else
result = "fail"
MsgBox ("result=" & result)
End If
If (percentage > 75) Then
class = "distinction"
MsgBox ("class=" & class)
ElseIf (percentage < 75 And percentage > 60) Then
class = "fist"
MsgBox ("class=" & class)
ElseIf (percentage <= 60 And percentage >= 45) Then
class = "pass"
MsgBox ("class=" & class)
End If
End Sub
OUTPUT-
PRACTICAL NO.-13
AIM- Write a program in VBA to print multiplication table in current worksheet.
PROGRAM CODE-
Sub multiplication()
For i = 1 To 10
For j = 1 To 10
Cells(i, j).Value = i * j
Next
Next
End Sub
OUTPUT-
PRACTICAL NO.-14
AIM- Write a program in VBA to print numbers 1 to 10.
PROGRAM CODE-
Sub forloop1()
For i = 1 To 10
Debug.Print i
Next
End Sub
OUTPUT-
PRACTICAL NO.-15
AIM- Write a program in VBA to print numbers 10 to 1.
PROGRAM CODE-
Sub untildemo()
Dim i As Integer
i = 10
Do
Debug.Print i
i = i -1
Loop While i =10
End Sub
OUTPUT-
PRACTICAL NO.-16
AIM- Write a program in VBA to add any five numbers using user input & loop.
PROGRAM CODE-
Sub addition()
Dim a As Integer
Dim total As Integer
total = 0
For i = 1 To 5
a = InputBox("enter the number")
total = total + a
Next i
MsgBox (total)
End Sub
OUTPUT-
PRACTICAL NO.-17
AIM- Write a program in VBA to print odd numbers between 1 to 10 reverse order.
PROGRAM CODE-
Sub dinesh()
Dim i As Integer
For i = 10 To 1 Step -1
If (i Mod 2 <> 0) Then
Debug.Print i
End If
Next
End Sub
OUTPUT-
PRACTICAL NO.-18
AIM- Write a program in VBA to print even numbers between 50 to 60.
PROGRAM CODE-
Sub even()
For i = 50 To 60 Step 2
Debug.Print i
Next
End Sub
OUTPUT-
PRACTICAL NO.-19
AIM- Write a program in VBA to find given number is odd or even number using
function.
PROGRAM CODE-
Function even_or_odd(a As Integer)
If a Mod 2 = o Then
MsgBox ("even number")
Else
MsgBox ("odd number")
End If
End Function
Sub even_or_odd1()
Dim b As Integer
b = InputBox("enter the value of number")
Debug.Print even_or_odd(b)
End Sub
OUTPUT-
PRACTICAL NO.-20
AIM- Write a program in VBA to find greatest number in given number using
function.
PROGRAM CODE-
Function greatest(a As Integer, b As Integer, c As Integer)
If (a > b & a > c) Then
MsgBox ("a is greatest number")
ElseIf (b > a & b > c) Then
MsgBox ("b is greatest number")
Else
MsgBox ("c is greatest number")
End If
End Function
Sub greatest1()
Dim x As Integer
Dim y As Integer
Dim z As Integer
x = InputBox("enter the first number")
y = InputBox("enter the second number")
z = InputBox("enter the third number")
Debug.Print greatest(x, y, z)
End Sub
OUTPUT-
PRACTICAL NO.-21
AIM- Write a program in VBA to find out triangle is equilateral, Isosceles, or Scalene
using function.
PROGRAM CODE-
Function types_of_triangle(a As Integer, b As Integer, c As Integer)
If (a = b And a = c) Then
MsgBox ("this is a equaliteral triangle")
ElseIf (a <> b And b <> c And a <> c) Then
MsgBox ("this is a scelene triangle")
Else
MsgBox ("this is a isosceles triangle")
End If
End Function
Sub triangles()
Dim x As Integer
Dim y As Integer
Dim z As Integer
x = InputBox("enter the length of triangle")
y = InputBox("enter the width of triangle")
z = InputBox("enter the height of triangle")
Debug.Print types_of_triangle(x, y, z)
End Sub
OUTPUT-
PRACTICAL NO.-22
AIM- Write a program in VBA to print all week days using array week day number.
PROGRAM CODE-
Sub array2()
Dim a1
a1 = Array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday",
"sunday")
For i = 0 To UBound(a1) Step 1
Cells(i + 1, 1).Value = i + 1
Cells(i + 1, 2).Value = a1(i)
Next
End Sub
OUTPUT-
PRACTICAL NO.-23
AIM- Write a program in VBA to add two numbers using userform.
PROGRAM CODE-
Option Explicit
OUTPUT-
PRACTICAL NO.-24
AIM- Write a program in VBA to find greatest number using user form.
PROGRAM CODE-
OUTPUT-
PRACTICAL NO.-25
AIM- Write a program in VBA to to find odd or even number using user form.
PROGRAM CODE-
Option Explicit
OUTPUT-
PRACTICAL NO.-26
AIM- Write a program in VBA to to create login form using user form .
PROGRAM CODE-
OUTPUT-
PRACTICAL NO.-27
AIM- Write a program in VBA to create a simple calculator using user form .
PROGRAM CODE-
Private Sub CommandButton1_Click()
txtresult.Text = Val(txtnumber1.Text) + Val(txtnumber2.Text)
End Sub
End Sub
OUTPUT-
PRACTICAL NO.-28
AIM- Write a program in VBA to calculate marksheet using user form.
PROGRAM CODE-
Private Sub Label19_Click()
txttotal = 400
txtobtained.Text = Val(txttt.Text) + Val(txtss.Text) + Val(txtpot.Text) + Val(txttp.Text)
txtpercentage.Text = Val(txtobtained.Text) / 4
If (txtpercentage >= 80) Then
txtgrade.Text = "A"
txtdivision.Text = "FIRST"
txtresult.Text = "PASS"
ElseIf (txtpercentage >= 60) Then
txtgrade.Text = "B"
txtdivision.Text = "SECOND"
txtresult.Text = "PASS"
ElseIf (txtpercentage >= 33) Then
txtgrade.Text = "C"
txtdivision.Text = "THIRD"
txtresult.Text = "PASS"
Else
txtgrade.Text = "D"
txtdivision.Text = "FAILED"
txtresult.Text = "FAIL"
End Sub
Private Sub Label20_Click()
txttt = " "
txtss = " "
txtpot = " "
txttp = " "
txttotal = " "
txtresult = " "
txtdivision = ""
txtgrade = ""
txtpercentage = ""
txtobtained = ""
End Sub
Private Sub Label21_Click()
Unload Me
End Sub
OUTPUT-
PRACTICAL NO.-28
AIM- Write a program in VBA to use all form controls using user form.
PROGRAM CODE-
Private Sub CommandButton1_Click()
Dim myvar As String
Dim x As Double
Dim dcc As Long
Dim abc As Worksheet
Set abc = Worksheets("sheet1")
dcc = Sheets("sheet1").Range("a" & Rows.Count).End(xlUp).Row
With abc
.Cells(dcc + 1, 1).Value = Me.txtname.Value
.Cells(dcc + 1, 2).Value = Me.txtphone.Value
.Cells(dcc + 1, 4).Value = Me.ComboBox1.Value
.Cells(dcc + 1, 3).Value = Me.ListBox1.Value
myvar = " "
For x = 0 To Me.ListBox1.ListCount - 1
If Me.ListBox1.Selected(x) Then
If myvar = " " Then
myvar = Me.ListBox1.List(x, 0)
Else
myvar = myvar & "," & Me.ListBox1.List(x, 0)
End If
End If
Next x
.Cells(dcc + 1, 3).Value = myvar
Me.Hide