Analyzing and Presenting Business Data
Objectives
In this session, you will learn to:
Identify the most commonly used objects in VBA and Use VBA
objects properties and methods in code
Create user-defined functions
Ver. 1.0 Slide 1 of 20
Analyzing and Presenting Business Data
Working with Excel VBA Objects
Objects in Excel are related to each other in a hierarchical
manner.
There are five main objects in Excel that are used most
often in VBA codes.
Application: represents the Excel application
Workbook: represents the Excel file
Worksheet: represents the Excel worksheet
Range: represents one or more cells on a worksheet
Chart: represents the charts you create to graphically
represent the data
The object browser has two columns displaying the classes
and their members.
Ver. 1.0 Slide 2 of 20
Analyzing and Presenting Business Data
Working with Excel VBA Objects (Contd.)
An object comprises of properties and methods related to
the entity it represents.
These properties and methods are called members of the
object.
The properties are data members that store data for the
object
The methods are defined tasks or activities that the object
performs. The methods are also known as procedures or
functions.
Ver. 1.0 Slide 3 of 20
Analyzing and Presenting Business Data
Working with Excel VBA Objects (Contd.)
The Objects can be referred in a VBA code by:
Setting the properties of an object.
Getting the properties of an object.
Executing the methods of an object.
To refer objects in a code you need to use the object
hierarchy.
An object variable of type object is a generic variable that
can be used to point to any object.
A group of similar objects is called a collection.
The Count property of a collection allows you to count the
number of objects present in the collection.
The Add property allows you to add a new object to the
collection.
Ver. 1.0 Slide 4 of 20
Analyzing and Presenting Business Data
Using the Application Object
The Application object controls the application-wide
properties.
Some of the commonly used properties of Application
Object are:
ActiveWorkbook
ActiveSheet
ActiveCell
ThisWorkbook
Some of the commonly used methods of Application Object
are:
Goto
InputBox
Ver. 1.0 Slide 5 of 20
Analyzing and Presenting Business Data
Using the Workbook Object
The Workbook object represents an Excel application file. It
allows you to refer to workbooks in VBA code.
Some of the commonly used properties of workbook object
are:
Saved
Path
Some of the commonly used methods of workbook object
are:
Activate
Add
Close
Save
Ver. 1.0 Slide 6 of 20
Analyzing and Presenting Business Data
Using the Worksheet Object
The Worksheet object represents a sheet of a workbook.
Some of the commonly used properties of worksheet object
are:
Name
Visible
Some of the commonly used methods of worksheet object
are:
Calculate
Copy
Move
Delete
Ver. 1.0 Slide 7 of 20
Analyzing and Presenting Business Data
Using the Range Object
The Range object represents a single cell, a group of
selected cells, a row, a column or a 3D range.
Some of the commonly used properties of range object are:
Address
Count
Formula
Offset
Value
Some of the commonly used methods of range object are:
Activate
Select
Clear
Ver. 1.0 Slide 8 of 20
Analyzing and Presenting Business Data
Demo: Working with Excel VBA Objects
Demonstrate the usage of the range objects for calculating
totals of values.
Ver. 1.0 Slide 9 of 20
Analyzing and Presenting Business Data
Building Connectivity between Excel Worksheets and UserForm
You can build powerful applications by transferring the data
entered into UserForms to Excel worksheet.
To write an efficient code, you can refer multiple properties
and methods using the With statement.
Ver. 1.0 Slide 10 of 20
Analyzing and Presenting Business Data
Demo: Working with Excel VBA Objects
Scenario:
Ed Young needs to create a sales report. The specifications of
the template for the sales report are:
The name of the worksheet should be BooksSalesReport.
Cell A1 should display “Books Sales Report”.
The font of the title should be Arial of size 14. The title should
appear in bold and italics.
Ver. 1.0 Slide 11 of 20
Analyzing and Presenting Business Data
Demo: Working with Excel VBA Objects (Contd.)
The following should be entered in the specified cells as the column
headings for the report:
Column Heading Cell
Serial No. A4
Category B4
Number of Books Sold C4
Cost Price Per Book D4
Selling Price Per Book E4
Profit/Loss F4
Additional Specifications
The font of the columns should be Arial of size 11.
The column headings should be in bold and italics.
Ver. 1.0 Slide 12 of 20
Analyzing and Presenting Business Data
Demo: Working with Excel VBA Objects (Contd.)
Solution:
To solve the preceding scenario, you need to perform the
following task:
Write a program in VBA to create a template for Books Sales
Report.
Ver. 1.0 Slide 13 of 20
Analyzing and Presenting Business Data
User-Defined Functions
User defined functions are created to perform specific tasks
as per the requirement of the user.
You can also pass objects to a function and make functions
return objects.
Ver. 1.0 Slide 14 of 20
Analyzing and Presenting Business Data
Demo: Creating User-Defined Functions
Scenario:
Books Treasure, Inc. has to prepare the monthly payroll for its
employees. The HR manager Pat Greene wants the payroll
information to be entered using a form to reduce the errors that
might creep in when data is entered directly into the cells.
Create a form that will help input the following values:
Employee ID
Employee Name
Basic Salary – range provided
Ver. 1.0 Slide 15 of 20
Analyzing and Presenting Business Data
Demo: Creating User-Defined Functions (Contd.)
When the user clicks the OK button on the form after inputting
the values for the first user, the other details should be
calculated and input into the Excel sheet.
The input values should be based on the information given
below and the text boxes in the form should be blank for the
next data entry.
The user should be allowed to enter values till the Exit button is
selected.
Ver. 1.0 Slide 16 of 20
Analyzing and Presenting Business Data
Demo: Creating User-Defined Functions (Contd.)
Solution:
To solve the preceding scenario, John need to perform the
following:
Calculate the Taxable Income as per the following details:
DA: 50% of basic salary
HRA: 35% of basic salary
Conveyance: 10% of basic salary
(Hint: Taxable Income = Basic Salary + DA + HRA + Conveyance)
Ver. 1.0 Slide 17 of 20
Analyzing and Presenting Business Data
Demo: Creating User-Defined Functions (Contd.)
Solution:
Calculate the Net Income as per the following details:
Provident Fund: 12 % of basic salary.
Income tax is deducted as per the following rules:
No tax is deductible if taxable income is less than $3000 per
month.
10% tax if taxable income is between $3000 and $6000.
20%% tax if taxable income is between $6000 and $9000.
30% tax if taxable income is above $9000.
(Hint: Net Income = Taxable Income – Income tax – Provident
fund)
Ver. 1.0 Slide 18 of 20
Analyzing and Presenting Business Data
Summary
In this session, you learned that:
VBA code can be written or modified using the VBA objects to
add functionality to an existing macro or create custom
applications.
In VBA, you can:
Set the properties of an object
Get the properties of an object
Execute the methods of an object
The VBA objects follow a hierarchy, which is represented by an
object model.
The objects that are present higher in the hierarchy contain the
objects that are present lower in the hierarchy.
Ver. 1.0 Slide 19 of 20
Analyzing and Presenting Business Data
Summary (Contd.)
Every object has properties and methods that specify the
characteristics and behavior of the object.
The Application object is the highest-level object that controls
the application-wide properties.
Ver. 1.0 Slide 20 of 20