Module Lab - Excel Macro

download Module Lab - Excel Macro

of 31

Transcript of Module Lab - Excel Macro

  • 8/17/2019 Module Lab - Excel Macro

    1/31

     Haeryip Sihombing – BMFP 4653

    [1 ]

    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.

  • 8/17/2019 Module Lab - Excel Macro

    2/31

     Haeryip Sihombing – BMFP 4653

    [2 ]

    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 formatfile 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  

  • 8/17/2019 Module Lab - Excel Macro

    3/31

     Haeryip Sihombing – BMFP 4653

    [3 ]

      EXCEL 2010

    1 .Click File→ Option→ Customize Ribbon→ Developer→ OK. 

  • 8/17/2019 Module Lab - Excel Macro

    4/31

     Haeryip Sihombing – BMFP 4653

    [4 ]

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

  • 8/17/2019 Module Lab - Excel Macro

    5/31

     Haeryip Sihombing – BMFP 4653

    [5 ]

    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 activateTrust Center as follows:

    1.  File→ Option→ Trust Center→ Trust Center Setting.

  • 8/17/2019 Module Lab - Excel Macro

    6/31

     Haeryip Sihombing – BMFP 4653

    [6 ]

    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.

  • 8/17/2019 Module Lab - Excel Macro

    7/31

     Haeryip Sihombing – BMFP 4653

    [7 ]

    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. 

  • 8/17/2019 Module Lab - Excel Macro

    8/31

     Haeryip Sihombing – BMFP 4653

    [8 ]

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

    5.  In order to de-active the warning default, chose Message Bar → Never show information…and Enable TrustCenter logging (as figure below)→ OK. 

    Finish. 

  • 8/17/2019 Module Lab - Excel Macro

    9/31

     Haeryip Sihombing – BMFP 4653

    [9 ]

    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 windowof 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.

  • 8/17/2019 Module Lab - Excel Macro

    10/31

     Haeryip Sihombing – BMFP 4653

    [10 ]

    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.

  • 8/17/2019 Module Lab - Excel Macro

    11/31

     Haeryip Sihombing – BMFP 4653

    [11 ]

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

    or press CTRL + J together.

    7.  To make Macro, there are 2 way.a.  Type the program code in Code Windowb.  Record the every steps when we use the excel as figure below.

     

     After click Record Macro, it will appear the box of Record Macro withMacro name is Macro1.

      Change the macro name to LATIHAN

  • 8/17/2019 Module Lab - Excel Macro

    12/31

     Haeryip Sihombing – BMFP 4653

    [12 ]

     

    To fill up the shortcut key:o  Locate the mouse to the box after Ctrl and maintain to pres it. (don’t release)o

     

    Press Shift button in your keyboard and maintain (don’t release)o  Press L button in your keyboard and maintain (don’t 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 inMacro]

      Type ino

     

    sel A1 is HASOLOAN HAERYo  sel A2 is LECTURER FOR DITG 3113

  • 8/17/2019 Module Lab - Excel Macro

    13/31

     Haeryip Sihombing – BMFP 4653

    [13 ]

    o  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

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

    box).

  • 8/17/2019 Module Lab - Excel Macro

    14/31

     Haeryip Sihombing – BMFP 4653

    [14 ]

    o  While in the Excel, click Developer→ Macros menu.o  It will appear Macro box that the name is Latihan. We can record the step by click Run.o  Start Record by click Record Macro in the Excelo

     

    Type in sel 1: HASOLOAN HAERYo  Type in sel 2: LECTURER FOR DITG 3113o  Click Stop Recordingo   ALT + F11 so that the code of Macro appear.

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

  • 8/17/2019 Module Lab - Excel Macro

    15/31

     Haeryip Sihombing – BMFP 4653

    [15 ]

    EXAMPLE :

    1.  RUNNING THE MACRO

    a. 

    Membuat Macro Excel : Open Excel

    b.  Click Developer→ Macros→ Write on Macro name "Hallo"

  • 8/17/2019 Module Lab - Excel Macro

    16/31

     Haeryip Sihombing – BMFP 4653

    [16 ]

    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?"

  • 8/17/2019 Module Lab - Excel Macro

    17/31

     Haeryip Sihombing – BMFP 4653

    [17 ]

    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

  • 8/17/2019 Module Lab - Excel Macro

    18/31

     Haeryip Sihombing – BMFP 4653

    [18 ]

    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.

  • 8/17/2019 Module Lab - Excel Macro

    19/31

     Haeryip Sihombing – BMFP 4653

    [19 ]

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

  • 8/17/2019 Module Lab - Excel Macro

    20/31

     Haeryip Sihombing – BMFP 4653

    [20 ]

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

    •  Chose text box [] ] in the ToolBox and drag to UseForm as figure below.

    •  Chose Label [] ] and drag to the UseForm.

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

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

  • 8/17/2019 Module Lab - Excel Macro

    21/31

     Haeryip Sihombing – BMFP 4653

    [21 ]

    e. 

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

    •  Be ensured the Enabled is "TRUE". Do the same way for 'WIDTH", while for "Calculation for AREA" theEnabled is "FALSE".

    •  For "CALCULATE", change the CAPTION to: CALCULATE.

    f.  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].

  • 8/17/2019 Module Lab - Excel Macro

    22/31

     Haeryip Sihombing – BMFP 4653

    [22 ]

    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 pressCALCULATE. The result shows in the Calculation for AREA (=200).

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

  • 8/17/2019 Module Lab - Excel Macro

    23/31

     Haeryip Sihombing – BMFP 4653

    [23 ]

    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 thenexecute the program by double clicking the CALCULATE box and then pressing F5 or Run the program, what theresult? Can you type the data inside the blank and get the result needed?

  • 8/17/2019 Module Lab - Excel Macro

    24/31

     Haeryip Sihombing – BMFP 4653

    [24 ]

    3.  MAKE THE FORM-1

    a.  Make a form in excel as below.

  • 8/17/2019 Module Lab - Excel Macro

    25/31

     Haeryip Sihombing – BMFP 4653

    [25 ]

    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.

  • 8/17/2019 Module Lab - Excel Macro

    26/31

     Haeryip Sihombing – BMFP 4653

    [26 ]

    d.  Change or type in the Properties – UserForm1 as below:o  (Name) : BOOKRENTo  Caption: BOOK BORROEWERo  Enabled: True

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

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

    o  Drag (TextBox) from toolbox to BOOK BORROWER box.

    o  Repeat this step for "ADDRESS" and "PHONE", while for "BOOK" use(ComboBox); "NEW" and "OLD" use (CheckBox) and "YES" or "NO" use(Option Button).

    o  In Figure below, for "NEW" and "OLD" or "YES" or "NO" are circled with Frameo  For OK button using Command Button.

  • 8/17/2019 Module Lab - Excel Macro

    27/31

     Haeryip Sihombing – BMFP 4653

    [27 ]

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

  • 8/17/2019 Module Lab - Excel Macro

    28/31

     Haeryip Sihombing – BMFP 4653

    [28 ]

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

  • 8/17/2019 Module Lab - Excel Macro

    29/31

     Haeryip Sihombing – BMFP 4653

    [29 ]

    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.

  • 8/17/2019 Module Lab - Excel Macro

    30/31

     Haeryip Sihombing – BMFP 4653

    [30 ]

    f.  Type for the SAVE as below

    g.  The last, type for the FINISH as below

  • 8/17/2019 Module Lab - Excel Macro

    31/31

     Haeryip Sihombing – BMFP 4653

    [31 ]

    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