Vba Litho.
Vba Litho.
Vba Litho.
S.I. (M.V.Khetiya
Contents
Demo no. 68: Familiarization with VBA editor(week no.-30)...............................................................4
Demo no. 69: Use Form Controls in Excel VBA(Week no.-30)............................................................5
Page No. 1
Visual Basic Application
1. Button:..........................................................................................................................................6
2. Combo-Box & List-Box:.................................................................................................................6
3. Check Box & Radio Button:...........................................................................................................7
4. Spin Button & Scroll Bar:...............................................................................................................7
5.From Controls.................................................................................................................................9
Demo-70 Basic Programming in VBA(Weekno.-31)..........................................................................10
1-Print..............................................................................................................................................10
2-Variable Constant Data type.........................................................................................................11
3-Arithmetic Operators....................................................................................................................13
4-Concatenation...............................................................................................................................14
5-Comparison Operators..................................................................................................................15
6-Logical Operators.........................................................................................................................17
Demo-71 Dialog oxes in VBA(Week no.-31)......................................................................................19
1-MSG Box.....................................................................................................................................19
2-Input Box......................................................................................................................................21
Demo-72 Conditional Statements in VBA(Weekno.-31).....................................................................22
Demo-73 Loops in VBA(Weekno.-32)................................................................................................26
Page No. 2
Visual Basic Application
Page No. 3
Visual Basic Application
Open VBA Editor Run or Record Macro ActiveX or Form Modify Controls
Controls Properties
There are various methods to open VBA editor. Which is as following.
Click on “Visual Basic” button in the “code” panel of “Developer” tab.
Click on “View code” button in the “Controls” panel of “Developer” tab.
By pressing Alt + F11 shortcut key.
Page No. 4
Visual Basic Application
Menu Bar Tool Bar
X
S.I. (M.V. Khetiya)
Page No. 5
Visual Basic Application
1. Button:
First take a button of “Form controls” from “Insert” in “controls” panel of “Developer”
tab.
Window of “Assign Macro” will open. Which is as below
Page No. 6
Visual Basic Application
Click in “control” tab inside “Format control” window.
In input range select the range where you want to print answer.
In the cell link select a cell you want to link.
Then click on “ok” button.
Page No. 7
Visual Basic Application
Incremental change means the number of scroll you want to scroll at a time.
After setting all these values click on “ok” button.
Page No. 8
Visual Basic Application
5.From Controls
Output:-
X
S.I. (M.V. Khetiya)
Page No. 9
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
MsgBox ("Dharaviya Bhagavatee")
End Sub
Private Sub CommandButton2_Click()
Range("b1").Value = "Hello"
Range("c1:c10").Value = "Bhagavatee"
End Sub
Private Sub CommandButton3_Click()
Cells(11, 1) = "Copa-B-77"
ActiveCell.Value = "Apple"
End Sub
Output:-
Page No. 10
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
Dim byte_no As Byte
byte_no = 25
Range("c5") = "byte datatype"
Range("d5") = byte_no
Page No. 11
Visual Basic Application
Dim myname As String
myname = "Mahila Iti"
Range("h5") = "string"
Range("i5") = myname
End Sub
Output:-
3-Arithmetic Operators
Write a program in VBA to demonstrate the use of Arithmetic Operators in
VBA. Page No. 12
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
Range("l4") = Range("j4") + Range("k4")
Range("l5") = Range("j5") - Range("k5")
Range("l6") = Range("j6") * Range("k6")
Range("l7") = Range("j7") / Range("k7")
Range("l8") = Range("j8") Mod Range("k8")
End Sub
Output:-
4-Concatenation
Write a program in VBA to demonstrate the use of Concatenation Operators
of VBA.
Page No. 13
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
Range("c2") = Range("a2") + Range("b2")
Range("c3") = Range("a3") + Range("b3")
Range("d2") = Range("a2") & Range("b2")
Range("d3") = Range("a3") & Range("b3")
End Sub
Output:-
5-Comparison Operators
Input:-
Private Sub CommandButton1_Click()
If Range("E6") = Range("F6") Then
Range("G6").Value = "It's Equal to."
Else
Range("G6").Value = "It's not Equal to."
End If
If Range("E7") < Range("F7") Then
Range("G7").Value = "It's Equal to."
Else
Range("G7").Value = "It's not Equal to."
End If
If Range("E8") > Range("F8") Then
Range("G8").Value = "It's Equal to."
Else
Range("G8").Value = "It's not Equal to."
End If
If Range("E9") <= Range("F9") Then
Range("G9").Value = "It's Equal to."
Else
Range("G9").Value = "It's not Equal to."
End If
If Range("E10") >= Range("F10") Then
Range("G10").Value = "It's Equal to."
Else
Range("G10").Value = "It's not Equal to."
End If
If Range("E11") <> Range("F6") Then
Range("G11").Value = "It's Equal to."
Else
Range("G11").Value = "It's not Equal to."
End If
End Sub
Page No. 15
Visual Basic Application
Output:-
6-Logical Operators
Write a program in VBA to demonstrate the use of Logical Operators of
VBA.
Page No. 16
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
Dim Age1 As Integer
Age1 = 22
If Age1 < 13 And Age > 19 Then
Range("H7") = "Age is Between"
Else
Range("H7") = "Age is not Between"
End If
End Sub
Private Sub CommandButton2_Click()
Dim Age1 As Integer
Age1 = 22
If Age1 < 13 Or Age > 19 Then
Range("H8") = "Age is Between"
Else
Range("H8") = "Age is not Between"
End If
End Sub
Private Sub CommandButton3_Click()
Dim Age1 As Integer
Age1 = 22
If Not Age1 < 13 Then
Range("H9") = "Age is Between"
Else
Range("H9") = "Age is not Between"
End If
End Sub
Page No. 17
Visual Basic Application
Output:-
X
S.I. (M.V.Khetiya)
Page No. 18
Visual Basic Application
End Sub
Private Sub CommandButton7_Click()
no = MsgBox("This MSG BOx is New Line" & vbNewLine & "Bhagavati Dharviya",
vbQuestion + vbYesNo, "Do You Save This File?")
End Sub
Page No. 19
Visual Basic Application
Output:-
Page No. 20
Visual Basic Application
2-Input Box
Write a program in VBA to demonstrate the use of Input box function & its
options.
Input:-
Private Sub CommandButton1_Click()
Dim a As String
a = InputBox("Plese Enter Your Name:", "Name Box", "Enter Your Name", 1000)
ActiveCell = a
End Sub
Output:-
X
S.I. (M.V. Khetiya)
Page No. 21
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
Dim a As Integer
a = Range("i4")
If a = 20 Then
Range("i5") = "Number is 20."
End If
End Sub
Page No. 22
Visual Basic Application
End Sub
Page No. 23
Visual Basic Application
Range("i22") = "Your Gread is E. "
ElseIf a >= 50 And a < 60 Then
Range("i22") = "Your Gread is D. "
ElseIf a >= 60 And a < 70 Then
Range("i22") = "Your Gread is C. "
ElseIf a >= 70 And a < 80 Then
Range("i22") = "Your Gread is B. "
Else
Range("i22") = "Your Gread is A . "
End If
Else
Range("i22") = "You Are Fail."
End If
End Sub
Page No. 24
Visual Basic Application
Range("i28") = Result
End Sub
X
S.I. (M.V. Khetiya)
Page No. 25
Visual Basic Application
Input:-
Private Sub CommandButton1_Click()
Dim a As Integer
Dim r As Integer
a = Range("c3")
r=4
For i = a To 0 Step -2
Cells(r, 3) = i
r=r+1
Next
End Sub
Private Sub CommandButton2_Click()
Z=1
flower = Array("Sunflower", "Rose", "Chamali")
Dim flowersname As Variant
For Each Item In flower
flowersname = flowersname & Item & Chr(10)
Cells(12, Z) = flowersname
Z=Z+1
Next
End Sub
Page No. 26
Visual Basic Application
Private Sub CommandButton3_Click()
a=1
Do While B < 5
B=B+1
Cells(16, B) = B
Loop
End Sub
Page No. 27
Visual Basic Application
X
S.I. (M.V.Khetiya)
Page No. 28