Podcast
Questions and Answers
Which of the following applications is Excel particularly well-suited for?
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?
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?
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?
Using VBA, how does one modify a property of an Excel object?
What do ‘methods’ represent in the context of Excel VBA objects?
What do ‘methods’ represent in the context of Excel VBA objects?
Which statement accurately describes how Excel objects are organized?
Which statement accurately describes how Excel objects are organized?
What is the primary implication of applying a method or changing a property value at the 'collection' level in VBA?
What is the primary implication of applying a method or changing a property value at the 'collection' level in VBA?
Which Excel object is the parent of both the 'Worksheets' collection and the 'Charts' collection?
Which Excel object is the parent of both the 'Worksheets' collection and the 'Charts' collection?
What character is used to separate various objects and collections when accessing Excel objects in VBA?
What character is used to separate various objects and collections when accessing Excel objects in VBA?
Given the code Workbooks("UE111.xlsx").Worksheets("Sheet3").Activate
, what is the purpose of the Activate
method?
Given the code Workbooks("UE111.xlsx").Worksheets("Sheet3").Activate
, what is the purpose of the Activate
method?
Which of the following is NOT a valid way to specify a range of cells in VBA?
Which of the following is NOT a valid way to specify a range of cells in VBA?
In VBA, what does the Offset
property of a Range object allow you to do?
In VBA, what does the Offset
property of a Range object allow you to do?
In VBA, which property is used to assign a name to a range of cells?
In VBA, which property is used to assign a name to a range of cells?
Which property is used to insert a formula into a cell using VBA?
Which property is used to insert a formula into a cell using VBA?
What is the key difference between using Formula
and FormulaR1C1
properties in VBA?
What is the key difference between using Formula
and FormulaR1C1
properties in VBA?
What is the purpose of creating 'code modules' in VBA?
What is the purpose of creating 'code modules' in VBA?
In VBA, what are 'UserForms' primarily used for?
In VBA, what are 'UserForms' primarily used for?
What is the main distinction between a 'Sub' procedure and a 'Function' procedure in VBA?
What is the main distinction between a 'Sub' procedure and a 'Function' procedure in VBA?
In VBA, which declaration keyword is used to specify that a variable's value should be preserved between calls to the procedure?
In VBA, which declaration keyword is used to specify that a variable's value should be preserved between calls to the procedure?
What does defining a variable as Public
within a module achieve?
What does defining a variable as Public
within a module achieve?
Which of the following VBA data types consumes the least memory?
Which of the following VBA data types consumes the least memory?
In VBA, what happens if you do not explicitly declare a variable's data type?
In VBA, what happens if you do not explicitly declare a variable's data type?
In VBA, what is the purpose of the Set
keyword when working with object variables?
In VBA, what is the purpose of the Set
keyword when working with object variables?
In VBA, what is a Dynamic Array?
In VBA, what is a Dynamic Array?
What is the purpose of 'Option Base' in VBA?
What is the purpose of 'Option Base' in VBA?
Which code block represents the correct syntax for an If...Then...Else
statement in VBA?
Which code block represents the correct syntax for an If...Then...Else
statement in VBA?
Which operator is used for integer division in VBA?
Which operator is used for integer division in VBA?
What is the primary purpose of 'comparison' operators in VBA?
What is the primary purpose of 'comparison' operators in VBA?
Which logical operator returns True
only if both conditions being evaluated are True
?
Which logical operator returns True
only if both conditions being evaluated are True
?
When should the Select Case
statement be preferred over a series of If...Then...ElseIf
statements?
When should the Select Case
statement be preferred over a series of If...Then...ElseIf
statements?
Which type of loop is most suitable when you need to iterate through all elements of a collection?
Which type of loop is most suitable when you need to iterate through all elements of a collection?
What is the key difference between a Do While
loop and a Do Until
loop?
What is the key difference between a Do While
loop and a Do Until
loop?
What is the purpose of the Excel Macro Recorder?
What is the purpose of the Excel Macro Recorder?
What is a 'breakpoint' in the context of VBA debugging?
What is a 'breakpoint' in the context of VBA debugging?
What functionality does defining a 'watch' provide during VBA debugging?
What functionality does defining a 'watch' provide during VBA debugging?
A workbook object wb
has a worksheet named “Summary”. Which of the following will correctly refer to cell A1 on that worksheet?
A workbook object wb
has a worksheet named “Summary”. Which of the following will correctly refer to cell A1 on that worksheet?
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
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
What happens to the value of a Static
variable upon exiting the procedure in which it is declared?
What happens to the value of a Static
variable upon exiting the procedure in which it is declared?
In VBA, what is an object?
In VBA, what is an object?
Which of the following is an example of an Excel object?
Which of the following is an example of an Excel object?
What are the two key features that describe the behavior of an Excel object?
What are the two key features that describe the behavior of an Excel object?
In VBA, what does a 'property' of an object represent?
In VBA, what does a 'property' of an object represent?
What is meant by a 'method' in the context of Excel VBA objects?
What is meant by a 'method' in the context of Excel VBA objects?
To change the background color of cell 'A1' to blue using VBA, which property of the Range object would you modify?
To change the background color of cell 'A1' to blue using VBA, which property of the Range object would you modify?
To cut the contents of cell A1 and paste them into cell A2, which method would you use?
To cut the contents of cell A1 and paste them into cell A2, which method would you use?
What is a 'collection' in the context of the Excel object model?
What is a 'collection' in the context of the Excel object model?
Which of the following is an example of an Excel 'collection'?
Which of the following is an example of an Excel 'collection'?
What is typically the naming convention to identify Excel objects?
What is typically the naming convention to identify Excel objects?
If you apply a property value change or a method at the 'collection' level (e.g., to all worksheets), what is the implication?
If you apply a property value change or a method at the 'collection' level (e.g., to all worksheets), what is the implication?
In the Excel object hierarchy, which object is the 'parent' of the 'Worksheets' collection?
In the Excel object hierarchy, which object is the 'parent' of the 'Worksheets' collection?
Which symbol is used to separate objects and collections when accessing Excel objects in VBA?
Which symbol is used to separate objects and collections when accessing Excel objects in VBA?
In VBA, what does the term 'Range' refer to?
In VBA, what does the term 'Range' refer to?
Which of the following is NOT a valid way to refer to a range of cells?
Which of the following is NOT a valid way to refer to a range of cells?
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?
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?
To insert a formula into cell C5 using VBA, which property of the Range object would you typically use?
To insert a formula into cell C5 using VBA, which property of the Range object would you typically use?
What referencing style does the FormulaR1C1
property use in VBA?
What referencing style does the FormulaR1C1
property use in VBA?
What syntax creates an absolute reference to row (column) i, where i is an integer different from 0, using the FormulaR1C1
property?
What syntax creates an absolute reference to row (column) i, where i is an integer different from 0, using the FormulaR1C1
property?
What are the main components in Excel VBA projects?
What are the main components in Excel VBA projects?
Which type of module would be used to store standard VBA code?
Which type of module would be used to store standard VBA code?
What is the primary use of UserForms in VBA?
What is the primary use of UserForms in VBA?
What are the two main procedure types in VBA?
What are the two main procedure types in VBA?
What are Sub procedures designed to do?
What are Sub procedures designed to do?
If you want a procedure to calculate and return a value, which type of procedure should you use?
If you want a procedure to calculate and return a value, which type of procedure should you use?
When defining a Sub procedure, which of the following statements is true?
When defining a Sub procedure, which of the following statements is true?
What is the purpose of the Call
keyword in VBA?
What is the purpose of the Call
keyword in VBA?
Which declaration statement will make a procedure that is only able to be recognized within the module it belongs to?
Which declaration statement will make a procedure that is only able to be recognized within the module it belongs to?
If a variable is declared as Static
within a procedure, what happens to its value each time the procedure is run?
If a variable is declared as Static
within a procedure, what happens to its value each time the procedure is run?
Which of the following VBA types consumes the most memory?
Which of the following VBA types consumes the most memory?
What is the default data type assigned to a variable if it is not explicitly declared in VBA?
What is the default data type assigned to a variable if it is not explicitly declared in VBA?
What is the main purpose of the Set
keyword in VBA?
What is the main purpose of the Set
keyword in VBA?
What is a 'dynamic array' in VBA?
What is a 'dynamic array' in VBA?
What happens if Option Base 1
is declared?
What happens if Option Base 1
is declared?
Which arithmetic operator isn't like the others?
Which arithmetic operator isn't like the others?
In VBA, what is the purpose of comparison operators such as >
, <
, and =
?
In VBA, what is the purpose of comparison operators such as >
, <
, and =
?
When using the And
logical operator, what condition must be met for the entire expression to be True?
When using the And
logical operator, what condition must be met for the entire expression to be True?
When is the Select Case
statement preferable over a series of If...Then...ElseIf
statements?
When is the Select Case
statement preferable over a series of If...Then...ElseIf
statements?
In VBA, what is a For Each...Next
loop primarily used for?
In VBA, what is a For Each...Next
loop primarily used for?
What key aspect differentiates Do While
and Do Until
loops?
What key aspect differentiates Do While
and Do Until
loops?
What is the primary advantage of using VBA to manipulate Excel objects, compared to directly interacting with them?
What is the primary advantage of using VBA to manipulate Excel objects, compared to directly interacting with them?
In the Excel object model, what is the significance of 'collections' related to Excel objects?
In the Excel object model, what is the significance of 'collections' related to Excel objects?
When using the Formula
property in VBA, what distinguishes a relative reference from an absolute reference in a formula assigned to a cell?
When using the Formula
property in VBA, what distinguishes a relative reference from an absolute reference in a formula assigned to a cell?
What is the key distinction between 'Code modules' and 'Class modules' within a VBA project?
What is the key distinction between 'Code modules' and 'Class modules' within a VBA project?
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?
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?
Flashcards
What is an Excel object?
What is an Excel object?
Any element of the Excel application you can interact with.
What are object properties?
What are object properties?
Characteristics such as color, height, width, and font.
What are object methods?
What are object methods?
Actions an object can perform, such as Select, Cut, and Paste.
What is a Collection?
What is a Collection?
Signup and view all the flashcards
What is the Excel objects hierarchy?
What is the Excel objects hierarchy?
Signup and view all the flashcards
What are range objects?
What are range objects?
Signup and view all the flashcards
What does 'Range' refer to?
What does 'Range' refer to?
Signup and view all the flashcards
What is EntireRow?
What is EntireRow?
Signup and view all the flashcards
What is EntireColumn?
What is EntireColumn?
Signup and view all the flashcards
What does End(<xlDirection>) do?
What does End(<xlDirection>) do?
Signup and view all the flashcards
What is the 'Select' method?
What is the 'Select' method?
Signup and view all the flashcards
What is the 'Activate' method?
What is the 'Activate' method?
Signup and view all the flashcards
What is a relative reference?
What is a relative reference?
Signup and view all the flashcards
What is an Absolute reference?
What is an Absolute reference?
Signup and view all the flashcards
What is a project?
What is a project?
Signup and view all the flashcards
What is a Class module?
What is a Class module?
Signup and view all the flashcards
What is a Code module?
What is a Code module?
Signup and view all the flashcards
What are UserForms used for?
What are UserForms used for?
Signup and view all the flashcards
What is a procedure?
What is a procedure?
Signup and view all the flashcards
What are Sub procedures?
What are Sub procedures?
Signup and view all the flashcards
What is the syntax for a sub-procedure?
What is the syntax for a sub-procedure?
Signup and view all the flashcards
What are Function procedures?
What are Function procedures?
Signup and view all the flashcards
What are common variable types?
What are common variable types?
Signup and view all the flashcards
What are Object variables?
What are Object variables?
Signup and view all the flashcards
What does the variable type 'Variant' do?
What does the variable type 'Variant' do?
Signup and view all the flashcards
What does 'Declaring variables' mean?
What does 'Declaring variables' mean?
Signup and view all the flashcards
What is an array?
What is an array?
Signup and view all the flashcards
What is procedure scope?
What is procedure scope?
Signup and view all the flashcards
What is private module scope?
What is private module scope?
Signup and view all the flashcards
What is public module scope?
What is public module scope?
Signup and view all the flashcards
What are dynamic variables?
What are dynamic variables?
Signup and view all the flashcards
What are Control statements?
What are Control statements?
Signup and view all the flashcards
What is a Conditional Statment?
What is a Conditional Statment?
Signup and view all the flashcards
What are common operator types?
What are common operator types?
Signup and view all the flashcards
What is the use of an operator with '='?
What is the use of an operator with '='?
Signup and view all the flashcards
What are Loops used for?
What are Loops used for?
Signup and view all the flashcards
What is the 'For Each Next' loop?
What is the 'For Each Next' loop?
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
- Assign the cell's
- 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
- To activate
- 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
- Work with a single workbook (UE111)
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
- Ex:
- 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
- Ex:
- Name: assigns a name to a range
- Ex:
Range("A1:C3").Name = "MyRange"
Range("MyRange").Select
- Ex:
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
- General syntax:
- 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 callsecondary
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 belongPublic
(default option):Public
procedures are recognized by all procedures within the project
- A
- 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
- 2 possible values including:
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 usingSet <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
- Ex:
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
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.