0% found this document useful (0 votes)
43 views

Learning Unit 7

The document introduces Visual Basic for Applications (VBA) as a programming language built into Microsoft Excel that allows users to automate tasks and develop Excel-based applications. It discusses enabling the Developer tab in Excel, which provides tools for advanced functions like coding, macros, and importing/exporting files. The Visual Basic Editor (VBE) is introduced as the interface for writing VBA code, with an overview of the different windows it contains like the code window and properties window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Learning Unit 7

The document introduces Visual Basic for Applications (VBA) as a programming language built into Microsoft Excel that allows users to automate tasks and develop Excel-based applications. It discusses enabling the Developer tab in Excel, which provides tools for advanced functions like coding, macros, and importing/exporting files. The Visual Basic Editor (VBE) is introduced as the interface for writing VBA code, with an overview of the different windows it contains like the code window and properties window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

S T U DY U N I T 7

An Introduction to Aspects
25

of Coding in Macros and


Visual Basic for
Applications (VBA)

7.1 Introduction

In study units 4 to 6, you learnt how to work with formulas, functions, data presentation,
and spreadsheet security. The foundations learnt in AIN1501 as well as study units 4
to 6 has provided you with a solid foundation for this study unit and the study units to
follow within this topic.

The purpose of this study unit is to build on your Excel knowledge by introducing you
to an additional tab found within the Microsoft Excel ribbon, known as the “Developer”
tab. Gaining an understanding of the tools available in this tab is necessary in
preparing to learn a new skill, in the format of coding. The study unit will also introduce
you to the basic principles of VBA (Visual Basic for Applications), where you will be
exposed to some aspects that you will need for macros and the actual coding process.
Adding an additional and relevant skill to your arsenal is always beneficial in the long
run, and so our journey begins in this environment of basic programming.

The learning outcomes of this study unit are as follows:

• Navigate the developer ribbon.


• Reflect on Macro security and the importance thereof.
• Work through introductory principles of VBA in Microsoft Excel.

The following icons are included in this study unit:


ICON DESCRIPTION

This icon indicates reading material you must study.

This icon indicates the documents used in the process.

This icon indicates that you need to self-reflect.

This icon indicates that you need to do some critical thinking.

This icon illustrates an activity you must complete.

This icon indicates that you need to click on the link to view a brief
video file.

7.2 What is Visual Basic for Applications (VBA)?

Let’s start with a simple example. I give you the following instruction:

Fac mihi aliquid capulus, quaeso.

What do you do? Do you understand what I am saying?

I think most of you do not know what I am instructing you to do here 😊😊.

Now let’s run this through a translator:


“Fac mihi aliquid capulus, quaeso” is Latin and means “Make me some coffee, please”.

Now you understand the instruction given and will perform the task by making me
some coffee.

But what did you need to perform this task? You had the instruction in Latin, a Latin
translator and the instruction given in a language you could understand, which was
English. Our communication with computers is the same.

Computers communicate in binary code (a language consisting out of 0’s and 1’s).
Let’s illustrate this: a computer wants to communicate to you and displays the
following: 01001000 01100101 01101100 01101100 01101111 00100001. According
to Zych (2015) the computer is telling you “Hello”.

If you want to learn more on binary code, you can read the article by
Zych (2015) by clicking on the link below.

Note: This article is not part of the syllabus and therefore not
examinable.

Learn How To Write Your Name In Binary Code (sciencefriday.com)

Just like in the case with the Latin phrase, we need interpreters to interpret instructions
and communications between us and computers. Programming languages act as
these interpreters. So, if I tell a programming language to give a computer an
instruction to copy cells in a sheet of a spreadsheet and to copy it to another workbook,
the computer will comply. Fortunately, I do not have to communicate this directly to
the computer using 0s and 1s. However, you get different interpreters, which in this
case is your programming languages.

The problem is that each programming language has its own rules (or syntax). For us
to communicate efficiently with a computer we need to know the programming
language in such a way that we can communicate with it efficiently.

There are different programming languages on the market. Popular languages are
Java, Python, C,C++ and VBA. As you were introduced to a Microsoft-based solution
called Microsoft Excel (Excel) in AIN1501 and AIN2601, we will now teach you to
create a solution using VBA (Visual Basic for Applications).
7.2.1 Making use of VBA

VBA is a powerful programming language built into Excel, which can be used to
automate routine and repetitive tasks, create custom worksheet formulas, and develop
Excel-based applications for other users (Alexander & Kusleika, 2022:867). The focus
of this module will be to use VBA to automate routine and repetitive tasks. Creating
custom worksheet formulas and form-based solutions is outside the scope of this
module.

VBA is an event-driven programming language that allows you to automate different


tasks in Excel by creating macros and other functions. You can use it to create and
maintain complex trading, pricing, and risk-management models, forecast sales and
earnings, and to generate financial ratios. With VBA, you can create various portfolio-
management and investment scenarios.

Note that VBA is not available on the web version of Microsoft Excel. You will need to
use the desktop version of Microsoft Excel 365 to work with VBA.

Computer activity 7.1

Let’s first enable the develop ribbon on Excel.

• Open Excel.
• Right-click on the ribbon and select "Customize the Ribbon". You will see the
following:
Figure 7.1: Customise the ribbon options

• Check "Developer" as shown in Figure 7.1 and click on "OK".


• "Developer" should appear on your menu bar now. See Figure 7.2 below.

Figure 7.2: Microsoft Excel menu bar

• Click on "Developer". You will see the following:

Figure 7.3: Developer ribbon


You have covered the ribbon in AIN1501; however, the “Developer” tab is what is
required for VBA.

7.3 The Microsoft Excel “Developer” tab

If we’re going to be using the “Developer” tab, then we need to know what it is
actually used for as well as be able to navigate our way around the “Developer” tab.

Excel is among the most valuable tools used by finance professionals. It allows them
to conduct a financial analysis of companies based on different parameters or build
intricate financial models to help investors improve their investment portfolios and
guide their decisions regarding financial markets.

Imagine you are a manager of a financial modelling department, and tomorrow is the
earnings release date for multiple companies. Would you be able to cope with all the
work on Microsoft Excel? Absolutely not! Working 10-12 hours continuously under
intense pressure is no joke. That's why it is important to use Microsoft Excel
efficiently and maximise its effectiveness using the various simple but powerful tools
that it provides.

The Microsoft Excel “Developer” tab helps in creating reports, analysing large
volumes of data, generating reports, etc. The spreadsheet application makes it easy
for financial analysts, investment bankers and portfolio managers to make decisions
with ease.

When the “Developer” tab is activated in Microsoft Excel, they carry out more
advanced functions such as designing forms, writing code, creating macros, as well
as importing and exporting XML file formats. It automates your daily repetitive work.

The relevant tools within the “Developer” tab will be elaborated on at each
appropriate section to which the tools apply. The tools that will be discussed are as
follows:

• Visual Basic Editor (VBE)


• Macros and Recording Macros
• Using Relative References
• Macro Security
• Add-ins
• Interactive Controls

If you click on “Visual Basic”, you will go to the Visual Basic Interactive Development
Environment. We refer to this environment in short as the Visual Basic Editor (VBE).

7.3.1 The Visual Basic Editor (VBE)

The Visual Basic Editor (VBE) is the first icon in the “Developer” tab, which is one of
the core icons that will be used, when you begin to code. Clicking on the Visual
Basics from the Developer tab will open a window that will allow you to write your
own code to automate a task in Excel. But first, you need to add a module to write
the code. A VBA module acts as a vessel for your code, keeping it all together. The
creation of modules will be discussed in the next study unit (study unit 8).

Let’s explore the VBE by clicking on “Visual Basic”, as illustrated below:

Figure 7.4: Click on Visual basic.

The VBE will appear as illustrated in Figure 7.5.


Figure 7.5: The Visual Basic Editor (VBE)

The VBE is like a workbench when doing woodwork. All the tools are here to write your
code (sometimes also refer to as macros). The VBE will become your new best friend
when automating solutions in Microsoft Excel. If your screen does not look exactly like
Figure 7.5, don’t stress. We will make sure you are able to access the most important
areas in your VBE throughout the rest of this study unit.

Let’s first make sure, out options are the same:

Computer activity 7.2

• Click on “Tools” and select “Options”. You will see the following:
Figure 7.6: VBE Options

• Make sure that all the boxes are ticked like displayed in Figure 7.6. Press
“OK”.

• Now click on “View”, “Toolbars” and make sure that Edit and Standard are
selected. If not selected, select them.

7.3.1.1 The VBE applied

Project
window

Code window

Properties
window

Immediate window

Figure 7.7: VBE windows


When looking at Figure 7.7, you get a complete view of the VBE. I am first going to
discuss the different windows. If some of the windows are not present on your VBE,
then click on “View” and select the window not present. It will then appear on your
VBE.

Project window

The project window displays a tree diagram that shows every workbook
currently open in Excel (Alexander & Walkenbach, 2019:33). Note that your
modules are also displayed. Modules are units where VBA code are stored.

Properties window

This represents the properties of the selected object selected. In Figure 7.7, it
is the properties of Module1. You will see a (Name) property, where we can
change the name of Module1.

Code window

The Code window is where you add your VBA code (Alexander & Walkenbach,
2019:33). The code window makes use of colours to assist you in writing your
code. If text is blue, it is normally reserved words used in VBA. Green text
indicates comments, while red text indicates that an error was made in the
code.

Immediate window

The immediate window returns the results of statements you have typed in the
immediate window (Etheridge, 2007:26). It is very useful for testing VBA
statements, especially when you want to test code or solve an error in your
code. We will discuss the Immediate window later in the study unit.

7.3.2 Macros and Record Macro


The Macros option is the code group in the “Developer” tab, and it gives users access
to a list of macros that they can work with. Users can record new macros, run macros,
and even manage already recorded macros. A macro is a set of instructions/tasks that
a user can record and get them repeated automatically once a specific action is carried
out.

Figure 7.8: Macros and Record Macros

Under the Macro options, users can also open the Visual Basic Editor (VBE) Editor to
create, edit, and record macros that can perform frequent or repetitive tasks in MS
Office applications. While users can still create macros in the traditional manner,
writing a macro from the VBE provides more flexibility, and users can complete more
complicated tasks using VBA.

The “Record Macro” function in the “Developer” tab is used for recording the macro,
and will be referred to when this action is practically required in the study unit 8.

7.3.3 Use Relative References


The principle of relative references was covered in Topic 2, where you’ve learnt how
one cell that is associated with another in a formula in a spreadsheet, moves in relation
to each other.

Relative reference macros record an offset from the active cell. Such macros will be
useful if you have to repeat the steps at various places in the worksheet. Macros are
usually recorded in absolute mode, where it will refer to the same cells as the initially
recorded macro.

Depending on the outcome you would like to achieve in recording a macro, It is


sometimes necessary to record in the relative mode, using relative references. To do
this, you would need to activate relative references by clicking on the “Use relative
references” button. If you do not select the “use relative references” button, the
process of recording a macro will revert to its default setting, which is the “absolute”
recording.

7.3.4 Macro security


Macros (VBA code) have the potential to cause serious damage to your computer,
such as erasing files or installing malware (Alexander & Kusleika, 2022:871). Microsoft
has therefore added macro-security features to prevent macro-related problems
(Alexander & Kusleika, 2022:871).

Figure 7.9: Macro security


To view this setting, open Excel. Choose Developer. Click on “Macro security” as
illustrated above:
You will see the following options:

Figure 7.10: The Macro settings of the Trust Centre dialog box
Microsoft (Microsoft.com) provides the following guidance to select the correct option
on Macro settings:
Disable all macros without notification
This option should be selected if you don’t trust macros. Macros will not work in these
documents.
Disable all macros with notification
Click this option if you want macros to be disabled, but a notification should appear to
inform you that there are macros present in the document. If you trust the document,
then you can enable the macros from the message received.
Disable all macros except digitally signed macros
This setting is the same as the “Disable all macros with notification” option, except that
if the macro is digitally signed by a trusted publisher. Therefore, the macro can run if
you have already trusted the publisher. If you have not trusted the publisher, you are
notified. Note that all unsigned macros are disabled without notification.
Enable all macros (not recommended, potentially dangerous code can run)
Click this option to allow all macros to run. Using this setting makes your computer
vulnerable to potentially malicious code and is not recommended.
It is the view of the AIN2601 lecturers that the “Disable all macros with notification”
should be selected. If you distribute a macro solution to another user, then ask the
user to select “Enable Content” if he/she receives the notification. The notification the
user will receive is illustrated in Figure 7.11 below.

Figure 7.11: Macro security warning

7.3.5 Add-ins
Add-ins are additional programs that users can install to get additional features,
functionalities, and commands in Excel. When a user installs Excel for the first time,
add-ins are not immediately available, and users need to install and activate them first.
Add-ins and Excel Add-ins allow web technologies to interact with Office applications
across different platforms. They are similar to Google extensions and were previously
called Office Apps (now referred to as Office Add-ins). They replicate the role of an
assistant helping you with tasks that Office cannot do easily.
Analytical tools for statistics as well as automation add-ins can be included in the
workbooks by clicking on Excel add-ins and marking ticks on the required tool packs.
Some of the Excel add-ins are built within the application, such as Analysis Toolpak
and Solver, while others are developed by solution providers to perform specific
functions. Users can also create an add-in by writing or recording the code in a module,
saving it as an Excel add-in, and then inserting the add-in to the Quick Access toolbar.
COM Add-ins present the developer with a way to extend the functionality of Office
applications for custom tasks. COM Add-ins are typically used to automate Excel in
response to a click of a CommandBar button, a form or dialog box, or some other
event specific to Excel such as opening or closing workbooks or entering data on
worksheets. COM Add-in functions cannot be directly called from cell formulas in
worksheets.

Figure 7.12: Add-ins


Unlike COM add-ins, Excel add-ins do not require you to install any code on a user's
device, or within Excel.

7.3.6 Interactive Controls


The controls section allows an individual to add customisable objects that interact with
the data in user forms or documents. For instance, many online forms are devised with
radio buttons that provide only a unique choice of option selection for the question by
the user.
There are two types of controls in Excel worksheets that are used on worksheet forms.
They include Form Controls and ActiveX Controls. Form Controls are simple to use.
On the other hand, ActiveX is more advanced, and it provides a flexible design that
users can use in VBA.
Users can insert the controls in a worksheet, edit the VBA code for control, and switch
the design mode to on or off. Users can also use the controls to insert interactive tools
such as radio buttons, checkboxes, and combo boxes

Figure 7.13: Form Controls

There are 11 different types of controls in the Developer tab.

Check Box: also called tick box or selection box, is a control tool that displays the
selected option using a checkmark in a little square box. The check box is used to
select or deselect single and/or multiple given options.

Command buttons or push buttons are used to draw buttons that execute or run an
assigned macro. If a workbook contains multiple macros, it can sometimes be
inconvenient to run the desired macro from the Developer tab or by using Alt + F8 and,
in such instances, creating a command button and assigning the macro to it eases the
task for an Excel user.
Combo box is an interactive tool that allows you to add items of your choice and select
them via the drop-down list. The item can be selected by either typing in the text or by
clicking on it from the drop-down list.

Image Control is used to embed a picture such as JPEG or GIF allowing us to crop,
move and resize it while letting us display pictures as part of the data in the
spreadsheet; for instance, when using a company logo and employee photos on the
employee attendance spreadsheets.

A label in MS Excel is a descriptive text that gives information or instructions to the


user regarding the data in different rows and columns of the Excel. Labels are not
interactive by nature and display only the values that are assigned to them. For
example, "WallStreetOasis."

List Box: Like a combo box, a list box allows you to add items and lets you select a
value from a set of many values. Despite the similarity in function, they are different in
their appearances. A list box is scrollable and displays more values as opposed to a
combo box with only one value. A combo box will allow users to write text in the box,
while a list box does not contain a text box to specifically search for an item.

Option Button: An option button, or also called the radio button, is a graphical control
tool that accepts only a single response from the user from the several options in the
set. Any attempt to select another option automatically deselects the previous one,
meaning that option buttons are mutually exclusive, allowing only one response from
the user. Like a coin toss that will yield either a head or a tail, an option button will
allow only one response from the user.

Scroll Bar: The scroll bar allows you to increase or decrease the value of the linked
cell. You can easily navigate through a large list of items by using horizontal or vertical
scroll bars as well as the up-and-down arrow. The interactive nature of the scroll bar
allows a user to scroll for selecting the values from the given list rather than manually
typing the value, thereby saving time for the user.

Spin Button: Spin buttons are similar to scroll bars, allowing the user to make
increments or decrements of value by clicking on the up and down arrow, respectively.
This helps to quickly change a value or change a range of values such as number
increments, date, or time.
Text Box: A text box allows the user to insert text in them. By default, it allows you to
only add a single line of text; however, you can modify it to hold more information. It
can be static or dynamic, depending on the type of input you expect from other users.
For example, you can make the text boxes dynamic by creating a user form that will
accept age only as a number. Any alphabetic response will be rejected by the form
built in the spreadsheet.

Toggle Button: A toggle button is used to indicate a condition in Excel either as a


Yes/No or acts as an On/Off switch. When the toggle button is switched on, it enables
the particular condition and disables it when switched off. For example, a toggle button
can be used to hide and unhide the rows in a spreadsheet, charts, or pictures.

7.3.7 XML Commands


XML (Extensible Markup language) is a human- and computer-friendly format derived
from Standard Generalized Markup Language (SGML), which is used for representing
structured information. An XML tag identifies the data and stores it, as opposed to the
HTML tag, which is used to display the data.

If you have worked on HTML codes before, you might be aware that the two tags
<title> and <body> respectively display the text in different sections of the webpage.
On the other hand, irrespective of the position, an XML tag allows you to store the data
using self-descriptive tags or language that best suits the application. For example, a
part of XML data written as <phone>212-877-298X</phone> in notepad means that
an element is a phone number as described in the XML tag.

You can import data that’s in XML format into your spreadsheet.
Figure 7.14: XML data onto a spreadsheet

You can export your data in XML format so that other applications can use the XML
formatted data.

Figure 7.15: Exported data in XML

7.4 Summary
In this study unit, we looked at an introduction to VBA and its purpose within our
environment as a student. We also took a closer look at what VBA is, and started off
by navigating our way around the ribbon in Microsoft Excel to access the “Developer”
tab where VBA applications are located.

We also took a look at the VBE, which creates the environment for future coding to
take place and serves as our workbench for projects with VBA.

In the next study unit, we will delve deeper into VBA by looking at macros and how to
write some code.

REFERENCES

Alexander, A. & Kusleika, D. 2022. Microsoft Excel 365 Bible. The comprehensive
tutorial resource. 1st edition. John Wiley & Sons. New Jersey.

Alexander, A. & Walkenback, J. 2019. Excel VBA Programming for dummies. 5th
edition. John Wiley & Sons. New Jersey.

Etheridge, D. 2007. Microsoft Office Excel 2007 Programming. 1st edition. Wiley
Publishing, Inc.

Zych, A. 2015. Write your name in binary code. Available at:


https://www.sciencefriday.com/educational-resources/write-your-name-in-binary-
code/

You might also like