Академический Документы
Профессиональный Документы
Культура Документы
PRACTICAL EXERCISE
EXERCISE EXERCISE TITLE PAGE NO.
NO.
1 Familiarizations With The VBA Editor 1
2 Simple Program Involving VBA Data Type ,Variables 2
,Operator And Constants
3 Working with string Function in VBA 5
4 Creating and manipulating arrays in VBA 6
Procedure –
Procedure –
Step1. Click To Insert And Insert A Form
Step4. Double Click To Command Button and U See The Code Window
Object3.1- Working with string fixed variable and variable length string Variable length string.
Example 1 Lan(string)
Example 2 LCase
Example 3 ucase
Example 4 Left(string)
a = lnputBox("Enter a Word")
MsgBox Left(a, 4)
Example 5 right(string)
a = InputBox("Enter A word")
MsgBox Right(a, 5)
Example 6 Mid(string)
a = InputBox("Enter A Word")
MsgBox Mid(a, 4, 5)
Example 7 string
a = lnpuBox("Enter A Word")
MsgBox String(4, a)
a = lnputBox("Enter a Name")
MsgBox StrReverse(a)
Example 1O Now() function
MsgBox Now()
Go To statement
Control Use
Stores text which is not editable by user, and is used to describe other
Label
controls.
TextBox Holds text by allowing user to enter or modify.
Is a list of items from which a user can select, and also allows user to enter his
ComboBox
own item in the text box. ComboBox is a combination of TextBox and ListBox.
Is a list of items from which a user can select, but does not allow user to enter
ListBox
his own item.
A CheckBox whether selected or not, indicates True or False values. Used to
CheckBox
select mutually exclusive options viz. Yes/No, On/Off, …
Used to make one selection from multiple options. Also referred to as Radio
OptionButton
Button.
Executes one action when clicked first and a different action on the second
ToggleButton
click. The button toggles bewteen the two states, On and Off.
Used to group controls that work together or have some commonality.
Frame Particularly useful to group OptionButtons which become mutually exclusive
within a Frame.
CommandButton A button, when clicked by the user, executes a code or action.
Is a collection of Tabs wherein each Tab contains the same set of controls, and
TabStrip
the content of the controls changes when a different Tab is selected.
Comprises of one or more Page objects, each containing its own set of
MultiPage controls. Selecting a Page (ie. making it visible) hides the other Pages of the
MultiPage control.
Is used to change (increment or decrement) the value displayed by other
ScrollBar controls. The ScrollBar box can be dragged to change the control's value over
larger increments.
Similar to a ScrollBar, is used to increment or decrement the value displayed
SpinButton
by other controls, without the drag facility.
Image Is used to display a Picture on the UserForm.
Allows a user to select a worksheet range from a box, or to type in the range
RefEdit
therein. It behaves similar to the built-in Excel reference boxes.
UserForm Basics
UserForm
VBA Code What is Does
Application
Displays the UserForm with name UserForm1. This
code should be inserted in a Standard VBA Module
To Display a and not in the Code Module of the UserForm. You
UserForm1.Show
UserForm can create a button in a worksheet, then right click
to assign macro to this button, and select the macro
which shows the UserForm.
Load statement is useful in case of a complex
Load a
UserForm that you want to load into memory so
UserForm into
Load UserForm1 that it displays quickly on using the Show method,
memory but do
which otherwise might take a longer time to
not display
appear.
Remove a Note: The Hide method (UserForm1.Hide) does not
UserForm from unload the UserForm from memory. To unload the
Unload UserForm1
memory / Close UserForm from memory, the Unload method should
UserForm be used.
Use the Me keyword in a procedure in the Code
Unload Me
Module of the UserForm.
Using the Hide method will temporarily hide the
Hide a
UserForm1.Hide UserForm, but will not close it and it will remain
UserForm
loaded in memory.
Print a The PrintForm method sends the UserForm directly
UserForm1.PrintForm
UserForm for printing.
If the UserForm is displayed as Modeless, user can
continue working in Excel while the UserForm
Display continues to be shown. Omitting the Boolean
UserForm as UserForm1.Show False argument (False or 0) will display the UserForm as
Modeless Modal, in which case user cannot simultaneously
work in Excel. By default UserForm is displayed as
Modal.
Close a
Unload UserForm1 The Unload method closes the specified UserForm.
UserForm
The Unload method closes the UserForm within
Unload Me
whose Code Module it resides.
Use the End statement in the "Close"
End CommandButton to close the form. The "End"
statement unloads all forms.
Specify Caption is the text which describes and identifies a
UserForm1.Caption
UserForm UserForm and will display in the header of the
= "Bio Data"
Caption Userform.
Set UserForm UserForm1.Height =
Set Height of the UserForm, in points.
Size 350
UserForm1.Width =
Set Width of the UserForm, in points.
550
Set UserForm
Position:
Left & Top Distance set is between the form and the Left or
UserForm1.Left = 30
properties Top edge of the window that contains it, in pixels.
UserForm1.Top = 50
Move method includes two arguments which are
UserForm1.Move 200,
Move method required - the Left distance and the Top distance, in
50
that order.
Exersice No12. Useing active X Controls
Create ActiveX controls such as command buttons, text boxes, list boxes etc. To create an
ActiveX control in Excel VBA, execute the following steps.
1. On the Developer tab, click Insert.
2. For example, in the ActiveX Controls group, click Command Button to insert a command
button control.
Note: you can change the caption and name of a control by right clicking on the control (make
sure Design Mode is selected) and then clicking on Properties. Change the caption of the
command button to 'Apply Blue Text Color'. For now, we will leave CommandButton1 as the
name of the command button.
The Visual Basic Editor appears.
6. Add the code line shown below between Private Sub CommandButton1_Click() and End Sub.
7. Select the range B2:B4 and click the command button (make sure Design Mode is
deselected).
Result:
Exersice No13. Simple Project Involing MS Excel And VBA
Dim c As Integer
Dim t As Integer
Private Sub CommandButton1_Click()
txtid.Text = ""
txtname.Text = ""
txtsex.Text = ""
txtdob.Text = ""
txtpob.Text = ""
txtphone.Text = ""
End Sub
End If
End Sub
Option Explicit
Sub Calculate()
loan = Range("D4").Value
rate = Range("F6").Value
nper = Range("F8").Value
rate = rate / 12
nper = nper * 12
End If
End Sub
Project4. Equation Solving Program
Code
Private Sub Solve_Click()
Dim a, b, c, d, m, n As Integer
Dim x, y As Double
a = Val(Txt_a.Text)
b = Val(Txt_b.Text)
m = Val(Txt_m.Text)
c = Val(Txt_c.Text)
d = Val(Txt_d.Text)
n = Val(Txt_n.Text)
x = (b * n - d * m) / (b * c - a * d)
y = (a * n - c * m) / (a * d - b * c)
Lbl_x.Caption = Round(x, 2)
Lbl_y.Caption = Round(y, 2)
End Sub
'to get new equations
Private Sub Cmdnew_Click()
Txt_a.Text = ""
Txt_b.Text = ""
Txt_m.Text = ""
Txt_c.Text = ""
Txt_d.Text = ""
Txt_n.Text = ""
Lbl_x.Caption = ""
Lbl_y.Caption = ""
End Sub