Visual Basic for Applications: Introduction Craig K. Tyran Western Washington University Fall 2002
Agenda Background Visual Basic for Applications (VBA) VBA Development Environment Macro Recorder Macros and Toolbars Macro Security Hands-on Exercise
A. Visual Basic: Background Beginner’s All-Purpose Symbolic Instruction Code Mid-60s: Initially created as a teaching tool Enter Microsoft … Mid-70s: Adopted/promoted a version of BASIC as a programming language for the early PCs Late-80s: Included BASIC as part of MS Word Early-90s: Modified BASIC to work with Windows environment “Visual Basic” (VB)
A. VB: Background (cont.) 3. Visual Basic (VB) Based on BASIC programming language Added editor Added visual, graphical “controls” (e.g., command buttons, check boxes, etc.) Recent survey: VB is the most popular IS development environment 4. Visual Basic for Applications (VBA) Mid-90s: Microsoft introduced VBA Support a standard “development environment” for MS Office products
B. VBA 1. Current status 2. Why learn VBA? Now a part of all MS Office products E.g., Excel, Access, Word, PowerPoint Non-Microsoft companies have licensed VBA to include in their applications 2. Why learn VBA? Leverage VB skills with MS Office products VBA The standard programming language for many Windows-based desktop applications Allows one to easily customize applications for users E.g., Create a professional, user-friendly DSS using Excel Helps developers to integrate different applications E.g., Connect an Access database to an Excel-based DSS
Excel/VBA Example: User Friendly Car Loan DSS
C. VBA Development Environment VBA program code is displayed and edited in the “Visual Basic Editor” (VBE) Windows displayed inside of VBE may include: Project Explorer window Code Module window(s) Properties window Other information …
Visual Basic Editor Example
D. Excel Macro Recorder A“macro” Macro Recorder Another name for a set of VBA code statements (i.e., a VBA program) Macros can automate simple tasks A macro can be executed in several ways Use the Tools | Macro menu approach Assign a “short cut” key (e.g., ctrl-a) “Attach” macro to a toolbar button Macro Recorder A tool that automatically converts Excel keystroke/mouse actions into VBA code Can be a useful way to quickly “record” and create VBA code associated with basic Excel tasks
D. Excel Macro Recorder (cont.) The program statements created by the Macro Recorder are stored in a code “Module” inside of the Visual Basic Editor It is easy to review (and modify) the code
E. Macros and Toolbars Adding a toolbar button – and attaching a macro to a toolbar – is surprisingly easy The approach we will use Create a new toolbar Add a new toolbar button to the toolbar Attach a macro to the toolbar button Add a “Screen Tip” to the button Descriptive text that appears when cursor hovers over the button
F. Macro Security Viruses can be hidden in macros For protection, Microsoft has created a security feature which alerts a user that a macro is included with a file When opening up an Excel file, “Security Level” setting needs to be selected to enable a common macro to be included with a file Procedure to use when working with files w/macros on WWU’s PCs Open file Change security setting to “Medium” via Tools | Macro | Security Save and close file Reopen file and “Enable Macros” when prompted
E. In-class Exercise Let’s get started!!!