Excel Macros
Excel Macros
Excel Macros
LEVEL 1
Sreejith R
Module IV
The Fundamentals of Excel VBA, the Visual Basic Editor, Objects and Properties.
Excel Objects - Workbook Included, Object Hierarchy, Application, Workbook, Worksheet, Range,
Command Bars. Variables and Constants, Data types, Variable Levels, Declaring Variables, Constants.
Loops, Types of Loops, Loop Pitfalls.
Effective Decision Making, If/And/Or, Select Case, Dates and Times. Workbook and Worksheet
Events.
Debugging and Error Handling, Prevention, Good Habits, Runtime Errors.
Excel Worksheet Functions in VBA, Specific Examples, Excels Built-In Features.
User Defined Functions
An Introduction to VBA
- What is VBA
An abbreviation for Visual Basic for Applications
Official name is "Visual Basic, Applications Edition."
VBA is Microsoft's common application programming (macro) language for
Word, Excel, Access, etc.
Also being implemented in other Microsoft applications such as Visio and is
at least partially implemented in some other applications such as AutoCAD...
VBA and VB have a lot in common, but they are different. VB is a
programming language that lets you create standalone executable programs.
An Introduction to VBA
- What Can You Do With VBA
Database Operation ….
An Introduction to VBA
- VBA- Object Based Programming Language
What is Object?
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Containers or Collections
A Group of Similar Objects Share Common Properties, Methods and Events
Such as Workbooks, Worksheets, etc.
Worksheets is a collection of all the Worksheet objects in the specified or
active workbook.
Worksheets(1) refers to the 1st worksheet of current active workbook.
Worksheets (“mooring”) refers to the worksheet named “mooring”.
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Objects
The term Excel Objects (collectively referred to as the Excel
Object Model) refers to the entities that make up an Excel
workbook, such as Worksheets, Rows, Columns, Cell
Ranges, and the Excel Workbook itself.
Such as Workbook, Worksheet, Range, Cell, Chart, Name,
etc.
Worksheets(1) is an Object Referring to the First Sheet
Range("A1:B15") is an Object Referring to a Range
Cells(1,1) or Range(“A1”) is an Object Referring to Range
“A1”
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Properties
Properties are the Physical Characteristics of Objects
and Can be Measured or Quantified.
Properties for Collections
- Worksheets.Count (Read Only)
- Worksheets.Visible = True (Read and Write)
Properties for Object
- Range("A1:B15").Rows.Count (Read Only)
- Range("A1:B15").Font.Bold = True (Read and Write)
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Methods
Methods are the Actions that Can be Performed by
Objects or on Objects
Methods for Collections
- Worksheets.Add
- Worksheets.Delete
Methods for Objects
- Range("A1:B15").ClearContents
- ActiveCell.Copy
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Events
Objects Can Respond to Events, Such as Mouse Click,
Double Click on a Cell, Active a Worksheet,
Open/Close/Save a Workbook, etc.
Worksheet Events –
Such as Activate, Deactivate, Change, Selection
Change, Before Double Click, etc.
Workbook Events-
Such as Activate, Deactivate, Open, Before Close,
Before Saving, Before Print, New Sheet.
An Introduction to VBA
- VBA Object Based Programming Language
Concepts – Referring To
Use brackets () to refer to member object
Worksheets(“mooring”)
Use dot . to refer to child object or object’s
properties and methods
Worksheets(“mooring”).Range(“A1:B3”).Font.Bold
An Introduction to VBA
- VBA Object Based Programming Language
Concept Collections
(worksheets)
Summary
Objects
(worksheet)
Properties Method
name Add
Event
Activate
Variables
Variables are programmer-defined names.
Variables can store values that can change while the application is
running.
Variables store values in computer memory.
A program code statement can change the value at any time.
A value for a variable exists only while a program is running.
Constants
x = 234
y = 234
If x > y Then
MsgBox ("X is Greater than Y“)
ElseIf y > x Then
Msgbox ("Y is Greater than X“)
Else
Msgbox ("X and Y are EQUAL“)
End If
End Sub
Decision Making Switch Case
Private Sub switch_demo_Click()
Dim MyVar As Integer
MyVar = 1
For i = 0 To a Step 2
MsgBox ("The value is i is : " & i)
Next
End Sub
Exam Perspective : Explain Loops in VBA
Looping is repeating the execution of certain statements more than one time.
you can distinguish between 2 loop categories
Category #1: Fixed-iteration loops, which repeat the relevant statements a set number of times.
Category #2: Indefinite loops, in which the number of iterations is more flexible.
In either case, the factor that determines the number of iterations is sometimes referred to as the loop invariant or
determinant.
[statements]
[Exit For]
[statements]
Next [counter]
Example: