Excel VBA for Finance

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which of the following applications is Excel particularly well-suited for?

  • Financial modeling (correct)
  • Operating system development
  • Video game design
  • Web server management

What does the Excel VBA IDE provide to aid in code development?

  • A virtual reality interface
  • An automated marketing tool
  • A code editor and debug functions (correct)
  • A database management system

Which term best describes an element within the Excel application that can be manipulated using VBA?

  • Algorithm
  • Function
  • Object (correct)
  • Variable

Using VBA, how does one modify a property of an Excel object?

<p>Using the syntax <code>&lt;Object&gt;.&lt;Property&gt; = &lt;value&gt;</code> (D)</p> Signup and view all the answers

What do ‘methods’ represent in the context of Excel VBA objects?

<p>Actions that an object can perform (C)</p> Signup and view all the answers

Which statement accurately describes how Excel objects are organized?

<p>Objects are stored within collections and have a hierarchical structure. (D)</p> Signup and view all the answers

What is the primary implication of applying a method or changing a property value at the 'collection' level in VBA?

<p>It applies to each of the objects within the collection. (B)</p> Signup and view all the answers

Which Excel object is the parent of both the 'Worksheets' collection and the 'Charts' collection?

<p>Workbook (D)</p> Signup and view all the answers

What character is used to separate various objects and collections when accessing Excel objects in VBA?

<p>Dot (.) (A)</p> Signup and view all the answers

Given the code Workbooks("UE111.xlsx").Worksheets("Sheet3").Activate, what is the purpose of the Activate method?

<p>To make a specific worksheet active (C)</p> Signup and view all the answers

Which of the following is NOT a valid way to specify a range of cells in VBA?

<p><code>Worksheets.Range(&quot;A1:C3&quot;)</code> (D)</p> Signup and view all the answers

In VBA, what does the Offset property of a Range object allow you to do?

<p>Shift the range relative to the initial range (B)</p> Signup and view all the answers

In VBA, which property is used to assign a name to a range of cells?

<p>Name (D)</p> Signup and view all the answers

Which property is used to insert a formula into a cell using VBA?

<p>Formula (A)</p> Signup and view all the answers

What is the key difference between using Formula and FormulaR1C1 properties in VBA?

<p><code>Formula</code> uses A1-style references, while <code>FormulaR1C1</code> uses R1C1-style references. (B)</p> Signup and view all the answers

What is the purpose of creating 'code modules' in VBA?

<p>To store VBA command lines (code) (A)</p> Signup and view all the answers

In VBA, what are 'UserForms' primarily used for?

<p>Creating interactive user interfaces. (D)</p> Signup and view all the answers

What is the main distinction between a 'Sub' procedure and a 'Function' procedure in VBA?

<p>A 'Function' procedure must return a result, while a 'Sub' procedure does not. (D)</p> Signup and view all the answers

In VBA, which declaration keyword is used to specify that a variable's value should be preserved between calls to the procedure?

<p>Static (A)</p> Signup and view all the answers

What does defining a variable as Public within a module achieve?

<p>It makes the variable accessible from any procedure in any module within the project. (A)</p> Signup and view all the answers

Which of the following VBA data types consumes the least memory?

<p>Boolean (D)</p> Signup and view all the answers

In VBA, what happens if you do not explicitly declare a variable's data type?

<p>The variable is assigned the Variant data type. (A)</p> Signup and view all the answers

In VBA, what is the purpose of the Set keyword when working with object variables?

<p>To assign an object to the object variable. (B)</p> Signup and view all the answers

In VBA, what is a Dynamic Array?

<p>An array with a dimension (size) that can be changed during runtime. (C)</p> Signup and view all the answers

What is the purpose of 'Option Base' in VBA?

<p>To define the starting index for array elements. (C)</p> Signup and view all the answers

Which code block represents the correct syntax for an If...Then...Else statement in VBA?

<p><code>If condition Then ... Else ... End If</code> (D)</p> Signup and view all the answers

Which operator is used for integer division in VBA?

<p>\ (B)</p> Signup and view all the answers

What is the primary purpose of 'comparison' operators in VBA?

<p>Evaluating conditions. (B)</p> Signup and view all the answers

Which logical operator returns True only if both conditions being evaluated are True?

<p>And (D)</p> Signup and view all the answers

When should the Select Case statement be preferred over a series of If...Then...ElseIf statements?

<p>When evaluating a single expression against multiple possible values. (D)</p> Signup and view all the answers

Which type of loop is most suitable when you need to iterate through all elements of a collection?

<p><code>For Each...Next</code> loop (C)</p> Signup and view all the answers

What is the key difference between a Do While loop and a Do Until loop?

<p>A <code>Do While</code> loop executes as long as the condition is <code>True</code>, while a <code>Do Until</code> loop executes as long as the condition is <code>False</code>. (B)</p> Signup and view all the answers

What is the purpose of the Excel Macro Recorder?

<p>To record a series of actions and generate the corresponding VBA code (D)</p> Signup and view all the answers

What is a 'breakpoint' in the context of VBA debugging?

<p>A location in the code where the debugger will pause execution (A)</p> Signup and view all the answers

What functionality does defining a 'watch' provide during VBA debugging?

<p>Monitors the value of a variable or expression. (C)</p> Signup and view all the answers

A workbook object wb has a worksheet named “Summary”. Which of the following will correctly refer to cell A1 on that worksheet?

<p><code>wb.Sheets(&quot;Summary&quot;).Range(&quot;A1&quot;)</code> (D)</p> Signup and view all the answers

What will be displayed in the message box when the following VBA code is executed? Sub Example(): Dim x As Integer: x = 10: Call ModifyValue(x): MsgBox x: End Sub, Sub ModifyValue(ByRef y As Integer): y = y * 2: End Sub

<p>20 (B)</p> Signup and view all the answers

What happens to the value of a Static variable upon exiting the procedure in which it is declared?

<p>It retains its value for the next time the procedure is called. (B)</p> Signup and view all the answers

In VBA, what is an object?

<p>Any element of the Excel application that can be manipulated. (D)</p> Signup and view all the answers

Which of the following is an example of an Excel object?

<p>A cell within a worksheet. (B)</p> Signup and view all the answers

What are the two key features that describe the behavior of an Excel object?

<p>Properties and Methods. (C)</p> Signup and view all the answers

In VBA, what does a 'property' of an object represent?

<p>A characteristic or attribute of the object. (A)</p> Signup and view all the answers

What is meant by a 'method' in the context of Excel VBA objects?

<p>An action that the object can perform. (B)</p> Signup and view all the answers

To change the background color of cell 'A1' to blue using VBA, which property of the Range object would you modify?

<p>Interior.Color (B)</p> Signup and view all the answers

To cut the contents of cell A1 and paste them into cell A2, which method would you use?

<p>Cut (D)</p> Signup and view all the answers

What is a 'collection' in the context of the Excel object model?

<p>A set of objects sharing the same characteristics. (D)</p> Signup and view all the answers

Which of the following is an example of an Excel 'collection'?

<p>Worksheets (C)</p> Signup and view all the answers

What is typically the naming convention to identify Excel objects?

<p>Collections have names ending with ‘s’. (B)</p> Signup and view all the answers

If you apply a property value change or a method at the 'collection' level (e.g., to all worksheets), what is the implication?

<p>It applies to each of the objects within the collection. (B)</p> Signup and view all the answers

In the Excel object hierarchy, which object is the 'parent' of the 'Worksheets' collection?

<p>Workbook (B)</p> Signup and view all the answers

Which symbol is used to separate objects and collections when accessing Excel objects in VBA?

<p>. (D)</p> Signup and view all the answers

In VBA, what does the term 'Range' refer to?

