You are on page 1of 6

Visual Basic for Application

Ex1. The Arithmetic Program


1. Open MS Excel
2. Select Developer tab Visual basic (See Fig 1.). Shortcut is by pressing Alt +
F11
3. From the menu, select Insert, then choose UserForm. A form with the name
UserForm1 and a toolbox containing controls will be displayed
4. Click View and choose project explorer and properties window if they are not
displayed,
5. To add controls, just select the desired control and insert it anywhere in the
form. Add the following controls: command (8), textbox (5), labels (5) . See
Fig. 2
6. Set the properties of the controls according to Table 1.
Table 1
The Properties of The Arithmetic Program
Object
UserForm1

Property
Name
Caption
Name

Value
frmArithmetic
The Arithmetic Program
txtNum1

Text box2
Text box3
Text box4
Text box5
Label1

Name
Name
Name
Name
Name
Caption

Label2

Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption

txtNum2
txtAnswer
txtString1
txtString2
lblInfo
This program demonstrates arithmetic
operations
lblInt1
Enter the first of two numbers
lblInt2
Enter the second of two numbers
lblString1
Type first string
lblString2
Type second string
cmdAdd
Add
cmdSubtract
Subtract
cmdMultiply
Multiply
cmdDivide
Divide
cmdCombine
Concatenate
cmdClear
Clear

Text box1

Label3
Label4
Label5
Command1
Command2
Command3
Command4
Command5
Command6

Command7
Command8

Fig. 1

Name
Caption
Name
Caption

cmdExit
Exit
cmdConString
Concatenate String

Fig. 2
Writing the codes
Note: You can just copy the codes from this page then paste it in the Visual basic
page.
Each time after you have written the code, run it by pressing F5 or choose Run
from the menu.
To switch between Code view and object view , click these icons located just
below the Project explorer.
1. The simplest code is for exit. Double click the Exit button and you will see
(by default)
Private Sub cmdExit_Click()
End Sub
2. Type End between the two lines so that it becomes
Private Sub cmdExit_Click()
End
End Sub
3. To see the effect of this code you run the program by pressing F5 or choose
Run in the menu then choose Run Sub/Userform. Click the exit button and it
will close the form

4. Now double-click the Add button. Again you will see the two lines
Private Sub cmdAdd_Click()
End Sub

5. Add the following codes (for Add button)so that when complete it looks like
Private Sub cmdAdd_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 + Num2
txtAnswer = Ans
End Sub
6. Repeat the same for the other buttons i.e Multiply, Divide and Concatenate.
The following are the codes for each button. Remember first double click that
button (i.e Multiply, Divide and Concatenate) then insert the codes between
the two lines Private Sub and End Sub.
7. You can actually copy and paste this code. Copyfrom this page and then
paste it beteen the two lines.
Codes for Subtract (the operator is -)
Private Sub cmdSubtract_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 - Num2
txtAnswer = Ans
End Sub
Codes for Multiply(the operator is *)
Private Sub cmdMultiply_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 * Num2

txtAnswer = Ans
End Sub

Codes for Divide ((the operator is /)


8. Private Sub cmdDivide_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 / Num2
txtAnswer = Ans
End Sub
Codes for Concatenate (the operator is &)
The Concatenate operator joins the two numbers together
Private Sub cmdCombine_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 & Num2
txtAnswer = Ans
End Sub
9. We need to clear the entries (numbers and answers) if we want to repeat the
calculations using different numbers. We have created the Clear button or
control. The code to clear is as follows:
Codes for Clear
Private Sub cmdClear_Click()
txtNum1 = ""
txtNum2 = ""
txtAnswer = ""
End Sub
10.We will use the concatenate operator on string datatype ie. text or numbers
+ text.
Codes for Concatenate strings
Private Sub cmdConString_Click()
txtAnswer = txtString1 & txtString2

End Sub
11.To clear the input for the strings, add two more lines to the Clear button. The
complete code for the Clear button looks like this
Private Sub cmdClear_Click()
txtNum1 = ""
txtNum2 = ""
txtAnswer = ""
txtString1 = ""
txtString2 = ""
End Sub
12.Final display when you run the program is as shown in Fig. 3. (Note the
concatenate string button was added later so it was not seen in Fig. 2)

Fig.3

You might also like