VBA Presentation FAIR 08

  • Upload
    yprachi

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

  • 7/29/2019 VBA Presentation FAIR 08

    1/27

    Introduction to VBAMacros in MS Excel

    Magdy Helal & Sandra Archer

    University of Central FloridaFeb 07, 2008

    F A I R

    2008 FAIR Conference - February 6 8, 2008 Indialantic, FL

    FLORIDA ASSOCIATION FOR INSTITUTIONAL RESEARCH

  • 7/29/2019 VBA Presentation FAIR 08

    2/27

    Record and runn VBA macros

    Introduce the VB Editor VBE

    Edit a VBA macro

    Debug and Run macros in VBE

    FAIR 2008: Introduction to VBA Macros in MS Excel 2February 07, 2008

  • 7/29/2019 VBA Presentation FAIR 08

    3/27

    Event-driven programming language

    A version of the Visual Basic programming language,

    with tools, functions, and methods customized to u

    February 07, 2008 3FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    4/27

    Record, write, or edit recorded VBA code (macros) to automate a

    Macros are saved in your specific Excel workbook or globally

    acros can e assoc a e w even s; c c , ou e-c c ,keystroke, etc to fire them

    You may add Windows objects (buttons, dropdown lists, check boxes,textboxes dialo ue boxes menus etc.. then write VBA code for each

    The VB editor (VBE) is a part of Excel to view, edit, and run macros

    February 07, 2008 4FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    5/27

    Goal:

    Fill in the shown table using the given dataset (in an Excel sheet) Have the table filled automatically using a VBA macro

    Process: Study the data and plan steps to prepare the summary for 2002

    Record a VBA macro while performing these steps

    Edit the macro to use it for the rest of the years

    2002 2003 2004 2005 2006

    UCF

    FAMU

    FAU

    CountsofFemaleGraduateStudentsUnder25

    FIU

    FSU

    UF

    UNF

    USF

    February 07, 2008 5FAIR 2008: Introduction to VBA Macros in MS Excel

    FGCU

  • 7/29/2019 VBA Presentation FAIR 08

    6/27

    Ri ht-click in the toolbars area in Excel and select the Visual Basic

    toolbar from the toolbars list

    Run aVB

    EditorToolbox

    macro

    DesignMode

    Recording

    Sto

    February 07, 2008 6FAIR 2008: Introduction to VBA Macros in MS Excel

    Recording

  • 7/29/2019 VBA Presentation FAIR 08

    7/27

    Click the Record button

    This dialog box appears Give a name to the macro

    ,

    Specify where Excel saves it:

    This Workbook

    Personal Macro Workbook

    Saved in the STARTUP folder inyour Office application directory

    Becomes available whenever youuse Excel

    February 07, 2008 7FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    8/27

    ,

    Turn on the auto filter in Excel, using the first row of the data Filter to 2002, graduate, women, under 25 years of age

    Copy the filtered records and title row to the table sheet

    Add summation formulas for the filtered records

    Copy and paste-special (values & transpose) the summationcells into the table under the 2002 title

    Go back to Data sheet and remove the filter

    O en VBE to view the recorded macro next slide

    February 07, 2008 8FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    9/27

    Design ModeRun macroPause runningEnd running

    Add break oint

    Project WindowProperties window

    Object model window

    Project

    Window

    Step by step run

    CodeWindow

    February 07, 2008 9FAIR 2008: Introduction to VBA Macros in MS Excel

    Window

  • 7/29/2019 VBA Presentation FAIR 08

    10/27

    Sub SubName()

    VBA statements

    VBA statements

    End Sub

    y VBA statements deal with the Excel object model

    y Object model: collection of workbooks, sheets, columns,rows, ranges, cells, charts, formulas, filters, etc.

    y VBA statement basic uses:

    y Change the properties of an object

    February 07, 2008 10FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    11/27

    Functions /Methods

    Properties

    February 07, 2008 11FAIR 2008: Introduction to VBA Macros in MS Excel

    Definition of selected item

  • 7/29/2019 VBA Presentation FAIR 08

    12/27

    Click the Run button in the VBA menu bar Or ress Alt + F8

    From the Macro dialogue box choose a macro to run then click Run

    Step by steprunning in VBE

    (debugging)

    Start VBE

    Changeshortcut

    List ofmacros

    February 07, 2008 12FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    13/27

    Start VBE from the VBA toolbar Or ress Alt + F11

    Select the macro module in the Project Window (make sure to clickinside the macro you want to run)

    ,

    Running the macro:

    Use the icons in VBE toolbar or the Run menu Keyboard: F5 to run the current macro and Ctrl + Break to

    pause the run

    Editing the macro

    Debugging the macro Use the Step Into icon for step by step running

    Ke board: ress F8 re eatedl

    February 07, 2008 13FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    14/27

    Change the following to run the macro for the 2003 year

    Changeto 2003

    Replace with: Application.Range("A1").CurrentRegion.Select

    Changeto C3

    February 07, 2008 14FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    15/27

    An exam le of an error messa e:

    Shows if Application. is not used in Application.Range("A1").CurrentRegion.Select

    Sometimes depending on Excel version

    End: Stops executing the macro

    Debug: takes you to the error in code

    Continue: ignore the error if possible

    Common errors:

    Miss ellin

    Variables not defined

    Missing arguments or options

    -

    Not using Application. before Range, Rows, Columns, Sheets, and few others

    February 07, 2008 15FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    16/27

    Referrin to a cell: Cells Rowindex Columnindex

    Cell in row 3 and column 21

    :

    Cells(3,21).Select

    ange e , e . e ec

    Cell in row 6 and column 4: Range(D6).Select

    Range across several rows and columns: ange ce s , , e s , . e ec

    Range(D4,H12).Select

    Range of columns: Columns(FirstColIndex, LastColIndex) Columns 5,6,and 7: Columns(E,G).Select

    Ran e of rows: Columns(FirstRowIndex, LastRowIndex)

    Rows 2 through 7: Rows(2:7).Select

    February 07, 2008 16FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    17/27

    R1C1 uses numbers for columns instead of letters

    Excel uses this format when recording a macro Must be inserted as a character in the selected cell range

    " " . - -

    February 07, 2008 17FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    18/27

    Each member of the Excel ob ect model has ro erties methods

    functions, and sometimes members of its own The . operator following the object name activates its members list

    There can be several levels of membership

    February 07, 2008 18FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    19/27

    Man members re uire ar uments and ma have o tions

    When adding a comment in a cell, type ( after selecting AddComment:

    Add the comment text, e.g. This is my chosen cell

    Options do not have to be between parentheses:. = , = , _

    :=False, Transpose:=True

    February 07, 2008 19FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    20/27

    U on o enin an Excel file with a macro the followin warninappears

    To use the macro click Enable Macros

    ,

    ,allow macros

    February 07, 2008 20FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    21/27

    From the Tools menu select Macro then Securit

    Set the security level in the Security dialogue box to medium Restart the Excel file

    February 07, 2008 21FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    22/27

    We have introduced usin VBA in MS Excel How to record and run a macro

    How to use VBE

    How to debug and edit a recorded macro

    How to set the security level to allow the saved macros

    How to practice:

    Record macros while perfoming different Excel tasks and study the code Modify the code in VBE and watch the results using step by step execution

    What do you need: One or moreExcel VBA programming books and the VBA help

    Learn some general purpose VB statements The IF Thenstatement

    The FOR Nextstatement

    FAIR 2008: Introduction to VBA Macros in MS Excel 22February 07, 2008

  • 7/29/2019 VBA Presentation FAIR 08

    23/27

    Ma d Helal Research Associate [email protected]

    Sandra Archer; Director, [email protected]

    University Analysis and Planning Support

    Universit of Central Florida

    12424 Research Parkway, Suite 215

    Orlando, FL, 32826-3207

    Phone: 407-882-0285. .

    Presentation will be available at http://uaps.ucf.edu

    VBA Resources:

    MSDN: http://msdn2.microsoft.com/en-us/isv/bb190538.aspx

    , , .,

    FAIR 2008: Introduction to VBA Macros in MS Excel 23February 07, 2008

  • 7/29/2019 VBA Presentation FAIR 08

    24/27

    The Visual Basic icon bar of 2003 is 1

    If the Developer tab is not shown onamong Excel menus, add the Developertab as follows:

    1. Click the Office Button

    2. Click Excel Options

    3. Select Popular

    4. Check Show Developer tab in the Ribbon

    23

    4

    February 07, 2008 24FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    25/27

    The Developer tab appears on the Excel ribbon

    cons an uttons oo erent rom t ose n

    Buttons are pointed to All buttons do the same functions as in 2003

    Toolbox and userinterface design

    toolsRecord amacro

    tartVBE

    Show macrosdialog box

    February 07, 2008 25FAIR 2008: Introduction to VBA Macros in MS Excel

    ecur tySetting

  • 7/29/2019 VBA Presentation FAIR 08

    26/27

    Click Macro Securit in the Develo er tab

    Trust Center dialog box appears Select Macro Settings

    February 07, 2008 26FAIR 2008: Introduction to VBA Macros in MS Excel

  • 7/29/2019 VBA Presentation FAIR 08

    27/27

    When ou o en an Excel file with a macro a securit warnin a earsbelow the ribbon with the Options button

    Click Options to show the Microsoft Office Security Options dialog box

    Click Enable This Content to all the macro

    February 07, 2008 27FAIR 2008: Introduction to VBA Macros in MS Excel