<p>Individual cells, ranges of cells, and/or groups of cells. (A)</p> Signup and view all the answers

Which of the following is NOT a valid way to refer to a range of cells?

<p><code>Rows(&quot;1:5&quot;).Columns(&quot;A:B&quot;)</code> (A)</p> Signup and view all the answers

In VBA, which property is used to assign a name to a range of cells, allowing you to refer to it by that name later in your code?

<p>Name (C)</p> Signup and view all the answers

To insert a formula into cell C5 using VBA, which property of the Range object would you typically use?

<p>Formula (A)</p> Signup and view all the answers

What referencing style does the FormulaR1C1 property use in VBA?

<p>R1C1 (B)</p> Signup and view all the answers

What syntax creates an absolute reference to row (column) i, where i is an integer different from 0, using the FormulaR1C1 property?

<p>RiCi (C)</p> Signup and view all the answers

What are the main components in Excel VBA projects?

<p>Worksheets, Modules and UserForms (C)</p> Signup and view all the answers

Which type of module would be used to store standard VBA code?

<p>Code modules (D)</p> Signup and view all the answers

What is the primary use of UserForms in VBA?

<p>Creating user-defined interactive interfaces (B)</p> Signup and view all the answers

What are the two main procedure types in VBA?

<p>Functions and Subs. (D)</p> Signup and view all the answers

What are Sub procedures designed to do?

<p>Perform a specific action. (A)</p> Signup and view all the answers

If you want a procedure to calculate and return a value, which type of procedure should you use?

<p>Function Procedure (B)</p> Signup and view all the answers

When defining a Sub procedure, which of the following statements is true?

<p>A Sub procedure performs a given task and requires no obligation to return a result. (B)</p> Signup and view all the answers

What is the purpose of the Call keyword in VBA?

<p>Execute a Sub procedure. (B)</p> Signup and view all the answers

Which declaration statement will make a procedure that is only able to be recognized within the module it belongs to?

<p>Private Sub (A)</p> Signup and view all the answers

If a variable is declared as Static within a procedure, what happens to its value each time the procedure is run?

<p>It retains it's value from the last time the code was run. (D)</p> Signup and view all the answers

Which of the following VBA types consumes the most memory?

<p>Double (A)</p> Signup and view all the answers

What is the default data type assigned to a variable if it is not explicitly declared in VBA?

<p>Variant (B)</p> Signup and view all the answers

What is the main purpose of the Set keyword in VBA?

<p>To assign an object reference to a variable. (A)</p> Signup and view all the answers

What is a 'dynamic array' in VBA?

<p>An array whose size can be changed during runtime. (C)</p> Signup and view all the answers

What happens if Option Base 1 is declared?

<p>It defines the base index for arrays to be 1 instead of 0. (B)</p> Signup and view all the answers

Which arithmetic operator isn't like the others?

<p>\ (A)</p> Signup and view all the answers

In VBA, what is the purpose of comparison operators such as >, <, and =?

<p>Evaluate conditions. (A)</p> Signup and view all the answers

When using the And logical operator, what condition must be met for the entire expression to be True?

<p>Both conditions must be True. (B)</p> Signup and view all the answers

When is the Select Case statement preferable over a series of If...Then...ElseIf statements?

<p>When evaluating a single expression against multiple possible values. (A)</p> Signup and view all the answers

In VBA, what is a For Each...Next loop primarily used for?

<p>Iterating through each element in a collection. (D)</p> Signup and view all the answers

What key aspect differentiates Do While and Do Until loops?

<p><code>Do While</code> continues as long as a condition is true, whereas <code>Do Until</code> continues as long as a condition is false. (D)</p> Signup and view all the answers

What is the primary advantage of using VBA to manipulate Excel objects, compared to directly interacting with them?

<p>VBA allows for automation of repetitive tasks and increased computation capabilities. (A)</p> Signup and view all the answers

In the Excel object model, what is the significance of 'collections' related to Excel objects?

<p>Collections group objects of similar characteristics, allowing you to apply the same method or property change to all members. (A)</p> Signup and view all the answers

When using the Formula property in VBA, what distinguishes a relative reference from an absolute reference in a formula assigned to a cell?

<p>Relative references automatically update when copied, while absolute references remain fixed. (A)</p> Signup and view all the answers

What is the key distinction between 'Code modules' and 'Class modules' within a VBA project?

<p>'Code modules' contain standard VBA command lines, whereas 'Class modules' are used for defining new objects. (A)</p> Signup and view all the answers

If you declare a variable without specifying a data type, VBA assigns it the 'Variant' type. While versatile, what is a potential drawback of using the 'Variant' data type extensively?

<p>It can reduce the effectiveness and efficiency of your code due to higher memory and time consumption. (C)</p> Signup and view all the answers

Flashcards

What is an Excel object?

Any element of the Excel application you can interact with.

What are object properties?

Characteristics such as color, height, width, and font.

What are object methods?

Actions an object can perform, such as Select, Cut, and Paste.

What is a Collection?

A set of objects sharing the same characteristics.

Signup and view all the flashcards

What is the Excel objects hierarchy?

The Excel application at the top, workbooks, worksheets, ranges, etc.

Signup and view all the flashcards

What are range objects?

Objects that allow you to define individual cells, ranges, or groups of cells.

Signup and view all the flashcards

What does 'Range' refer to?

Single cell, range of cells, group of cells.

Signup and view all the flashcards

What is EntireRow?

Returns a Range object representing the entire row(s) containing the range.

Signup and view all the flashcards

What is EntireColumn?

Returns a Range object representing the entire column(s) containing the range.

Signup and view all the flashcards

What does End(<xlDirection>) do?

Obtained Range starts from an initial Range by pressing End + Arrow key.

Signup and view all the flashcards

What is the 'Select' method?

The method that selects a Range object.

Signup and view all the flashcards

What is the 'Activate' method?

The method that activates a cell.

Signup and view all the flashcards

What is a relative reference?

Writing the row number/column letter of the corresponding row/column.

Signup and view all the flashcards

What is an Absolute reference?

Putting a $ symbol before the row number or column letter.

Signup and view all the flashcards

What is a project?

Applications in VBA are structured in a...

Signup and view all the flashcards

What is a Class module?

A type of module used for defining new objects (for advanced users).

Signup and view all the flashcards

What is a Code module?

A module containing VBA command lines.

Signup and view all the flashcards

What are UserForms used for?

Objects corresponding to user-defined interactive interfaces.

Signup and view all the flashcards

What is a procedure?

Set of instructions aimed at performing a given task.

Signup and view all the flashcards

What are Sub procedures?

Procedures that perform a task with no obligation to return a result.

Signup and view all the flashcards

What is the syntax for a sub-procedure?

Name, Instructions. End.

Signup and view all the flashcards

What are Function procedures?

A procedure that must return a result.

Signup and view all the flashcards

What are common variable types?

Boolean, Numerical, String, Object, Variant

Signup and view all the flashcards

What are Object variables?

Variables aimed at establishing a reference to an object.

Signup and view all the flashcards

What does the variable type 'Variant' do?

Accepts all other types, takes more memory.

Signup and view all the flashcards

What does 'Declaring variables' mean?

Assigning a type to a variable.

Signup and view all the flashcards

What is an array?

Structure made of cells which all contain info of the same type.

Signup and view all the flashcards

What is procedure scope?

The variable is accessible only from within the procedure in which it is declared.

Signup and view all the flashcards

What is private module scope?

Variable accessible only from within the module in which it is declared.

Signup and view all the flashcards

What is public module scope?

Variable accessible from any procedure in any module in the project.

Signup and view all the flashcards

What are dynamic variables?

Variables whose number of dimension can change over time.

Signup and view all the flashcards

What are Control statements?

Instructions that modify the behavior of a program.

Signup and view all the flashcards

What is a Conditional Statment?

Allow to redirect the execution of a program.

Signup and view all the flashcards

What are common operator types?

Arithmetic, Comparison, Logical

Signup and view all the flashcards

What is the use of an operator with '='?

Assigns a value to a variable of the worksheet.

Signup and view all the flashcards

What are Loops used for?

Repeated execution of a group of instructions.

Signup and view all the flashcards

What is the 'For Each Next' loop?

Applies a change to each object of code.

Signup and view all the flashcards

Study Notes

  • Excel VBA Programming
  • Financial Modeling and Applications, UE 111 is covered in this course.
  • The course covers:
    • Introduction
    • Excel object model
    • The structure of Visual Basic projects
    • Visual Basic language
    • Miscellaneous

Course Objectives

  • Learn how to use Excel + VBA to solve problems in finance, covering:
    • General principles of VBA language (object-oriented)
    • Excel object model
    • Fundamentals of VBA language (variables, loops, tests)
    • Standard problems in finance (diversification, optimal portfolios, option pricing)

Teaching Method

  • Course: Consists of 21 hours, broken into 7 sessions lasting 3 hours each
  • Tutorial sessions (TD): 15 hours in total, split into 10 sessions of 1.5 hours each

Grading

  • Tutorial sessions account for 15% of the grade
  • The Midterm exam (MCQ + short answers) accounts for 35% of the grade
  • The Final exam accounts for 50% of the grade

Course Outline

  • Part I: Introduction to Visual Basic for Applications (VBA) in Excel
    • Chapter 1: Excel object model
    • Chapter 2: Structure of Visual Basic projects
    • Chapter 3: Visual Basic language
    • Chapter 4: Miscellaneous
  • Part II: Financial applications
    • Chapter 1: Properties of stock returns
    • Chapter 2: Diversification / efficient frontier
    • Chapter 3: Option pricing

Excel for Financial Modeling

  • Excel is useful for financial modeling as it has:
    • Statistical functions like probability distributions, regression tools, and data analysis tools
    • Mathematical functions like matrix operations and numerical optimization procedures

Excel VBA

  • From version 5.0 onwards, Excel features a user-friendly IDE and VBA as a tool
  • The IDE includes a code editor and debug functions
  • VBA, a programming language (loops, conditional statements, worksheet management tools)
  • Programming can extend Excel's capabilities by automating repetitive tasks and building additional functions

Chapter 1: Excel object model

  • VBA is an object-oriented language
  • An object is any element of the Excel application you can interact with, like:
    • workbook
    • worksheet
    • range
    • cell
    • menu

VBA Objective

  • Manipulating objects through computer code rather than manual interaction
  • Cons: Less intuitive, requires knowledge of objects and VBA language
  • Pros: Automation, increased computation capacities

Section 1 Excel objects: Properties and Methods

  • Excel object behavior is described by:
    • Properties
    • Methods

Properties

  • Properties refer to an object's characteristics
  • Cell A1 has the properties:
    • Background color
    • Height and width
    • Font
    • Style and size of characters
    • Coordinates (row and column number)

Modifying Properties

  • To write the number 150 in Cell A1:
    • Use the object Range("A1")
    • Access the Value Property to set content
    • Range("A1").Value = 150
  • In general, to modify a property: <Object>.<Property> = <value>

Retrieving Values

  • To retrieve the content of Cell A1 and store it in a variable:
    • Assign the cell's Value property to the variable
    • Content_A1 = Range("A1").Value
  • In general, <Variable> = <Object>.<Property>

Object Methods

  • Methods are actions attached to an object
  • Actions translate to method names that are verbs
  • Methods attached to Cell A1 include:
    • Select
    • Cut
    • Paste
    • Drag and drop

