You are on page 1of 22

Analyzing and Presenting Business Data

Objectives

In this session, you will learn to:


Use Visual Basic for Applications (VBA) in conjunction with
Excel and automate repetitive tasks using macros
Create forms using Visual Basic Editor (VBE) and customize
the Excel workbook
Create VBA procedures to perform tasks, use conditional
statements, and perform event-driven tasks

Ver. 1.0 Slide 1 of 22


Analyzing and Presenting Business Data
Recording Macros

A macro is a set of commands required to automate a task.


Whenever you need to perform the same task, you run the
recorded macro
There are three possible locations to save a macro:
This Workbook
New Workbook
Personal Macro Workbook

Ver. 1.0 Slide 2 of 22


Analyzing and Presenting Business Data
Customizing Macros

Existing macros may be modified and customized to suit


your requirements.
Microsoft Excel converts the recorded steps into VBA code.
The IDE provides a centralized location for designing the
user interface for the application, writing code, and
compiling and debugging the application.
In addition to the standard interface elements, the VBE IDE
contains other elements such as the Properties window,
Code window, and Project Explorer window.

Ver. 1.0 Slide 3 of 22


Analyzing and Presenting Business Data
Demo: Creating Macros

Scenario:
John Barrett of Books Treasure, Inc. needs to present the
sales report for the month using the standard format. He has to
work on a large number of worksheets to create the
consolidated report. Here are the specifications to format the
sales report:
The current date should always appear at the top left corner of the
worksheet.
The column headings should appear in bold and blue in color.
The name of the organization should be center aligned, red in
color, bold, and underlined. Also, the font size should be 20 points.
The data in the cells should be green in color and appear in italics.
Also, the font size should be 10 points.

Ver. 1.0 Slide 4 of 22


Analyzing and Presenting Business Data
Demo: Creating Macros (Contd.)

Solution:
To solve the preceding scenario, you need to create a macro
to automate the task of formatting the report.

Ver. 1.0 Slide 5 of 22


Analyzing and Presenting Business Data
Demo: Customizing Macros

Scenario:
Tom Wilkins of Books Treasure Inc., has suggested some
changes in the specifications to format the sales report. Here
are the new specifications:
The column headings should appear in bold and red in color.
The data in the cells should be green in color and appear in italics.
Also, the font size should be 12 points.

Ver. 1.0 Slide 6 of 22


Analyzing and Presenting Business Data
Demo: Customizing Macros (Contd.)

Solution:
To solve the preceding scenario, you need to edit the macro
and customize it to the new specifications.

Ver. 1.0 Slide 7 of 22


Analyzing and Presenting Business Data
Customizing Excel Worksheets

Excel provides controls such as buttons, lists, and text


boxes that can be added to a worksheet.
The Excel interface is customized by adding controls to:
Display or enter data
Perform an action
Make the form easier to read

Ver. 1.0 Slide 8 of 22


Analyzing and Presenting Business Data
UserForms

A UserForm is a dialog box or a window for applications


created in Excel and adds a professional look to the
applications.
Users interact with the controls on the form to obtain the
desired result.

Ver. 1.0 Slide 9 of 22


Analyzing and Presenting Business Data
Demo: Customizing Excel Worksheets

Scenario:
John Barrett wants to customize the worksheet so that he is
able to see the total sales in rupees and dollars. He also wants
to display the category-wise total sales.

Ver. 1.0 Slide 10 of 22


Analyzing and Presenting Business Data
Demo: Customizing Excel Worksheets (Contd.)

Solution:
To solve the preceding scenario, John need to perform the
following:
Add two option buttons inside a frame control for rupees and
dollars. (By default total sales should be displayed in dollars)
Add a command button and attach it to a macro that calculates the
category-wise sales.

Ver. 1.0 Slide 11 of 22


Analyzing and Presenting Business Data
Demo: Creating UserForms

Scenario:
Books Treasure, Inc. has decided to offer home loans to their
employees. All the permanent employees will be eligible for the
loan. Any employee who applies for the loan has to fill up a
Home loan form.

Ver. 1.0 Slide 12 of 22


Analyzing and Presenting Business Data
Demo: Creating UserForms (Contd.)

Solution:
To solve the preceding problem, you need to create Home loan
form. The form should be appealing, easy to use, and should
consist of the following fields:
Employee ID
Employee Name
Date of Birth - with date, month, and year fields
Basic Salary – range provided
Date of Joining - with date, month, and year fields
Marital Status - options available for selection
Preferred Installment Period – multiple options defined with
preference criteria

Ver. 1.0 Slide 13 of 22


Analyzing and Presenting Business Data
Creating VBA Procedures

A procedure is a block of code that performs a specific task


or calculates a value.
There are two types of procedures in VBA:
Sub Procedures
Function Procedures

Ver. 1.0 Slide 14 of 22


Analyzing and Presenting Business Data
Using Variables and Constants

Variables can be defined as a location in the memory of a


computer used for storing information.
The data type of a variable determines the type of data that
a variable can store.
You can declare variables using the Dim (stands for
dimension) statement.
A constant is a variable with a static value.
You can declare a constant when the value of the variable is
not expected to change throughout the life of the program.
Scope of a variable is used to define the accessibility of that
variable.

Ver. 1.0 Slide 15 of 22


Analyzing and Presenting Business Data
Control Statements

If...Then...Else Statements:
It helps in the execution of the code based on a condition. In
case of multiple conditions, there must be a matching End If for
each If.
Select Case Statements:
It is used when a variable assumes multiple values and you
have to respond differently depending on the values stored.
Do…Loop Statement:
It causes a part of the program to be repeated for a specified
number of times, while the condition specified remains true.
For…Next Statement:
It allows you to loop through a set of statements for a definite
number of times.

Ver. 1.0 Slide 16 of 22


Analyzing and Presenting Business Data
Event-Driven Procedures

Applications react to events triggered by a user, another


program, or the computer.
Events are a series of messages sent by VBA to your
program. Events can also be called the trigger of the Excel
VBA procedure.

Ver. 1.0 Slide 17 of 22


Analyzing and Presenting Business Data
Demo: Performing Tasks

Scenario:
Books Treasure, Inc. is offering home loans to their
employees. All the permanent employees are eligible for the
loan. Any employee who applies for the loan should fill up a
form, which would calculate the amount of loan for which the
employee is eligible.

Ver. 1.0 Slide 18 of 22


Analyzing and Presenting Business Data
Demo: Performing Tasks(Contd.)

Solution:
Create an event for the “Calculate Loan” command button in
the home loan form.
At the click of this button, the amount of loan for which the
employee is eligible should be displayed.
Add a close button to the form to close the application.

Ver. 1.0 Slide 19 of 22


Analyzing and Presenting Business Data
Demo: Performing Tasks(Contd.)

Books Treasure, Inc. offers loan to its employees on the basis


of the Basic salary. The details of salary package and loan are
given in the following table:

Salary Package Loan


$3,000 Up to ten months salary

$4,000 Up to ten months salary

$5,000 Up to fifteen months salary


$6,000 Up to fifteen months salary

$8,000 Up to fifteen months salary

Ver. 1.0 Slide 20 of 22


Analyzing and Presenting Business Data
Summary

In this session, you learned that:


A macro is a program that contains the statements required to
complete a repetitive task.
There are three possible locations to save a macro:
This Workbook
New Workbook
Personal Macro Workbook
Excel converts the steps carried out to save to a macro into the
VBA code. An existing macro can be edited in the VBE.
UserForm is a dialog box or a window for applications created
in Excel.
A procedure is a single unit of code that performs a specific
task or calculates a value.

Ver. 1.0 Slide 21 of 22


Analyzing and Presenting Business Data
Summary (Contd.)

There are two types of procedures in VBA:


Sub Procedure
Function Procedure
Variables can be defined as a location in the memory of a
computer used for storing information.
A constant is a variable with a fixed value, which does not get
changed during the course of the program execution.
VBA provides various control statements to control the flow of
the program.
Events are a series of messages sent by VBA to your program.
Procedures written in the open event of the Workbook run
automatically when a workbook is opened.

Ver. 1.0 Slide 22 of 22

You might also like