You are on page 1of 31

 #Haeryip Sihombing BMFP 4653

MODULE LAB: DATABASE WITH VISUAL BASIC ON MACRO EXCEL


a. SETTING EXCEL FOR VISUAL BASIC ACTIVITIES
EXCEL 2007
For Excel 2007 user, it is required to call tab Developer on the ribbon before it can be used as Macro feature.
1.

Click Office Button Excel Option.

2.

Choose Popular and activate Show Developer tab in the Ribbon. Click OK.

[1 ]

 #Haeryip Sihombing BMFP 4653

It will appear the excel ribbon with DEVELOPER menu.


Visual Basic and Macros menu will appear after it was clicked on DEVELOPER

Note: Workbook standard Excel 97-2003 using XLS format file support Macro feature, while for Excel 2007 using XLSX format
file do not support Macro feature. However, it can be utilized using the format file XLSM and XLSB.
If you have managed the data in the file that you had activated with VB, then you are able to go for next process of
using Visual Basic. Instead, go to Control and Manage the Trust Center

[2 ]

 #Haeryip Sihombing BMFP 4653

EXCEL 2010
1 .Click File Option Customize Ribbon Developer OK.

[3 ]

 #Haeryip Sihombing BMFP 4653

2 .Click Developer will show Visual Basic and Macros on the left Menu.

[4 ]

 #Haeryip Sihombing BMFP 4653

2. CONTROL AND MANAGE THE TRUST CENTER


Every time you open Excel, there will be a box (by default) with warning statement to inform the user so
that they have to be carefully in using Macro command. In order to de-active this default, you can activate
Trust Center as follows:
1. File Option Trust Center Trust Center Setting.

[5 ]

 #Haeryip Sihombing BMFP 4653

2. Click Trusted Location [where you can save your data]. While you want to add the new location for your data,
then you have click Add New Locations as figure below.

[6 ]

 #Haeryip Sihombing BMFP 4653

3. After you decide to which you will be to save the data (as figure above), then you chose ActiveX Setting
Enable all controls (as figure below) OK.

[7 ]

 #Haeryip Sihombing BMFP 4653

4. After you click OK, then chose Macro Setting Enable all controls and Trust access to the VBA to activate
Visual Basic project (as figure below) OK.

5. In order to de-active the warning default, chose Message Bar Never show informationand Enable Trust
Center logging (as figure below) OK.

Finish.
[8 ]

 #Haeryip Sihombing BMFP 4653

3. VISUAL BASIC EDITOR


1.

To run visual basic editor Excel 2003, Tools Macro Visual Basic Editor.
While for Excel 2007 is choose Developer Visual Basic or directly press together Alt+F11. It will appear the window
of VB as below:

If the window of VB appears without Properties as figure below, you can click on Properties to show the properties of VB.

2.

The properties can be by Alphabetic or Categorized as shown in figure below.

[9 ]

 #Haeryip Sihombing BMFP 4653

3.

To show the Project made, choose Project Explorer icon

4.

While for Code, click on View table Code so that appear the box for coding activity as figure in the right side.

5.

Code window contains of Object Selector and Event Selector as below.

OBJECT SELECTOR

EVENT SELECTOR

[10 ]

 #Haeryip Sihombing BMFP 4653

6.

When to typing the code to macro, we can utilize Autolist feature to show the list, collection, properties or method used
of Object as below.

or press CTRL + J together.

7.

To make Macro, there are 2 way.


a. Type the program code in Code Window
b. Record the every steps when we use the excel as figure below.

After click Record Macro, it will appear the box of Record Macro with
Macro name is Macro1.
Change the macro name to LATIHAN

[11 ]

 #Haeryip Sihombing BMFP 4653

To fill up the shortcut key:


o Locate the mouse to the box after Ctrl and maintain to pres it. (dont release)
o Press Shift button in your keyboard and maintain (dont release)
o Press L button in your keyboard and maintain (dont release)
o Release together so that you can see the Shortcut key become : Ctrl+Shift+L

Choose the location to save the Macro in Store macro in.


Type the description: LATIHAN ME-RECORDING MACRO
Click OK.
[This means that every step will be recorded and coded in
Macro]

Type in
o sel A1 is HASOLOAN HAERY
o sel A2 is LECTURER FOR DITG 3113

[12 ]

 #Haeryip Sihombing BMFP 4653

Stop Recording

To check the recording code of Macro, press ALT+F11 together.


Find the Module 1 in the VBA Project on the left column.

a. To run Macro with recording the steps, we also can do directly by as follows:
o
o
o

Open Excel
ALT + F8
After Macro box open, type the Macro Name (Latihan) and the press Create so that appear Module 1(Code
box).

[13 ]

 #Haeryip Sihombing BMFP 4653

o
o
o
o
o
o
o

8.

While in the Excel, click Developer Macros menu.


It will appear Macro box that the name is Latihan. We can record the step by click Run.
Start Record by click Record Macro in the Excel
Type in sel 1: HASOLOAN HAERY
Type in sel 2: LECTURER FOR DITG 3113
Click Stop Recording
ALT + F11 so that the code of Macro appear.

To save the Macro file, generally Microsoft Excel will save the file with *.XLS (or *.XLSX for Excel 2010). However, to
save the excel files with Macro content, the file should be saved with *.XLSM (Excel Macro Enabled Workbook).

[14 ]

 #Haeryip Sihombing BMFP 4653