Modifying methods

  • To cut the content of Cell A1 and put it into cell A2 (drag and drop):
    • Cut the content using method Cut
    • Assign the destination of the Cut to Cell A2
    • Range("A1").Cut Destination:= Range("A2")
  • In general, <Object>.<Method>

Section 2 Organisation of Excel Objects

  • Rules organize the huge number of Excel objects, including:
    • Storing objects within collections
    • Hierarchical structure

Objects and Collections

  • A collection is a set of objects that shares characteristics
  • Collection names contain an 's' (plural), such as Workbooks, Worksheets, or Cells
  • When a property value changes, or a method is applied to the collection level, it applies to each object within the collection

Excel Object Hierarchy

  • The hierarchy is structured as follows:
    • Application: the Excel application
    • Application is parent of the collection Workbooks
    • Each workbook is parent of both the Worksheets and the Charts collections
    • Each worksheet is parent of:
    • The Names collection (named fields)
    • Several Range objects (ranges of cells)

Accessing Excel Objects

  • The hierarchy determines how access is achieved (top to bottom)
  • The dot symbol (.) separates objects and collections
  • To change the background color of cell B2 in worksheet Sheet3 of workbook UE111 to red, enter:
    • Workbooks("UE111.xlsx").Worksheets("Sheet3").Range("B2").Interior.Color = vbRed
  • To shorten the previous line:
    • Work with a single workbook (UE111)
      • Worksheets("Sheet3").Range("B2").Interior.Color = vbRed
    • Make the Workbook UE111 and Worksheet Sheet3 active:
      • To activate Workbooks("UE111.xlsx").Worksheets("Sheet3").Activate:
      • Range("B2").Interior.Color = vbRed
    • Use the index number of worksheets and workbooks:
      • Workbooks(1).Worksheets(3).Range...
    • In case several operations must be performed within a given workbook/worksheet:
      • With Workbooks("UE111.xlsx").Worksheets("Sheet3")
        • .Range("B2").Interior.Color = vbred
      • End With

Section 3 Range objects

  • Range objects are defined based on the worksheet collection properties
  • These objects allow defining individual cells, ranges of cells and groups of cells

Properties

  • Range: Single cell, range of cells, group of (non contiguous) cells, group of ranges, etc.
  • Cells: Cells collection of a Worksheet object (set of all cells in the worksheet) or single cell
  • Rows: Rows collection of a Worksheet object (set of all rows), single row or range of (contiguous) rows
  • Columns: Same as Rows but for columns
  • ActiveCell: Active cell in a worksheet
  • Selection: Last selected range object
  • Offset: Offset (shifted) object wrt initial range object
  • Resize: Resized range object wrt initial range object
  • EntireRow: Returns a Range object that represents the entire row (or rows) containing the initial range object
  • EntireColumn: Returns a Range object that represents the entire column (or columns) containing the initial range object
  • End(<xlDirection>) : Range obtained, starting from an initial range object, by pressing the End key followed by ↓, ↑, → or ←:
    • xlDown
    • xlUp
    • xlToRight
    • xlToLeft

Selecting ranges and cells

  • Select a range object.
  • Activate a cell
  • Select the selected cell to equal an active cell as a single cell
  • The cell in the upper left corner of the range is activated within the range of cells

Selection by direct referencing

  • Using the Range property
    • Range("A1").Select
    • Range("A1:C3").Select
    • Range("A1:C2,D4:E5,F6").Select
    • Range("A:C,E:E").Select
    • Range("1:3,5:5").Select
    • Range("A1:C3,D4,E:G,I:I,7:9,11:11").Select
  • Using the Cells property
    • Cells.Select
    • Cells(16).Select
      • 16 384 columns
      • 1 048 576 rows
    • Cells(2,3).Select
    • Range(Cells(1,1),Cells(3,3)).Select
    • Range (Cells(i,j),Cells(k,l)).Select
  • Using the Rows and Columns properties
    • Rows.Select
    • Rows(1).Select
  • Rows(i).Select
  • Rows("1:3").Select
  • Columns.Select
  • Columns("A").Select
  • Columns(1).Select
  • Columns(i).Select
  • Columns("A:C").Select

