Sunteți pe pagina 1din 4

Tutorial1 – Basic Calculator

In our first exercise we will construct a simple interface to get used to the Visual
Basic Language and Interface. In this program we will create program to Add,
Subtract, Multiply and Divide any two numbers.

Creating a form
To create a ‘form’ the user interface
or ‘front end’ of your program select:
Insert Userform1.
This will add a form item to your
Project manager window.

Should you ever slip out of form design mode you can get back to it by right clicking
Userform1 and selecting View Object.

On creating the form you will be


presented with a blank form and a
Toolbox consisting of familiar user
tools or controls.

Next Add 5 buttons to the form and


arrange approximately as shown.

These buttons will be used to add,


subtract, multiply and divide. The fifth
button will be used to end the
program.

Next Add 3 text boxes. The first two


will be used to accept user input.
The last will be used to display the
result.

Changing properties
We will now change some properties to make the program more self-explanatory /
user friendly. To do this we will change the Title top margin to ‘Basic Calculator:
Surname’, change the name of each button to convey the mathematical operation
performed. Add the word Exit to the 5th button. Enlarge to the font size in the used.

During development we will temporarily add values to the first two text boxes to save
having to type values in every time we want to test the program. These will ultimately
be removed.

1
Changing the form title
To change the form Heading or Title of the form
Select the ‘Userform1’.
This can be selected in the form design area or can
be selected from the drop down in the properties
section.
The specify Basic Calculator for the caption property.

Adding temporary values to Text boxes.


First of all add the number ‘5’ to Textbox1.
This is achieved by changing the value property of
textbox1. Do similar for textbox2 by adding the 2.

This is a temporary measure to make program


development easier. These numbers will be removed
in the final program.

Enlarging the font size


To change the font size, select the font type ‘Tahoma’
in under textbox1 properties, select he button
containing the three dots then in the dialog box which
appears specify a font size of 16.

Repeat the process for the other two text boxes.

Changing button text.


To change the words which appear on the buttons
this is achieved by changing the caption property.
To put the word add on the first button change the
caption property to ‘ADD’. Repeat the process for the
other 3 buttons so that they read,
‘SUBTRACT’,’MULTIPY’ and ‘DIVIDE’.

Finally add the word ‘CLOSE’ to the last button.

On completion the interface should look as shown


opposite.

Finally enlarge the button text so that it appears


similar to that shown.

Summary
So far all we have is the GUI or Graphical user interface. The main step to this
process ware, (a) creating a form, (b) adding objects to the form and (c) the design
time modification of object properties.

We must now do the appropriate programming to add functionality to the program.

2
Programming your calculator
We will now add the appropriate programming or code to make the program function
as intended. The main steps to this process are:
• Taking values form the text boxes
• Performing the necessary mathematical operation
• Placing the result in another text box.

We will first program the ‘Add’ button. To do


this double click on the add button. This will
cause the Commandbutton1 subroutine to
open.

The word ‘CommandButton1’ indicates that


you are in the code window associated with
Command-Button-1.
The word ‘Click’ indicates that the code you
are writing will be associated with the ‘Click’
event. i.e. this code will be activated by
clicking the mouse button over button1.

Next add the following code between ‘Private


Sub’ and ‘End Sub’.
a = TextBox1.Text
b = TextBox2.Text
c=a+b
TextBox3.Text = c

Note that as you type you will be presented


with a drop down list of valid options.
In this exercise we use ‘variables’ to hold or
contain the numeric quantities and perform
mathematical operations. The procedure
works as follows, the values are transferred
from the textbox to the variable (a and b), the
calculation is performed with the result
assigned to c. Finally result C his assigned to
textbox3.
Note assignment takes place right to left.

N.B. This is an example of changing an objects properties at ‘run time’.

You will notice that the numbers have just been stuck
together; they have not been added ‘mathematically’.
This is because VB is treating the numbers as regular ‘text’
or a ‘string’. Strings are usually represented by characters
inside inverted commas e.g. “ABCDE”. Numbers in
inverted commas are “12345” is just treated as a string or
a collection of characters which no mathematical meaning.
The “” are not visible but in inferred by the type of variable.

3
A text box as the name suggests can accept text of any type, both letters or
numbers. Therefore the contents / output of the text box is treated as text e.g.
“ABCDE” or “12345”.

As we want to perform a mathematical operation we must


convert the string into a numeric quantity i.e. “12345” to
12345. This is achieved by using the Val() command.
Complete the calculator repeating the same text in each
button except changing the mathematic operation to
subtract (-), multiply (*) and divide (/).

While you can return to the form and double click each
button in turn, you can also select the object from the drop
down menu. To minimize typing ‘copy and paste’ the text
from one button to the other.

Test your program to make see if is now working correctly.

Finally add the command End to the CLOSE button.

Saving your first VBA program.


A VBA program is embedded in its associated spreadsheet although it is possible to
export the export the program for use in another program.

To save you VBA program we will save the


associated spreadsheet.

First return to Excel by selecting File and Close and


Return to MS Excel. (Alt+Q)

Next save the Excel file. To save an Excel file which contain a VBA program you
must save it as a Macro Enable file.

To do this choose ‘Excel Macro-Enabled’ under


‘Save as type’. This changes the file extension from
*.XLSX to *.XLSM. Next save the file using the
appropriate name. i.e. Surname-Tutorial1.XLSM.

S-ar putea să vă placă și