1
An Introduction to Excel VBA
Excel VBA is probably the most commonly used computational tool in financial institutions, particularly when a new model is tested at a preliminary stage within a division. Many traders use Excel VBA to compute their trading strategies. Some data providers allow users to update information in real time using the Excel format. Excel VBA thus allows traders and risk managers to implement their solutions conveniently in real time.
1.1 HOW TO START EXCEL VBA
1.1.1 Introduction
VBA stands for Visual Basic for Application. It is a programming language that enhances the applicability of MS Excel by enabling the users to instruct Excel to perform tasks automatically. As most of the programs in this book are written in VBA, a brief introduction to VBA is provided in this opening chapter. Although we do not assume that readers have prior programming knowledge, programming experience in other languages would be helpful. For readers already familiar with VBA, this chapter serves as a refresher and quick reference. A list of the functions defined throughout the book can be found at the end of the chapter. These functions not only improve readability and traceability but also simplify the programs. For a more thorough understanding of Excel VBA, readers are referred to other books specializing in the matter. We believe, however, that this chapter is sufficient to allow a beginner to learn and execute the codes within the book.
MS Excel 2003 is used for illustration in this book. If readers are using another version of Excel, then they may find some minor differences. Nevertheless, if this is the first time for a reader to use Excel VBA, then set the macro security level to Medium or Low and restart Excel to enable the macros:
Click [Tools] → [Macro] → [Security] → [Medium] or [Low] (Fig. 1.1).
MS Excel 2007 users should click the Options button to enable the macros.
1.1.2 Visual Basic Editor
VBE, which stands for Visual Basics Editor, is the environment in which macros are created, modified and managed. Macros (VBA procedures) are the code components that automate repetitive Excel tasks. A macro consists of codes that start with the keyword Sub or Function and end with the keywords End Sub or End Function. These codes are known as Sub and Function procedures. A module contains one or more macros, and a project contains one or more modules. A macro developed in VBE becomes part of a workbook and is saved at the same time that the workbook is saved. To open and edit macros in VBE, follow the procedure below.
1. Open VBE: Click [Tools] → [Macro] → [Visual Basic Editor] or press Alt + F11 (Fig. 1.2).
2. Insert module: In the project window on the left of the VBE, right-click one of the worksheets → [Insert] → [Module] (Fig. 1.3).
3. Edit in VBE: Type the codes in the code window.
4. Execute the program: In VBE, click [Run] → [Run Sub] and choose the macro to be compiled. Equivalently, in Excel, click [Tools] → [Macro] → [Macro] and choose the macro to be compiled.
1.1.3 The Macro Recorder
Excel offers a macro recorder that records the actions of the mouse and/or keyboard and translates them into VBA codes, thus allowing the designated actions to be repeated by running the macro again. Although the macro recorder is sometimes useful, it is unable to generate codes that perform looping, assign variables, or execute conditional statements, which are...