Indirect Referencing Examples

  • Range("A1").Offset(2,3).Select
  • Range("A1:C3").Offset(2,3).Select
  • Range("A1").Activate
    • ActiveCell.Offset(1,1).Select
  • Range("A1").Activate
    • ActiveCell.Offset(1,1).Activate
    • ActiveCell.Offset(1,1).Select
  • Range("D2:G7").Resize(2,3).Select
  • Range("A1:A4").EntireRow.Select
  • Range("A1").End(xlDown).Select

Invoking a range object from a range object

  • Creates a range object using one of the properties of the Worksheets collection = global Range
  • A range object can be created from a parent range object:
    • Amounts to restricting the worksheet to the parent range object
    • References are relative to the upper left corner of the first range object (change in coordinates)
  • Examples
  • Range("B2:F6").Cells.Select
  • Range("B2:F6").Range("A1").Select
  • Range("B2:F6").Rows(2).Select

Properties

  • Row: returns the row index (1 for row 1, 2 for row 2) of the cell to which Row is applied
    • Ex: Range("A1").Row
  • Column: returns the column index (1 for A, 2 for B) of the cell to which Column is applied
  • Count: returns the number of elements in a collection
    • Ex: Range("A1:C3").Cells.Count
  • Name: assigns a name to a range
    • Ex: Range("A1:C3").Name = "MyRange"
    • Range("MyRange").Select

Within ranges and cells

  • Storing values:
    • General syntax: <Range object>.Value = <Expression>
    • If the range object contains more than one cell, the expression will be stored in each of the cells
    • Ex: Range("B2:F6").Value = 2024
  • Storing formulas:
    • Formula Property
    • FormulaR1C1 property

The Formula Property

  • Exhibits the same behavior as formulas written in interactive mode
  • Relative reference to a row/column is derived by writing the row number/column letter(s) of the corresponding row/column
  • Absolute reference to a row/column is derived by inputting a $ symbol before the row number/column letter(s)

The FormulaR1C1 Property

  • R1C1 is the notation
  • R to create a reference to a Row
  • C to create a reference to a column

The formula dependent on the referencing

  • R[i] (C[i]) where i is a non-zero relative integer
  • i>0: Generates a relative reference to the row (column) located i rows below (i columns to the right) with respect to the current row (column)
  • i<0: Generates a relative reference to the row (column) located i rows above (i columns to the left) with respect to the current row (column)

Chapter 2: The structure of Visual Basic projects

  • Applications developed in VBA are structured in a project
  • A project is made of:
    • Worksheets
    • Modules
    • UserForms
  • The various elements contained in a project interact with one another to achieve the objective of the VBA programmer

Section 1: modules

  • Two types of modules:
    • Class modules
    • Code modules, those with VBA code

Two types of modules:

  • Class modules: definition of new objects (for advanced users)
  • Code modules: which contains the VBA commands

Module features:

  • Methods: Created by the user
  • Single property = Name (name of the module)
    • By default, Modulei (i = order of creation of the module)
    • The name can be modified in the Properties panel of the IDE Declarations: Top of a module (see Listbox in upper right corner)

Section 2: UserForms

  • Objects corresponding to user-defined interactive interfaces

Section 3: Visual Basic procedures

  • Procedure: A set of instructions aimed at performing a given task
  • Sub procedures
  • Function procedures or functions
  • Property procedures (within class modules -> for advanced users)