EXAMPLE :
1.

RUNNING THE MACRO

a.

Membuat Macro Excel : Open Excel

b.

Click Developer Macros Write on Macro name "Hallo"

[15 ]

 #Haeryip Sihombing BMFP 4653

c.

In the Macro Box, chose "This Workbooks" Create

d.

It will appear as figure below.

e.

Type in the Module Box : "Hallo Apakabar....Apakah anda sudah boleh membuat MACRO VBA?"

[16 ]

 #Haeryip Sihombing BMFP 4653

f.

File Close and Return to Microsoft Excel

g.

Everytime you open the Macros in Excel, it will appear as figure below.

h.

When you type "Hallo" Run, you will see the Box as picture below.

i.

If you press OK, this is meant you are working in Macros environment.

j.

Finish

[17 ]

 #Haeryip Sihombing BMFP 4653

2.

MAKE THE CALCULATION

a. Activated Microsoft Excel Developer Visual Basic.

b. To make the new form, double click on ThisWorkbook to appear the Box.

[18 ]

 #Haeryip Sihombing BMFP 4653

c. Right Click on "ThisWorkbook" Insert User Form.

[19 ]

 #Haeryip Sihombing BMFP 4653

d. To generate the form object, you can use the Control Toolbox.

Chose text box []

Chose Label []

Type "LENGTH", "WIDTH", and "Calculation for AREA" as picture below.

Chose Label []and ] drag to the UseForm and type "CALCULATE" as picture below.

] in the ToolBox and drag to UseForm as figure below.

] and drag to the UseForm.

[20 ]

 #Haeryip Sihombing BMFP 4653

e. Manage the TextBox: active on Length Blank Box and Type "TextBoxLENGHT" .

f.

Be ensured the Enabled is "TRUE". Do the same way for 'WIDTH", while for "Calculation for AREA" the
Enabled is "FALSE".
For "CALCULATE", change the CAPTION to: CALCULATE.

To generate the formula for "CALCULATE" : double click on CALCULATE box.


Type the command program as below. [There are pop-up will shows to help you to make the command].

[21 ]

 #Haeryip Sihombing BMFP 4653

g. Run the program by pressing F5 or RUN menu to show the filling form as below.

h. After the UserForm appear, then type the number on blank column. e.g. LENGTH = 10 ; WIDTH = 20 and then press
CALCULATE. The result shows in the Calculation for AREA (=200).

*** HOW YOUR RESULT? [DO YOU FIND YOUR PROGRAM IS FALSE???, IF YES, WHY? DISCUSS!!!]

[22 ]

 #Haeryip Sihombing BMFP 4653

Answer:
* You can revise your program as below:

OR

The reason is due to Calculate is a command; not as manipulation of what you want from the process.

b. Try to change "Calculation" with "Cal", what your result show?

c. By double clicking on "UseForm1" and changing the TextBoxCalculationfor AREA to become "FALSE" and then
execute the program by double clicking the CALCULATE box and then pressing F5 or Run the program, what the
result? Can you type the data inside the blank and get the result needed?

[23 ]

 #Haeryip Sihombing BMFP 4653

3.

MAKE THE FORM-1

a. Make a form in excel as below.

[24 ]

 #Haeryip Sihombing BMFP 4653

b. Click Visual Basic tools on the Developer menu and then right click to show Insert User Form.

c. After show the working environment, it is required to manage the properties form as below.

[25 ]

 #Haeryip Sihombing BMFP 4653

d. Change or type in the Properties UserForm1 as below:


o
o
o

(Name) : BOOKRENT
Caption: BOOK BORROEWER
Enabled: True

e. Make objects as input media into form as figure below.

Drag

(Label) from toolbox to BOOK BORROWER box and type: "NAME"

Drag

(TextBox) from toolbox to BOOK BORROWER box.

Repeat this step for "ADDRESS" and "PHONE", while for "BOOK" use
(ComboBox); "NEW" and "OLD" use
(CheckBox) and "YES" or "NO" use
(Option Button).
In Figure below, for "NEW" and "OLD" or "YES" or "NO" are circled with Frame
For OK button using Command Button.

o
o

[26 ]

 #Haeryip Sihombing BMFP 4653

f.

Type the complete command as below and RUN the program by pressing F5 or RUN menu.

[27 ]

 #Haeryip Sihombing BMFP 4653

g. Fill and tick the Blank in Box and pressing OK to end the cycle so that in the excel shows as below.

[28 ]

 #Haeryip Sihombing BMFP 4653

4.

MAKE THE FORM-2

a. Make a form in excel as below.

b. Change the worksheet and type: "DataMaterial"

c. Click Visual Basic tools on the Developer menu and then right click to show Insert User Form.
d. Follow the process 3d until 3e. However, the LOCATION is 'ComboBox, while TOTAL Prices is Quantity x Price.
e.g. LOCATION are: Makmal Bahan, Makmal Proses, Makmal Rekabentuk, Makmal Robotik and Makmal Pengurusan.

e. Type the program for LOCATION as below.

[29 ]

 #Haeryip Sihombing BMFP 4653

f.

Type for the SAVE as below

g. The last, type for the FINISH as below

[30 ]

 #Haeryip Sihombing BMFP 4653

h. After complete the program then press F5 or RUN menu.

i.

Fill up the data on database by writing on the Material Data Box and do repeatedly based on the data you have.

FINISH

[31 ]

You might also like