Sub procedures

  • Perform a task but returns no result
  • General Rule for Sub Procedures
    • Max of 255 characters allowed for procedure name
    • Some characters and names are forbidden (reserved names)
  • Syntax:
    • Sub <procedure name> ([arg1], [arg2],...)
    • Instructions
    • End Sub
  • Behavior:
    • A Sub executes the instructions one after another
    • The execution stops at the End Sub line
    • A main procedure can call secondary procedures
    • Calling a secondary procedure: Call [<module name>].<procedure name>([arg1], [arg2], ...)
    • Private:Private scope procedures are recognized only within the module to which they belong
    • Public (default option): Public procedures are recognized by all procedures within the project
  • Static
    • Determines whether variables remain in existence as long as Visual Basic is running
    • Static: Variables are not reset
    • The alternative defaults to resetting variables

Function procedures or functions

  • MUST return a result
  • Syntax:
    • [Private|Public] [Static] Function <name> ([arg1],[arg2],...)
    • Instructions
    • <name> = <Expression>
    • End Function It is important to note that:
      • A Sub procedure can call another Sub procedure
    • A Sub procedure can call a Function
      • A Function procedure can call another Function procedure
    • A Function procedure can call itself = Recursion
      • A Function procedure cannot call a Sub procedure

Chapter 3: Visual Basic language

  • Covers variables and control statements

Section 1: Variables in VB

  • Boolean type: boolean or logical variables
    • 2 possible values including:
      • True
      • False
    • Default value: False

Numerical variables

Byte type: integer value within the interval [0;255] • Integer type: integer value within the interval [-32,768 ; 32,767] • Long type: integer value within the interval [- 2,147,483,648; 2,147,483,647] • Single type: 45 digit precision • Double type: 324 digit precision

String type

• String of characters • Up to 2 billions of characters • Default value: "" (empty string) - & is the concatenation operator

Object variables

• Variables aimed at establishing a reference to an object • As many types as the number of object types (Range, Workbook, Worksheet) • The assignment of an object to a variable involves using the Set instruction:

  • Set <variable> = <object> • The default value of an object variable: - Nothing It is recommended to release the memory allocated to an object variable once This variable is no longer useful using Set <variable> = Nothing

Variant type

• Accepts all other types • The default type assigned to a variable if this variable is not declared • Most memory- and time-consuming variables • The use of Variant type should be restricted to variables whose type can change over time (in case of conversion) or to array variables whose cells are used to Store information of different nature (e.g. numbers and characters) • Default value: Empty

Dim Instruction / Variables Declaration

• Declaring means assigning a type to a variable

  • Syntax: Dim <variable> as <type>
    • Ex: Dim MyRange as Range

Declaration of array variables

  • An array is a structure (vector, matrix) of cells
  • Cells all contain information of the same data type
  • Each element can be accessed through coordinates

Numbering of elements in every dimension:

• 0 by default or if Option Base 0 has been put in the declaration section of the module • 1 if Option Base 1 has been put in the declaration section of the module.

Data type

  • Procedure Scope
    • The variable is accessible from only within the procedure in which it is declared Private Module Scope
    • The variable is accessible from only within the module in which it is declared • Declaration: Private <name> as <Type> in declaration section of the module

Dynamic variables

• ReDim (dim1,dim2) ' to declare dynamic variables

Notes for Dynamic Variables

• Variables whose dimension (number of elements in any of the dimensions of the variable) can change over time • Syntax: ReDim <name> (<Nb'1>, A good practice is to first declare a dynamic variable • ReDim is mostly useful to declare a variable of which the number of elements is given by the content of another variable Ex: ReDim MyArray<Variable>

Section 2: Control statements

  • Instructions to modify program behaviour
  • Allow to redirect the execution of a program towards a given group of instructions
  • Dependency on a variable's values or a condition being either True or False

Control structures

  • The general layout for the IF..THEN..ELSE structure

Operators

  • Operators consist of:
    • Arithmetic
  • Comparison
  • Logical
  • For Next Loop · For Each Next Loop

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Budget Forecasting using Excel
8 questions
Financial Modeling Overview
16 questions
Financial Modeling Introduction Chapter 1
41 questions
Use Quizgecko on...
Browser
Browser