Excel VBA The Ultimate Intermediates Guide To Learn VBA Programming Step by Step by A - 1
Excel VBA The Ultimate Intermediates Guide To Learn VBA Programming Step by Step by A - 1
David A. Williams
Table Of Contents
Introduction
Chapter 1. VBA Integrated development environment
Chapter 2. Control Program Flow and Making Decisions in Visual Basic for Application
Chapter 3. How to Avoid Common Mistakes in Excel VBA
Chapter 4. Interacting with the user
Chapter 5. The “Speed Search Control Panel And Timing Comparison” Application
Chapter 6. Performing Statistical Analysis Using Formulas
Chapter 7. Scenario Building with One-way Data Table
Chapter 8. An Event Macro
Chapter 9. The Macro Recorder
Chapter 10 . The Excel Object Model
Chapter 11 . Derived Data Types in VBA
Chapter 12 . Beyond the Basics
Chapter 13 . The Cheat Sheet for Excel Formulas
Chapter 14 . Some other tips on VBA programming
Chapter 15 . Search And Reference Formulas
Chapter 16 . Department Data Validation
Chapter 17 . The Object Model
Chapter 18 . Relative and Absolute References
Chapter 19 . Project – Registration Form
Chapter 20 . A sample program
Chapter 21 . More Examples
Conclusion
Introduction
Excel is the most popular spreadsheet software in the world today. It was
developed by the giant software company Microsoft Inc. and is the single
most widely used spreadsheet program in businesses, government, schools,
universities, and other institutions.
While there are many different spreadsheet programs available, Excel
remains number one. It has continued to enjoy widespread use and massive
growth over the past three decades. There have been numerous upgrades
and new features introduced over the years.
Excel finds application in many different fields, including business
intelligence, finance, statistics, analysis, forecasting, billing, inventory, data
management, and so much more. This is why numerous employees and
workers are required to learn how to use this application if they are to be
effective in the workplace.
Most employers view Excel as an essential end-user computing tool,
especially those in the fields of accounting, information systems, and
business in general. It is widely used to carry out daily functions at the
workplace.
The first thing to check when preparing to use VBA for Excel is whether
macros have been enabled in the Excel version being used. To do this, go
to the developer option on the toolbar and click on Macro Security and
enable the usage of Macros.
Notice the above screenshot is for Excel 2016. This feature can be turned
off after practicing the parts of this book.
Now that the macros are enabled to get familiar with when programming
with Excel is the Visual Basic Interface/ Editor.
This can be opened in Excel 2016 by again clicking on the Developer
option on the main toolbar and choosing Visual Basic on the extreme left
corner.
Clicking this option will lead to the following screen/editor, as shown in the
picture below.
The white area on the right will become visible when clicking on sheet one
on the left-hand side. This is the default level, and many sheets can be
created depending on the project.
In this screen, the code for each sheet and object on the sheet will be written
on the right-hand side white area.
The various properties of the sheet and of objects will be shown on the
lower left. This is similar to that in Visual Basic.
One can select the properties and give it values through this segment of the
editor; however, the same can be done in code. The more professional way
is the latter.
On the top of the toolbar, the features that are needed the most are debugged
and run. Run makes the code run if every line of code is okay. Keep in mind
that logical errors are not checked by the debugger.
On running the code, a name for the new macro will be requested. Enter the
first macro for now and hit create. This macro will be a blank macro as no
code has been added to it.
A subroutine called the first macro will be created on the right-hand side of
the editor as follows.
Note that module1 has been added to the left-hand side of the editor's top
and bottom.
To display the words “Hello World” write the following on the right-hand
side within the subroutine first macro as follows:
Sub firstmacro()
Msgbox(“Hello World”)
End Sub
Now run the program
using the green > arrow on the top of the code on the sub toolbar or the run
option on the toolbar. The following will be the result.
This is the starting point of writing VBA. The steps that have been done so
far will be repeated again and again in the parts that follow.
Chapter 1. VBA Integrated development
environment
Getting into IDE
The Visual Basic Applications for computer programs are consolidated
inside all parts of the Microsoft Office suite for projects like Excel.
Initiating the VBA advancement programming sets the programming
condition for the IDE, which allows tools for use in working up the venture.
The speediest way into the VBA IDE is to click on Alt+F11 within the
essential Excel window.
It is similarly possible to confer from the standard Excel menu through
tools, at that point, Macro and afterward, the Visual Basic Editor. In the
wake of initiating the IDE, a window for the outlining of the VBA IDE and
a segment of the tools utilized for the production of projects will be created.
In numerous applications, there happens to be a menu bar over the highest
point of the window. You may simply see a few things that exist inside the
menu, and these will become evident as you proceed through the
manuscript.
One of the essential things would be the explorer window as showed up on
the topmost left area of the IDE window. The toolbar explorer records the
projects that are right by and by open, including the ones accessed via Excel
amidst the startup.
Besides that, the endeavor explorer records parts of any of the accessed
projects in the running with the figure; there is an outline of one envisions
known as Book 1. In the wake of opening it, it contains four Excel objects
that are sheet 1, 2, 3, and this workbook 1.
Just under the venture explorer window, as showed up in the above figure,
refers to the properties setting. The latter shows a quick overview of the
highlights or elements of the fundamental object found in the project. These
characteristics are needed to control the manifestation of the question where
they have a place.
The elements of sheet 1 appear in the above figure as picked inside the
undertaking explorer. Confirmation of some other object will allow
understanding of a properties list inside the properties window. Few parts of
the objects have relative properties. The main thing inside the control of
properties is to open another workbook and set the names.
In the event the undertaking explorer and the properties windows are not
starting at now demonstrated, then one can get to them by techniques for
the view menu. You may also use the keystrokes F4 and CTRL + R with a
specific target to get to the undertaking explorer and properties windows. At
the time that the undertaking explorer window is displayed, discover the
venture, which is illustrative of the workbook opened while in Excel. If the
workbook portions opened are not set, then click the + sign, which is close
by Microsoft Excel objects organizer that is under the undertaking name.
Then, consider the object that is named sheet one and a short time after
selecting it by then look towards the properties window. The subsequent
stage would be to consider down the properties window until the point, and
one would arrive at Name property, which is the one without the segment.
You can go to Excel by clicking Alt + F11 from the taskbar. It will be
evident the name of the sheet has, at present, been supplanted with MySheet
inside the Excel workbook. It has each one of the stores of being
unquestionably not hard to change the properties of the worksheet in Excel
with the utilization of VBA. As the VBA is creative, it will not be hard to
change the properties of the worksheet or workbook at the set time. By far,
most of the work affects the worksheets, and the workbooks would occur
during the runtime, as the goal is to alter the properties of ActiveX
controls.
In the event, one considers the figure of now; there is a standard window.
These are used as compartments for the projects. It is placed within the
program’s coding, so these windows function as content directors that take
after Notepad. As the client, one has to understand there exist pre-portrayed
code windows, particularly for Excel items, for example, the workbook.
The code window that displayed in the figure addressed starting at now
exhibits sheet one that is contained in the workbook known as Book 1.
There are more parts to the VBA IDE, though this is sufficient information
to begin with. Depending on the necessity, there will be further explanation
of other functionalities within the IDE leading furthering of projects.
Programming parts in Excel
Not every attractive element of the VBA programming software is located
inside the VBA IDE. Some programming-related parts are those that one
can obtain from Excel. The parts in this setting join the larger scale of items
that are detailed within the menu for the tools, and three of the toolbars,
Visual Basic, and the toolbox is placed inside Excel’s in-view menu.
Since the presentation part of VBA IDE has been anchored, it is pertinent to
consider how tools got can be arranged from Excel. Consider the vast scale
choice from the tools menu illustrated in the running. You should also note
the different items that displayed up in the figure, which have not yet been
tied down for these future Macros and Recording of new macros.
This type of construction runs one or more statements as long the condition
specified in the While statement is true (it is possible that the statements are
not executed if the condition is not true). When this type of construction is
used, make sure that the condition became true at one point; otherwise, the
code will enter an infinite loop and crashes.
Example:
Dim number As Byte
Number = 0
While number < 10
MsgBox number
number = number + 1 ‘variable will become ten and exit
the while
statement
End While
➢ Do Loops
This type of construction runs one or more statements as long the condition
specified in the Until statement is true (for Do-While construction,
statements will be run at least once even if the condition is false, as the
condition is tested at the end). When this type of construction is used, make
sure that the condition became true at one point; otherwise, the code will
enter an infinite loop and crashes.
Example1:
Dim number as Byte
number = 0
Do
MsgBox number
Number = number + 1
While (number < 10)
Example2:
Dim number As Byte
Number = 0
Do While (number < 10)
MsgBox number
Number = number + 1
Loop
➢ For Loops
This type of construction initiates a variable with initial value and last
value. The code runs the statements from the initial value until the last
value of the variable and auto increment it.
Definition:
For counter = start To end [ Step ]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
If it is needed to break the for and exit from it, it can be included an IF-
Then structure inside the for and use “Exit For” syntax to exit the for loop
at any time. For structures are the easiest way to loop through an array
items.
Next syntax (last line for the for construction), it auto-increment the value
with the specified step (positive or negative step).
Example1:
Dim number As Byte
For number = 0 to 10
MsgBox number
Next number
-numbers from 0 to 10 will be displayed ascending
Example2:
Dim number As Byte
For number = 10 to 0 Step -1
MsgBox number
Next number
-numbers from 10 to 0 will be displayed descending
Example3:
Dim Names(10) As String
Dim i as Byte
Names(0) = “ABC”
Names(1) = “BCD”
Names(2) = “CDE”
Names(3) = “DEF”
Names(4) = “EFG”
Names(5) = “FGH”
Names(6) = “GHI”
Names(7) = “HIJ”
Names(8) = “IJK”
Names(9) = “JKL”
For i = 0 to 9
If Names(i) = “FGH” then
MsgBox “FGH reached. For will be exit.”
Exit For
Else
MsgBox Names(i)
End If
Next i
-the for construction will loop through Names array and display the names
from the array. If the name is equal with “FGH,” then the for construction
will be interrupted (exit for)
➢ For Each Loop
This type of construction is similar to for-next construction, and the only
difference is that this construction is used to loop through elements of an
array or list without an index (it loops through values directly). The code
runs the statements from the first value until the last value of the structure
and auto-increment.
Definition:
For Each element In group
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ element ]
Example1:
Dim listOfNames as New List(of String) From_
(“ABC”,”BCD”,”CDE”,”DEF”)
For Each item As String In listOfNames
MsgBox item
Next
Example2:
Dim numbers(3) as Byte = (1,2,3)
Dim nr as Byte
Dim letters(3) As Char = {“a”, ”b”, ”c”}
Dim lett as Char
For Each nr in numbers
For Each lett in letters
MsgBox nr & “ “ & lett
Next
Next
-this example loops through both list (numbers and letters) and will display
the combination of the items (1a; 2a; 3a; 1b; 2b; 3b; etc.)
❖ Other Control Structures
➢ Using – End Structure
This structure construction establishes a statement block within the user can
use a resource (example SQL connection – needs to close all SQL
connections after interrogation of a database). The resource can be acquired
with Using keyword (when inserting Using keyword VBA will
automatically display a list of possible resources that can be used).
Definition:
Using { resourceList | resourceExpression }
[ statements ]
End Using
Example:
Sub WriteToTXTFile()
Using writer As System.IO.TextWriter = _
System.IO.File.CreateText("filename.txt")
writer.WriteLine("First line of text.")
writer.WriteLine("Second line of text.")
writer.WriteLine("Third line of text.")
End Using
End Sub
Sub ReadFromTXTFile()
Using reader As System.IO.TextReader = _
System.IO.File.OpenText("filename.txt")
Dim lineText As Strin g
lineText = reader.ReadLine()
Do Until line Is Nothing
MsgBox lineText
lineText = reader.ReadLine()
Loop
End Using
End Sub
-in this example are created two subs, one to create a txt file (filename.txt)
and write 3 lines inside the file, and the second one is to read the content of
the txt file and message line by line the content of the file. Because of the
TextWriter and TextReader classes implement in the IDisposable interface
the code use Using keyword to ensure that the txt file is closed right after
read/write operations.
➢ With – End Structure
This structure allows the user to specify an object reference once and then
access its members. This structure improves performance of the code
because the Visual Basic does not have to establish the reference for each
member, it establishes the reference once and access multiple members of
it.
Definition:
With objectExpression
[ statements ]
End With
Example1:
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
-this example set some application properties (used to improve the speed
and performance of the code) by establishing the reference for Application
just once and modify four of the members (without this structure it is
needed to create four Application references which slows down the code)
Example2:
With ThisWorkbook.Sheets(1).Range(“A1:A1”)
.Value = 2
.BackgroundColor = RGB (0,0,255)
.Borders(xlEdgeBottom).LineStyle = xlContinuous
End With
-this example set value, background color and border for a cell by
establishing the reference to Range property only once (establish reference
once, modify 3 parameters)
❖ Nested Control Structures
It refers to the possibility of combine multiple structures, some control
statements inside other control statements.
➢ Nesting Levels
All the control structures can be nested as many times is needed and as
many levels as are needed.
Example:
Dim i, j As Long
For i = 1 to 100
For j = 1 to 100
If i < j then
‘Statements
Else
‘Statements
End If
Next
Next
➢ Nesting Different Kinds of Control Structures
Different types of controls can be nested together.
Example:
Dim i, j As Long
For i = 1 to 100
j=i
While j < 50
If i + j < 100 then
‘Statements
Else
‘Statements
End If
j = j +1
Next
Next
➢ Overlapping Control Structures
Control structures can also be overlapped (ex. while structure can overlap
for structure).
Chapter 3. How to Avoid Common Mistakes in
Excel VBA
There is a lot to learn in Excel VBA and it can easily overwhelm you if
you’re not careful. As a beginner, you may find that you will make more
mistakes than you would like to. Don’t worry about that, it is perfectly
normal however, there are a few tricks that can help you from venturing off
down the wrong path so you don’t have to back track and fix them.
It is true that we all learn from our mistakes but especially in the world of
business, mistakes can quite often prove costly. Learning how to use VBA
is like learning any other skill, which means that there is only so much you
can learn from a book, the rest must be acquired through practice.
Remember, nothing worth learning can be glossed over and rushed. So take
your time and practice these skills over and over again until they feel like
second nature to you. If you take the time to do this first, before you expose
your newfound skill to your business, you’ll have less chance of making
those kinds of errors that could prove costly.
Still, there are a number of common mistakes people make with VBA that
are definitely worth taking a closer look at. By being aware of these types
of mistakes you can use your practice sessions to figure out ways to work
around them so you are less likely to fall into those traps when you take
your new skills and apply them in real life.
Overusing .Select and .Activate
Select and Activate are two of the most common commands you use when
using a macro recorder. However, more often than not they are not even
necessary. In many cases, using them could prove to be quite redundant for
several reasons. So, if you don’t need this particular function then it’s a
better idea to delete those commands altogether. Once you do, you will see
the code improve, working faster than normal. However, if you feel you
will need to perform either of these two functions, it is a good idea to keep
the commands in place as it is a means of ensuring the code continues to
function in the correct way.
Overuse of the variant type
Sometimes new users of VBA can get the many variant types confused. You
may think that you are using one type when in actuality you are using a
totally different one. For example, using a Dim code such as “Dim d, e, f as
Long”. You might assume that this is a Long type variant. It’s a logical
conclusion and a mistake that many newbies actually make.
If you’ve made this mistake, don’t worry, you’re in good company.
However, only types “d”, and “e” would be variant types, meaning that they
can be any type and can be changed from one type to the other whenever
needed.
Using this type of coding can be very tricky and confusing. Since using
them could create a lot of errors in the code that could be extremely difficult
to find. Because this is such an easy mistake to make, it may be a good idea
to avoid using Variant variables in the beginning. Once you become more
familiar with the other more basic forms of coding you can gradually ease
your head into the lion’s mouth and practice this more advanced form. It
will save you a lot of problems that will inevitably cause you frustration if
you do.
Underuse of the Application.ScreenUpdating = False
Every time you make a change to a cell in your worksheet, Excel will go
through and update the screen. This will ensure that your new changes
appear as you make them. If you are working for an extended amount of
time, you could easily input a lot of data. Depending on the power of your
computer and the amount of data you’ve accumulated, it could lead to a lag
time in the system if it is not updated. To do this, you can always stop your
work at some point and do an update by following the code below:
Public Sub MakeCodeFaster()
Application.ScreendUpdating = False
‘do some stuff
‘Always remember to reset this setting back!
Application.ScreenUpdating = True
End Sub
While the system does automatically update the screen periodically. If the
data you’re working with is very important, it never hurts to add a manual
update to protect your work.
Adding a name of a worksheet to a string when it is not necessary
It can be very tempting to write codes referencing the specific name of a
worksheet. It can be a means of ensuring that the program will access the
correct worksheet in a string. However, when working in VBA this is not
necessary. Consider this example:
Public Sub SheetReferenceExample()
Dim ws As Worksheet
Set ws = Sheets(“Sheet1”)
Debug.Print ws.Name
End Sub
While this type of mistake won’t yield you any negative impact, if you do
not break this habit, it may cause difficulties for others who try to use your
programs. For example, if you took your worksheet and gave it to someone
else who wanted to rename it, then it may no longer function as it should
because you’ve named the worksheet when it wasn’t necessary. So, in the
interest of camaraderie, it just makes sense that you try not to develop this
type of habit in the first place.
The best practice is to make sure that all of your references directly point to
a corresponding object and then allow the program to find the sheet. As
long as you have added the right code, you will see Sheet1 and not a series
of sheets.
Failing to qualify range references
When working with VBA, beginners usually fail to qualify their range
references. This is a common error as many newbies are not really sure
what to look for. So, it is very important that you take extra care to ensure
that you qualify all references as soon as possible so you don’t get stalled
after.
If you’re unsure of either what a range reference is or you’re in doubt about
how to find them, here is are a few things to keep in mind that could help
you.
If the code is Range (A1) then you need to consider the exact worksheet it
is referring back to. Your answer to this problem will always refer to your
active sheet unless there is something else in the code that is referencing a
different page. As a rule of thumb, the ActiveSheet will always be the sheet
that is currently under rechecking.
If you use Range() without specifying which worksheet to use, Excel will
automatically assume that you are referencing the active sheet. So, if you
want to reference a different sheet, you need to make sure that is added to
your code beforehand.
Sub functions that are too long
If you create a function and the result is longer than a single page of your
module then there is a good chance that it is too complicated or too long.
Long codes are not a problem in and of themselves but the more complex
the code is the more of a chance of error or of a code that lags and bogs
down the system. There are quite a few things you can do to prevent your
code from just going over the top. But one of the easiest ways to avoid this
problem is to pay extra attention to keeping the Sub and Function methods
as lean as possible. Make good use of any helper functions or any possible
sub procedures you can apply that will help you to do this.
It is inevitable that you will make mistakes and you learn this new skill but
those that take the time, practice, and pick up on these many tricks will
master it much faster than many others who just enjoy the experience of
diving right in.
We all know how complicated and confusing these things can be when you
are just learning how to use Excel VBA. No doubt, it will take an
investment in time to make sure you fully understand exactly how the
module works and what you have to do. If by some chance, you are having
issues with the coding, take the time to look up some of the more common
problems that others face and learn how they fixed them.
Chapter 4. Interacting with the user
Introduction
With VBA you can also make applications that look professional. So far we
have worked with the fundamentals that will allow us to handle many of the
strengths that make Excel and its programming language a very powerful
tool for the development of applications. Now we will see how we can
interact with the user of our applications, through forms (Userforms) that
are no more than the windows in which requests for information are made
or results are presented. The above can be done using either numbers, texts,
images and / or graphics. VBA offers a wide variety of controls to display
information to the user and to request it called widgets. Through these
controls you can display information, show options for the user to choose,
buttons that perform tasks, etc.
Userforms
Userforms are the main container which contains a graphical application.
Within a form there will be labels, buttons, lists of options, text controls,
etc. It is the interface with which the user interacts. Figure 11.1 shows how
an empty form looks. It is composed of a main area where we will add our
controls (enclosed in a rectangle). It has an "x" in the upper right corner,
which allows closing the window (forever), which in turn terminates the
execution of the program. This option is a "violent" way to exit the
execution, since it is also a way to abort the execution of the program (or
that portion of the program). In the upper left corner of the form there is a
text that is often used to show the name of the application, but in general it
can be customized with any message (in our figure it is where UserForm1
says).
Figure 11.2 shows a window with all the controls that can be added to a
form to customize it. We will give a brief explanation of each of them, by
building an application.
The application that we are going to develop is based on the subroutine 10.2
with the difference that now we will ask the user to enter the range of data
that is taken into account for the calculations. The user can choose the
calculation that he wants and the result will be shown in the form, instead of
using the MsgBox function. Additionally, the program will count the
number of samples in a range of ages.
In numeral 2.5 we showed how to add a form. To add a control, we select it
from the Toolbox (figure 11.2) and drag it into the form. There we can
move it to the position that we like and we can even change its size.
Another way of adding a control is by clicking on it and moving the mouse
to the form and clicking on it.
Userform.
Let's start by customizing our form. In figure 11.3 we can see it in the
design phase. In case we want to modify the size we can do it by dragging
one of the white squares that surround the form. It can also be done
manually, modifying its Height and Width properties in the properties
window (window located in the bottom left of figure 11.3).
Figure 11.2. Userform Toolbox (widgets).
The first thing we are going to do is change the name that the form will
have. To do this, we change the Name property.. As can be seen in figure
11.3, the name (Name in the properties window) and the title (Caption) are
the same. Changing the name does not change the title. The difference
between both is that the name (Name) will be used to refer to the form in
the code. The title can be a word or sentence that we want to show.
Property Value
Name Stat
Caption Statistical software
Figure 11.13. Space for the subroutine of the "Run" button associated with
the Dblclick event.
We are also going to program the KeyPress event, which will be activated
when the user, after clicking the "Run" button, now press any key. The
program will show a message that this option is not valid to run the
application. Figure 11.14 shows the body of the subroutine, associated with
the KeyPress event (rectangle).
Figure 11.14. Subroutine for the KeyPress event of the "Run" button.
Subroutine 11.1 shows the code for the KeyPress event.
Subroutine 11.1.
1 Private Sub Run_KeyPress(ByVal KeyAscii As
MSForms.ReturnInteger)
2 MsgBox "It is not here." & _
3 vbNewLine & "You must double-click on the button" &
vbNewLine & _
4 "to be able to run the application", _
5 vbOKOnly + vbCritical, "Results"
6 End Sub
The subroutine 11.2 shows the code associated with the DblClick event of
the "Run" button. This code is the one that will execute the calculations of
the application.
Subroutine 11.2.
1 Private Sub Run_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
2 Dim R As Range
3 Dim i, rows, n, lla, ula As Integer
4
5 Set R = Range(DataRange.Value)
6 rows = R.rows.Count
7 n=0
8 lla = CInt(LowerLimit.Value) 'Min Age
9 ula = CInt(UpperLimit.Value) 'Max Age
10
11 If lla >= WorksheetFunction.min(R) _
12 And ula <= WorksheetFunction.max(R) Then
13 For i = 1 To rows + 1
14 If R.Cells(i, 1).Value >= lla And _
15 R.Cells(i, 1).Value <= ula Then
16 n=n+1
17 End If
18 Next i
19 End If
20
21 If StatDevOp.Value = True Then
22 StandardDevRes =
VBA.Format(WorksheetFunction.StDev(R), "0.00")
23 End If
24 If AverageOp.Value = True Then
25 AverageRes = VBA.Format(WorksheetFunction.average(R),
"0.00")
26 End If
27 If MinAgeOp.Value = True Then
28 LowerAgeRes = WorksheetFunction.min(R)
29 End If
30 If MaxAgeOp.Value = True Then
31 UpperAgeRes = WorksheetFunction.max(R)
32 End If
33 nElementsRes = n 'elements in the age range
34 End Sub
To execute this program, you can press F5 or the button Execute macro (it
is easy to recognize this button in the standard toolbar: it is a green triangle
pointing to the right). This should be done in this way, since a form is not in
itself a complete program, but part of a larger one. In order for an
application with windows (forms) to be launched without having to be in
the programming environment, its invocation must be done from (or be part
of) a subroutine.
In figure 11.15 you can see a stage during the execution of the program, in
which the selection of the range of cells to be used in the calculations is
shown. Figure 11.16 shows the window with the results.
To exit the application, the "Exit" button was programmed. The code that is
executed when pressing it is the one that is shown in subroutine 11.3.
Subroutine 11.3.
1 Private Sub
ExitStat_Click()
2 Unload Me
3 End Sub
The formula that comes after will bring back the month corresponding to
the earlier formula’s temperature.
=INDEX(B4:B15,MATCH(E6,C4:C15,FALSE),1)
Two functions are provided by Excel to determine the smallest and largest
values found in a range: MIN and MAX. The two formulas can accept
about 255 arguments. The data we are working with is found in C4:C15,
and this is the range passed into MIN and MAX. MAX returns the highest
value in the range, which is 73, while MIN returns the smallest value,
which is 31.
Calculation of Median, Mean and Mode
When referring to average, we are talking about the arithmetic mean. If we
have some student’s scores set at cell C4 to Cell 23. For mean, we use the
AVERAGE function. To compute the median, mean, and mode for these
scores, we use:
=MEDIAN (C4:C23)
=AVERAGE (C4:C23)
=MODE (C4:C23)
If there’s a great difference between the MEDIAN and AVERAGE, it
means the grades were not distributed evenly throughout the population.
The MEDIAN returns the value that is in the center; if the number of grades
is even in number, then we cannot get a particular value that claims the
middle spot. So, in this case, we need to find the mean of the two values.
The MODE gives you the grade that appears the most.
Data separation into percentiles
When data is separated into bins or buckets, we are providing insight into
how each of these values is compared to the whole. The figure below
reveals a list of workers processing a product and how much default per one
thousand products. Here, we’ll be trying to bucket the data to 4 bins to
easily identify those performing well as well as those workers that may
need additional training.
To compute this demarcation, Excel 19 has the QUARTILE function. A
quartile is a type of bucket holding 25% of the whole data.
=QUARTILE ($C$4:$C$33,6-ROW(A2))
fx =QUARTILE ($C$4:$C$33,6-
ROW(A2))
A B C D E F G H
1
2
3 Employee Defects/1000 Quartile
4 Adam 47 1 Maximum 50.0
John
5 Alex Cone 31 3 25th 23.0
percentile
6 Alfred 50 1 50th 31.0
Gonzalez percentile
7 Alyssa 41 2 75th 44.5
Clark percentile
8 Amelia 30 3
Rose
9 Anna 30 3
Grace
1 Audrey 23 4
0 Love
11 Chloe 27 3
Brian
1 Eli Brown 34 2
2
1 Eric Green 48 1
3
1 Evan Lucy 17 4
4
1 Evelyn 22 4
5 Salt
1 Gabriel 43 2
6 John
1 Gabriella 45 1
7 Daniel
1 Genesis 23 4
8 Stone
1 Isabella 49 1
9 Garcia
2 Jaden 50 1
0 Henry
2 Jerry 19 4
1 Palmer
2 Joseph 17 4
2 Arthur
2 Katelyn 31 3
3 Stark
2 Kyle 19 4
4 Walker
2 Laura 47 1
5 Allen
2 Layla 47 1
6 Lovren
2 Lillian 27 3
7 Marshall
2 Mason 41 2
8 Matthews
2 Nathaniel 42 2
9 Allen
3 Samuel 21 4
0 Gomez
3 Sean 39 2
1 Chavez
3 Victoria 20 4
2 Cox
3 William 27 3
3 Freeman
3
4
There we are only interested in how many scored above 80. Here, however,
we need to do more. We could manually change the cutoff value (from 65
to 90, five times), and record the results for each cutoff. It will be great if
we could automate the process and let Excel perform this scenario analysis
on its own. Fortunately, Excel’s Data Table does just that.
In here, we just need to vary one variable, i.e, the cutoff value, which is
stored in a separate cell (B4). This allows us to do a one-way data table,
whose procedures are as follows:
1- List all the cutoff values in one column;
2- In the header of the adjacent column over, define what output is here.
Because we are interested in how many restaurant will cross a certain
threshold, which is in cell B4, we just type =B32, as the following shows:
3- Select the entire table, including the header, and go to Data → What-if
→ Data Table, and the following window shows up.
This basically ask you where those scenarios of cutoff values should go into
the model. The answer is cell B4, where the original cutoff value is stored.
Because those scenarios are lined up in a column as we did in step 1, we
leave “Row Input Cell” empty, and in “Column Input Cell” select cell B4.
You will notice that Excel automatically adds $ signs to absolute reference
that cell. After you click OK, the magic happens:
By now, you should be able to see what Data Table does. It is still the
model’s job to formulate a model and specify scenarios. But Excel can
automate the calculation for each scenario, and produce the desired results
for each scenario very quickly.
Then, let us look at an example, where it shows that you can output more
than one variable of interests using one-way data table.
Here all our scenarios are in a column, and they all refer to different down
payment in our original model. So enter $C$4 in the “Column input cell”
field. Leave the “Row input cell” field empty. Once you click OK, new
numbers will fill the table.
To check our results, as the down payment goes up, the monthly payment
decreases. So does the total interest payment. This is indeed what we
should expect. In fact, we can compute the difference for each increment in
down payment: every $10,000 increase in down payment will decrease
monthly payment by $76.50, and total interest payment by $3,769.88. With
such analytics knowledge, now it is up to Mary and Joe to decide which
down payment option to choose.
Note that, there are two different y-axis with different scales. This is
because the magnitude of the series differ too much to be meaningfully
presented in one figure (to see how to do add a second axis, check the
visualize segment of this course). Nevertheless, the slopes of the two lines
are exactly as we have anticipated: As one pays more down, her monthly
payment as well as total interests goes down. Because the y-axis are
negative numbers the chart shows a positive slope.
This following example shows that when the scenarios are built based on
two different cells, separates data tables need to be built.
Example (Which Channel? Scenario Analysis) Tom, an aspiring author, has
written a new novel and is using four channels to publish and distribute his
writing. There are two large online channels (called A and B), an
independent self-publishing channel (called C), and a physical store (called
D). Each channel has its own fee structure, and each commands different
market share (listed below).
Suppose the novel will sell 1,000 copies, and Tom has decided to price it at
$9.99. In addition to knowing the total revenue he gets from each channel,
Tom is concerned with the following:
(1) He wants to know how his income would change if his top two channels
(A and B) were to change their fee percentage.
(2) He is also curious as to how the overall demand will affect his revenue
from all four channels.
Build a what-if analysis to answer Tom’s questions.
Solution Before doing scenario analysis, we need to build a model. This
model has been built and mentioned in the Build Models module of this
course. So we will not repeat here, except to show the completed model
here (from Row 1 to Row 12 in the picture below).
To answer the first question, we need to set up a one-way data table for fee
percentages for channels A and B. To do that, let us say they could vary
between 10% to 70% for both channels, each in 5% increment, and list
them out as two separate columns (in B18-B30, and D18-D30,
respectively).
Then, we define our variables of interest. For channel A, it is the total
revenue from A in cell B12. Therefore, in the top row of the data table (cell
B17), we enter =B12.
The third step is to select the entire table (A17-B30), and go to Data→
What-if → Data table. Because those scenarios are about channel A’s fee
percentage in cell B12, we enter that as column input cell, and leave row
input cell empty (see below).
You will see the table gets filled after your click on OK.
Repeat the same steps for channel B. Its column input cell should be C8,
where the fee percentage of channel B is.
Now that we have the two tables, we can put them in one figure:
We can see that as the fee percentages for both channels go up, the authors
get less revenue. Moreover, we see that the slope of decrease is steeper for
channel A. In fact, from the data tables, we can compute the difference in
revenue for each 5% increase in fees. For channel A, it is $364.63, and for
channel B it is $69.93. Why?
The reason is apparent if we apply our modeling thinking and go back to
our original model. The objective of total revenue from each channel is
formulated as:
Revenue from a channel = Demand x unit revenue.
In our model, any fee change will not cause any change in demand, but it
will affect unit revenue to the author which is formulated as:
Unit revenue = Unit price x (1 - fee percentage).
So, any 5% increase in fee will result in a reduction of unit revenue by
$9.99 x 5% = $0.4995. Demand remains to be 1000 units x 73% market
share = 730 units. Hence the net effect of change in total revenue from
channel A is $0.4995 x 730 = $364.63.
Similarly for channel B, the net effect of change in total revenue is $0.4995
x (1000 x 14%) = $69.93.
The implication of this result is that the author’s revenue is more sensitive
to the channel that has the highest market share (i.e., channel A), therefore
more attention needs to be paid on this key variable.
To answer the second question on the effect of overall demand, we follow
the same procedure. To create scenarios, we vary demand from 300 up to
1800 with the increment of 100. This time, however, because all four
quantities of interests (Revenue A, B, C, and D) are functions of the same
demand, we only need one DataTable, as shown below.
In row 17, we link the four variables to the original model. Then select the
whole range of A17-E33, and go to Data Table, and select Demand (cell
B4) as column input cell:
After the table is filled, we can
then plot a line chart to visualize the effects (you can also use other
techniques such as conditional formatting there we have covered in the
Visualize module).
As
expected, we see that because channel A has the largest market share,
revenue from this channel is much more sensitive to demand changes.
Since channel B and channel C have relatively small market share
compared to channel A, their revenue impact is similar even though their
fees differ quite a bit.
So far we have covered one-way Data Table: scenario studies on just one
variable. Excel can also do two-way Data Table, considering simultaneous
changes in two variables. Let’s look at how it is done.
Exercise on one-way DataTable
1. In the channel example above, Tom is interested in seeing how his
revenue may change with potential changes in channel A’s market share
(say, at 50%, 60%, 70%, 80% and 90%). Build a model to answer that.
2. In the restaurant rating example, repeat the one-way table analysis but
this time calculate how many restaurants are (1) over each threshold and (2)
have agreed to be included in the guide.
3. In the mortgage example, what is the impact of reducing number of
payments on monthly mortgage payment and total interest amount?
Chapter 8. An Event Macro
The VBA is an event-oriented language.
An event macro enables a macro to run automatically following certain
events made in the manuscript, such as opening, closing, saving, double-
clicking, changing and more.
A macro of this kind is written at the worksheet level or at the workbook
level and not at the module level.
An event macro written at the worksheet level will apply only to the
worksheet it was written in, while an event macro at the workbook level
will apply to all the worksheets in the file.
Note:
The macro names are pre-determined and cannot be changed.
An event macro at the worksheet level
1. Double-click the name of the desired worksheet in the object
explorer:
__ 9) Either do a Ctrl-C (Control Key + the letter C) or from the menu bar,
select Edit, Copy.
__ 10) Switch to Sheet2. Select Cell F1. Then from the menu bar, select
Edit, Paste. Your data will be pasted into Sheet2 starting with cell F1.
__ 11) Now, in Sheet2, click on cell A1.
__ 12) Switch to Sheet1. Notice that the selected cells still have the active
copy status. Press the ESC key and click on cell A1 in Sheet1.
__ 13) You have performed all the actions you required, so now you are
ready to stop recording the macro. Either use the “Stop Recording” icon, or
from the menu bar, select Tools, Macro, Stop Recording. As soon as you
do this, your macro will appear in the VBE within a module.
__ 14) The code generated appears as follows:
__ 15 ) A professional developer would replace all the code above with a
much simpler version:
Sub CopySheet1CellsToSheet2Simple()
ThisWorkbook.Sheets("Sheet1").Range("A1:E10").Copy
ThisWorkbook.Sheets("Sheet2").Range("F1")
End Sub
The single statement used to copy cells A1:E10 from Sheet1 to Sheet2
explicitly references the workbook and worksheet names of the cells that
are copied. Another method, using workbook and worksheet variables, will
be described shortly in detail but the following example gives you a view.
While using workbook and worksheet variables increases the number of
statements, it is the best way to code for more complex applications. Here’s
the same copy function, but coded with workbook and worksheet variables:
Sub CopySheet1CellsToSheet2WithVar()
Dim wkbCopyExample As Workbook
Dim wksSourceWorksheet As Worksheet
Dim wksTargetWorksheet As Worksheet
Set wkbCopyExample = ThisWorkbook
Set wksSourceWorksheet = wkbCopyExample.Sheets("Sheet1")
Set wksTargetWorksheet = wkbCopyExample.Sheets("Sheet2")
wksSourceWorksheet.Range("A1:E10").Copy
wksTargetWorksheet.Range("F1")
End Sub
Launching The Macro Recorder In Excel 2007, 2010 and 2013
We will use the same example as we did for Excel 2003 - recording a macro
that will copy cells A1 through E10 from Sheet1 to cell F1 in Sheet2.
These are the steps from your Excel 2007/2010/2013:
__ 1) Open a new blank workbook
__ 2) Place some data in cells A1 through E10 of Sheet1
__ 3) Save the workbook as a macro-enabled workbook using any name
you choose by selecting the File tab, then Save As, and change the file type
to macro-enabled workbook:
__ 4) Start recording the macro by using the “Start Macro Recording”
button in the lower left portion of your screen. (You can also use the
“Record Macro” button in the Ribbon’s Developer Tab or the “Record
Macro” button in the Ribbon’s View Tab…it’s hiding underneath the
“Macros” button as a drop-down).
__ 12) As soon as you do this, your macro will appear in the VBE within a
module. The code generated appears as follows:
__ 13 ) A professional developer would replace all the code above with a
much simpler version:
Sub CopySheet1CellsToSheet2Simple()
ThisWorkbook.Sheets("Sheet1").Range("A1:E10").Copy
ThisWorkbook.Sheets("Sheet2").Range("F1")
End Sub
The single statement used to copy cells A1:E10 from Sheet1 to Sheet2
explicitly references the workbook and worksheet names of the cells that
are copied. Another method, using workbook and worksheet variables, will
be described shortly in detail but the following example gives you a view.
While using workbook and worksheet variables increases the number of
statements, it is the best way to code for more complex applications. Here’s
the same copy function, but coded with workbook and worksheet variables:
Sub CopySheet1CellsToSheet2WithVar()
Dim wkbCopyExample As Workbook
Dim wksSourceWorksheet As Worksheet
Dim wksTargetWorksheet As Worksheet
Set wkbCopyExample = ThisWorkbook
Set wksSourceWorksheet = wkbCopyExample.Sheets("Sheet1")
Set wksTargetWorksheet = wkbCopyExample.Sheets("Sheet2")
wksSourceWorksheet.Range("A1:E10").Copy
wksTargetWorksheet.Range("F1")
End Sub
Chapter 10. The Excel Object Model
As you use Excel, you probably see the workbooks, worksheets, and ranges
as part of the application and not as individual objects. Internally, however,
Excel treats these parts as individual, self-contained, objects in a
hierarchical model called the Excel Object Model. You can take advantage
of these objects to create VBA solutions without having to write too much
code.
Objects and Collections
In programming or software, objects are packaged functionality with a
defined set of behaviours (known as methods in VBA) and characteristics
(known as properties in VBA). You can put various objects together to
create solutions programmatically.
A collection is an object that groups several related objects together so that
they can be programmatically controlled as a unit. A collection can have
zero or more related objects. For example, there is a Worksheets collection
which can hold several Worksheet objects. Each Worksheet object in the
collection has its own properties and methods. However, the Worksheets
collection also has its own properties and methods that you can use to
control multiple Worksheet objects within it as a single unit.
The Object Hierarchy
The Excel object model is a structured model with different objects
organised into a hierarchy based on the relationships between the objects.
The object model defines which objects are exposed and how they relate to
each other. The highest object in the hierarchy is Application, which
represents the Excel application itself.
The image below shows a subset of the Excel object model. A rectangle
represents a collection of objects and an oval represents a single object.
You can use the pre-packaged functionality in Excel objects to create
custom solutions without having to write the code from scratch. For
example, you may want to create a solution that uses the PivotTable object
in Excel to display data to the user. You don't have to write the code to
create a pivot table from scratch. That would be way too daunting! Instead,
you can declare a PivotTable object and access the functionality that’s
already built into it.
Declaring an Object
To create an instance of an object to access its methods and properties, you
must first declare it as an object variable in your procedure.
For example, the following code declares rngVAT as a Range object
variable:
Dim rngVAT As Range
If you intend to use an object in multiple places in your code, you can
assign it to an object variable using the Set command. This makes for a
cleaner and more efficient code. The following example assigns a range
named Vat to a variable using Set:
Set rngVAT = Workbooks("AnnualFigures.xlsx") _
Worksheets("Sheet1"
).Range("Vat")
We can then access properties and methods of the object using the variable,
like in the example below:
rngVAT.Value = 0.2
You can also declare an object variable and use it in a For…Each loop to
iterate through a collection to access instances of the object. In the example
below, we loop through a collection (which is a range of selected cells) and
use a Debug.Print statement to display the value of each cell in the
Immediate window.
Dim rngCell As Range
For Each rngCell In Selection.Cells
Debug.Print rngCell.Value
Next rngCell
Properties and Methods
To access the functionality provided by objects, you work with properties
and methods.
Properties
Properties are a set of characteristics of an object. For example, the
Worksheet object has properties like Name and Visible.
Methods
Methods are behaviours that an object can exhibit. These are actions that
you can carry out with an object. For example, the Worksheet object has
methods like ShowAllData, Protect and Calculate.
Tip: To get a detailed list of the methods and properties of an Excel object.
Use the format “object name properties” or “object name methods” to
search for it in your search engine, where object name is the name of the
object for which you want help. One of the top search results will be the
official VBA help page for that object.
Setting Properties and Calling Methods
To change the characteristics or attributes of an object, you change its
properties by assigning different values to them. Methods, on the other
hand, cause an object to carry out an action or a task. Hence, you use
methods to perform actions and use properties to set or retrieve attributes of
an object.
Setting Properties
To set a property of an object in code, use the following syntax:
Object.Property = Value
The following example assigns True to the Bold property of the Font object
of the selected range. This bolds the text in that range.
Selection.Font.Bold = True
To turn off bold you can set the property to False, like this.
Selection.Font.Bold = True
Calling Methods
The way you invoke methods in your code will depend on whether the
method returns a value and if that value will be used in your procedure. You
can think of methods as verbs. For example, you can open a door. So, if we
were to translate that to VBA code, it would be:
Door.Open
Some methods return a value and others don’t. Also, some methods have
parameters and others don’t.
Note: A parameter is a variable that is part of a method’s definition or
syntax. When you call the method in your code, the values you pass into the
parameters are called arguments. These terms are sometimes used
interchangeably.
If you don't want to use the value returned by a method, or if the method
doesn't return a value, then use the syntax below to call it. Note that the
arguments are optional as not all methods have parameters.
Object.Method [arg1, arg2, …]
An example of a method that does not have a parameter or a return value is
the Select method of the Range object:
Range("A1:A10").Select
The following statement runs the AutoFit method of the Columns collection
to auto-fit the text in the selected columns.
Selection.Columns.AutoFit
If a method returns a value, then you call it by assigning the return value to
a variable.
If the method has parameters, then you place parentheses around the
arguments when you call it. You generally want to use parentheses any time
the method appears to the right of the equal sign.
Use the following syntax to call a method when you want to assign the
return value to a variable:
Variable = Object.Method([arg1, arg2, …])
You can also explicitly define a parameter by assigning an argument
directly to its name when you call the method. The following example
copies data from cells A1:A4 on Sheet1 to cells B1:B4 on Sheet1. You can
see that the Destination parameter was explicitly defined.
Sub CopyAndPaste()
Worksheets("Sheet1"
).Range("A1:A4").Copy
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range("B1:B4")
End Sub
Using the With…End With Statement
The With...End With statement is something you will encounter often when
you’re editing macros generated by the macro recorder. This statement is
used primarily to access properties and methods of objects.
When you access properties and call methods, you will often write several
statements that perform actions on the same object. You can use the With…
End With statement to make the code run more efficiently as you’re
referencing the object name just once.
The following piece of code sets various formatting options for the selected
range in Excel using the With...End With statement.
Sub UsingWith()
' Formats the selected range in the active worksheet.
With
Selection.Font
.Name = "Calibri"
.Size = 20
.Bold = True
.Italic = True
.Underline = True
End With
End Sub
Chapter 11. Derived Data Types in VBA
Here, we introduce derived data types in VBA, namely enumeration, and
custom. The custom data types represent a vital data structure which
permits one to deal with complex systems that reduce and simplify the
code. Enumerated data types are not as complicated as custom data types.
Instead, they have a simple data structure which creates codes that are
readable.
VBA Custom Data Types
Having a single name, these data types are a collection of related elements
of different types. An example is an application that stores and displays
customer information found in a database. The database has related
information such as age, address, identification number, and name. You
may declare five different variables for each of these particular variables,
but that would eventually prove to be cumbersome. In fact, the program
would end up being longer and less efficient, not to mention difficult to read
and difficult to write. Type and End type statements are used to define
custom data types. You can see these below:
To access individual elements of the custom data type, we use the dot (.)
operator. This has been illustrated below:
Other things that one can perform with custom data type include defining
elements as arrays, declaring variable arrays, and passing elements of
variables to procedures.
Enumerated Types
Enumerated types have several elements, and this data type originates from
an integer data type. In this data type, every integer is assigned an identifier.
The names of the enumerated types are constants. A constant will allow one
to use symbols instead of numbers. This improves the readability of the
program.
To define enumerated data types, it is a must to have its elements arranged
between Enum and End Enum statements. Below is an example to
demonstrate the definition of enumerated data types:
Within the Enum statement, all elements of an enumerated data type are
assigned a constant value. The elements can have both positive and
negative integers. When there is no exact initialization done, VBA
automatically gives the first element the value 0, the other element the value
1, and so forth.
To declare the variables of an enumerated type, we have to use its name. We
can assign them any integer value; however, there will be no point of
having an enumerated type if we allocate the variable enumerate constant.
Chapter 12. Beyond the Basics
Microsoft’s Excel VBA is a very much like an iceberg. No matter how basic
the function is, you will inevitably come across a more complex form of it
shortly. So, when it comes to creating your own macros, there is much more
involved than what can readily meet the eye.
When building a macro, there are quite a few things you must keep in mind.
By following some of these basic rules, you can truly simplify your venture
into the macro world but at the same time, leave yourself open to learn new
things.
Rule #1: Keep it simple
It is one thing to write code in practice where you are free to make mistakes
and find the time to fix them. But when you are learning something new, it
is extremely important that you do a trial run of any macro you make before
you decide to launch it live. To that end make sure that your practice macro
is simple. So if you need to write code that will allow you to automate the
updates of a hundred worksheets your practice macro should be to update
one. If you need a macro to automate a hundred cells, then limit your
practice macro to update five.
By approaching your code in this way, it will be easier for you to catch
errors and modify it whenever needed. Once you have mastered the code
and it is working as you expect, you can always expand it to match your
immediate needs. Here is a sample of the order of getting a macro live.
1. Create your new workbook
2. Add some sample data
3. Build your new macro
4. Do a test run
5. Fix any errors
6. Create a copy
7. Add your code to your active workbook
8. Retest the macro with the new data
9. Fix any errors
10. Create a backup copy
11. Test your macro
12. Fix any errors
13. Go live
This may seem like a lot of repetitive busy work, but these rules are very
practical. Errors are a very real part of a programmers life, and you should
expect to have more than your share of them. It is much better though, to
have an error in a practice session than in a live code that could
compromise the data you have.
These steps are relatively easy to do and the more successful you are at it,
the more confidence you will have when you are creating macros in real
life. Never be afraid of making a mistake. Even if you can’t find the error
having a backup copy makes it possible for you to always go back to a safe
place and start again without risk of losing data.
Keep reading: There is a lot to know about using Excel VBA, and you will
likely remember those codes, syntaxes, functions, loops variables, arrays,
etc. that you use frequently, however, by making sure that you keep abreast
of what can be done with VBA you will be able to stay ahead of the game.
Keep a glossary of terms around until you can remember them all by heart.
There are many that you will have to rely on as you continue to grow in
your learning of VBA.
Rule #2: Always leave comments in your code. This will serve as a
reminder so that when you look at it (perhaps in a few months), you know
what your intended purpose was.
Rule#3: Name your variables in a way that they will mean something to
you. This practice serves not just as a memory device, but it’s also
protection. It will keep you from mistakenly applying the wrong code to a
particular variable.
Rule #4: Whenever you see a code like
“MyWorkbooks.Worksheets(“Sheet1”), remember that it can be substituted
with the actual name of the worksheet itself.
Rule #5: Whenever you are working with large amounts of data first, copy
the values of the cells to an array. Then you will be able to manipulate
them, which will be a much faster process than trying to address each cell
one at a time.
Rule #6: When you write a new macro always create a copy of the
workbook first. This way, you reduce the risk of accidentally deleting the
data.
Rule #7: For new macros, make sure you divide the task up into smaller
bite-sized pieces. Then, tackle each of them one at a time. Get the first one
working smoothly and then move onto another one. Test each phase of the
code for errors before you begin working on another piece of the puzzle.
Rule #8: Before starting a new project, turn on the OPTION EXPLICIT
button at the top of the module. When on, you are required to declare a
variable before you start work. To do this go to your menu bar and select
“Tool”, the “Options”, and then check the box for “Require Variable
Declaration”.
Rule #9: When creating a new macro start by setting up a few sample
products before you apply your new code with actual data. You can learn
many different details about writing code in VBA, but just like learning a
new language, the only way you can cement them in your mind is to create
and run macros in much the same way you would practice reading, writing,
and speaking in your new tongue. Whether you choose to create your
sample data for yourself or you want to test your new code with smaller
projects, it is a necessary part of life with VBA to test the waters first. By
doing this, your skills in VBA will improve much faster than if you just try
to master it by studying alone.
These are just a few basic guidelines you need to keep in mind when
learning VBA. While some of these rules seem pretty obvious, it is easy to
forget them when you are overwhelmed with work. No matter what your
level of skill is when it comes to VBA there is always room to learn more.
By following these basic guidelines, you are less likely to get sidetracked
by common errors that could zap your enthusiasm for writing code. You
will be able to stay focused and completely tuned to everything you are
learning and as a result, your ability to write code will be a more enjoyable
experience in the long run.
Chapter 13. The Cheat Sheet for Excel
Formulas
You can use Excel to prepare reports, perform calculations, create text-
based files, and much more. Many people have used this program to
accomplish great things. In this cheat sheet, you'll find the formulas that
you need to complete your tasks with Excel.
The Most Popular Functions
Some functions have certain requirements. Most functions, however, apply
to any situation. Here are the functions that you will likely use in Excel
2016:
IF – This function tests a condition. It will give you a value depending on
the result of the test.
INT – This function deletes the decimal part of numeric values. Thus, you
will turn your numbers into integers.
NOW – Use this function to get the current time and date values of your
computer.
SUM – With this function, you can get the total of a set of values.
ROUND – This function can round numbers to specific digit positions or
decimal places.
COUNT – Use this function to count the cells that hold numbers.
SUMIF – This is the combination of SUM and IF. It will give you the total
of the values that satisfy your predetermined conditions.
TODAY – This function will give you the current date value of your
system.
AVERAGE – With this function, you can get the average of a set of values.
COUNTIF – This function is the combination of IF and COUNT. It counts
cells that meet your criteria.
The Text Functions that You Should Use
Excel 2016 has functions that work on text-based data (e.g. names, phone
numbers, email addresses). The list given below will describe how text
functions work:
LEN – Use this function to count the characters inside your chosen
cell/range.
MID – This function can extract characters. It allows you to specify the
number of characters to be extracted and the starting point of the process.
LEFT – This function extracts characters starting from the first character of
the text.
RIGHT – With this function, you can extract characters starting from the
final character of the text.
UPPER – Use this function to convert letters to uppercase.
LOWER – Use this function to convert letters to lowercase.
REPLACE – This function replaces text with another text.
PROPER – With this function, you can capitalize the first character of each
word.
CONCATENATE – Use this function to combine two or more text strings.
The Operation Precedence in Excel 2016
Mathematical operations follow a particular order, and the Excel program
follows that order. The list you'll find below describes the order that Excel
and math operations follow:
Operations inside parentheses
Operations that involve exponents
Multiplication
Division
Addition
Subtraction
The Cell References in Excel 2016
You can use relative or absolute cell references in your Excel formulas.
When you are copying a formula to other cells, the things that you'll need to
modify depend on the kind of reference you are using. The formula may
adjust its references automatically so that it matches the new cell (i.e. for
relative references). It's also possible that the reference will point to the
same cell regardless of the formula's new location (i.e. for absolute
references).
Excel lets you combine absolute and relative references in your formulas.
Thus, you can make formulas that have fixed rows and adjustable columns,
or fixed columns and adjustable rows.
In Excel 2016, you can set a reference as absolute by placing a dollar
symbol. Here are some examples:
D1 – This reference is completely relative.
D$1 – This reference has a relative row and absolute column.
$D1 – This reference has an absolute row and relative column.
$D$1 – This reference is completely absolute.
The Error Messages in Excel 2016
Excel is capable of displaying error messages. Thus, you'll immediately
know whenever you make a mistake. Error messages usually appear
whenever you createa circular reference or a formula that has incorrect
elements. Knowing what an error value means is the first step in solving
formula-related problems.
Here are the error values that you'll encounter while using Excel 2016:
#N/A – One of your functions or formulas cannot find your referenced
information.
#NULL! - This value states that you used a space in a multi-range
reference. You should use commas to separate ranges.
#REF! - You used an invalid reference.
#DIV/0 – You are attempting to divide a value by zero.
#VALUE – You assigned the wrong kind of argument to your function or
formula.
#NAME? - Excel doesn't recognize the text or name inside your formula.
#NUM! - You used an invalid number in your formula.
Chapter 14. Some other tips on VBA
programming
Other than the tools mentioned earlier and some tips mentioned along the
way earlier, below are some other tips on VBA programming for
beginners.
Writing more efficient VBA code
Working with arrays instead of a range of cells
Let us compare the execution times between these two approaches. Suppose
you want to count the number of numbers that are divisible by four in a
range of 50,000 cells. The code that works with an array is generally faster
than the one switches between VBA and Excel. The following table shows
the execution times (in seconds) for two different approaches in counting
the number of numbers that are divisible by four in a range of 50,000 cells:
Working with a range of cells Working with an array
0.20 0.031
The listing below is an example of VBA code that works with a range of
cells.
Sub WorkingWithRange()
'To count the number of numbers that are
'divisible by 4 in a range of 50,000 cells.
Dim r As Long, c As Long
Dim rN As Long, cN As Long
Dim cnt As Long
Dim StartTimer As Date, EndTimer As Date
StartTimer = Timer
rN = 500: cN = 100: cnt = 0
For r = 1 To rN
For c = 1 To cN
If Sheets(1).Cells(r, c) Mod 4 = 0 _
Then cnt = cnt + 1
Next c
Next r
EndTimer = Timer
Debug.Print cnt
Debug.Print EndTimer - StartTimer
End Sub
Another listing is an example of much faster VBA code that works with an
array.
Sub WorkingWithAnArray()
'To count the number of numbers that are
'divisible by 4 in a range of 50,000 cells.
Dim x() As Variant
Dim r As Long, c As Long
Dim rN As Long, cN As Long
Dim cnt As Long
Dim StartTimer As Date, EndTimer As Date
StartTimer = Timer
rN = 500: cN = 100: cnt = 0
ReDim x(rN, cN)
Sheets(1).Activate
x = Range("A1").Resize(rN, cN)
For r = 1 To rN
For c = 1 To cN
If x(r, c) Mod 4 = 0 Then _
cnt = cnt + 1
Next c
Next r
EndTimer = Timer
Debug.Print cnt
Debug.Print EndTimer - StartTimer
End Sub
Disabling screen updating, alert displays, events, and automatic calculations
If they are not necessary during the execution of your VBA code, disable
them before the execution and restore them to their initial settings right
before the execution ends. Restoring the settings rather than turning them
all on is a good practice since some users may have different settings, as
shown in the following listing.
'Declaration
Dim ScrnU As Boolean, DispA As Boolean, _
Evnt As Boolean, Calc As Long
'Save the settings
With Application
ScrnU = .ScreenUpdating
DispA = .DisplayAlerts
Evnt = .EnableEvents
Calc = .Calculation
End With
'Disable them before the execution begins
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Step 2: The 1st argument (The lookup value) is our “anchor value”, the value
that we have in both tables, the ID NUMBER. So select the corresponding
cell. In this case is first the cell with an ID number in our search tool, A3
=VLOOKUP(A3,
Why? Because Excel will try to find in the database whatever we have in A3
(in this case, excel will try to find in the database the number 14)
Step 3: Select the Lookup Array (The database) in order to indicate excel
where to search. Remember to add ABSOLUTE REFERENCES in order to be
able to drag the formula eventually.
=VLOOKUP(A3,$F$2:$J$89,
Why? Because the database starts in F2 and ends in J89.
SUPER IMPORTANT NOTE:
IN ORDER FOR VLOOKUP TO WORK CORRECTLY, THE FIRST
COLUMN IN THE DATABASE MUST HAVE YOUR LOOKUP VALUE
(ANCHOR VALUE). IN OTHER WORDS, IF YOU ARE USING THE ID
NUMBER AS YOUR LOOKUP VALUE, THE ID NUMBER MUST BE IN
THE FIRST COLUMN (LEFT COLUMN) OF YOUR DATABASE.
Step 3: Write the column number where your desired result is. In this example,
your desired result is the name of the hero, so, IN YOUR DATABASE the
name of the hero is in the 2nd Column, then you need to write 2.
=VLOOKUP(A3,$F$2:$J$89,2
Step 3: Write 0, in order to get and EXACT search. Close and drag the
formula.
=VLOOKUP(A3,$F$2:$J$89,2,0)
ID HEROE
14 Optimus Prime
2 Catwoman
56 Fantastic Four
70 Rocky Balboa
80 Gandalf
That’s it! Look how fast you got the names of the heroes using their IDs, and if
you change the ID number the name will change too! Try it.
ID HEROE
20 Zorro
30 Doc Savage
Luke
40 Skywalker
Now try by yourself to fill the search tool completely!
ID HEROE CITY BOSS
14 Optimus Prime
22 Catwoman
56 Fantastic Four
70 Rocky Balboa
80 Gandalf
Chapter 16. Department Data Validation
We want to allow a user to enter a department number and have Excel
automatically populate the related department name. The result is illustrated
in Figure 14 below.
PRACTICE
Figure 14
Summary
If we think about the goal for a minute, we can piece together the Excel
items needed. Here we’ll discuss the features and functions required at a
summary level, and in the Step-by-Step unit below we’ll chronologically
work through the detailed steps.
We allow the user to enter a department number into the input cell and then
use the VLOOKUP function to retrieve the proper department name.
Since we are asking Excel to perform a lookup, we’ll need to store the
department list somewhere inside the workbook. Since we want the user’s
experience to be clean, we place the department list on a separate
worksheet. Since we may ultimately add more departments over time, we
choose to store the department list in a table named tbl_depts.
Since we want to be clear about which cell requires input, we identify the
input cell with the Input cell style.
Since our lookup formula is designed to return the name for the
departments appearing in our list, it would return an error if the user entered
a department number that wasn’t in the list. To prevent this error, we
provide a drop-down to restrict input to a list of valid department numbers
with the data validation feature.
Since data validation does not directly support structured table references,
we must first set up the custom name dd_depts, which refers to the
structured table reference for the department number column.
Here is a summary of the independent Excel items mentioned above:
A table stores the department list.
The named reference enables the drop-down list to use the table data.
The Input cell style highlights the input cell.
The data validation feature provides the in-cell drop-down.
The VLOOKUP function retrieves the department name.
Good times! Now that we have worked through the overall concepts, let’s
sort through the specifics.
Step-by-Step
Here are the steps to setting up the worksheet.
DEPENDS ON
Step 1: Table. Since we use the workbook on a recurring basis and want to
make it easy to add new departments, we convert the ordinary range of
departments found on the Exercise 6 Data sheet to a table named tbl_depts.
NOTE
My personal preference is to start table names with tbl_.
Step 2: Named range. We need to set up a name that refers to the table’s
department number column so that we can use it with the data validation
feature. We assign the name dd_depts to the department number column of
the table tbl_depts[Dept Num].
NOTE
My personal preference is to start names that are used to provide a drop-
down list with dd_.
Step 3: Input cell style. We want to make it easy for the user to identify
which cell requires input, so we apply the Input cell style to the input cell.
Step 4: Data validation. We create an in-cell drop-down in the input cell by
setting up data validation to allow a list equal to our name dd_depts.
Step 5: Lookup formula. After the user picks a valid department number
from the drop-down list in C13, we ask Excel to display the corresponding
department name with the following lookup function:
=VLOOKUP(C13,tbl_depts,2,0)
Where:
C13 is the value we are looking up, the department number.
tbl_depts is the lookup range, the table of departments.
2 is the column that has the value we wish to return, the department name
column.
0 means we are not doing a range lookup.
Wow, wasn’t that a rush? Using five independent Excel features and
functions in harmony to achieve a goal—so fun!
This specific combination of features and functions is quite handy. Once
comfortable coordinating the individual steps, you’ll quickly be able to use
this approach in your workbooks.
BALANCE SHEET LOOKUP
In this exercise we’ll build a financial statement from an export from our
accounting system. We’ll set up the workbook so that each period when we
paste in updated data, Excel automatically recalculates the financial
statement amounts. Each period, we just paste and go; that’s hands-free
reporting.
PRACTICE
To work along, please reference the Exercise 7 worksheet.
Let’s pretend that periodically we obtain an extract from our accounting
system that reflects each account’s current balance. For simplicity, we’ll
refer to our export as the trial balance. Let’s also pretend we want Excel to
automatically place the values found in the trial balance into a perfectly
formatted financial statement.
The overall idea is simple: create one worksheet in the workbook dedicated
to storing the trial balance, and then set up the financial statement using
lookup formulas that pull the right values into the right cells in the report.
Since the report values are generated by formulas, they will automatically
refresh as soon as an updated trial balance is pasted into the workbook.
You’ve already written many VLOOKUP functions, so this should be pretty
easy. I did add one little twist, which demonstrates how to use column-only
references with a lookup formula.
NOTE
How can we ensure that all data in the trial balance flows properly through
to the financial statement? With an ErrorCk sheet, of course.
XREF
Take a look at the Exercise 7 Data sheet to observe the exported data. These
are the values we’ll ultimately pull into the Exercise 7 sheet, which
represents the balance sheet. We’ll use the VLOOKUP function to retrieve
the values.
We want to write our balance sheet formulas so that they include any new
rows that may appear in the trial balance over time. My preferred method is
to store the trial balance in a table. Since this isn’t always an available
option or is sometimes not appropriate, let’s explore an alternative method:
column-only references.
A column-only reference, such as B:B, includes the entire column. If the
following month’s data occupies more rows than last month, using a
column-only reference as an argument ensures the function will include all
data.
XREF
Column-only references are introduced earlier. This exercise illustrates a
practical application of them.
The lookup formula used to pull the values from the trial balance into the
balance sheet follows:
=VLOOKUP(B14,‘Exercise 7 Data’!B:C,2,0)
Where:
B14 is the value we are looking up, the account name.
‘Exercise 7 Data’!B:C is the lookup range, the exported data on the
Exercise 7 Data sheet; note the column-only reference B:C.
2 is the column that contains the value we want to return, the amount.
0 means we are not doing a range lookup.
KB
Here are the steps to writing the formula using only your keyboard. Type
=VL, and then hit Tab to insert the VLOOKUP function from the auto-
complete list into the cell. Left Arrow twice to insert the B14 cell reference
into the first function argument. Type a comma to finish the first argument.
Use Ctrl+PageDown to switch to the Exercise 7 Data sheet. Use the arrow
keys to navigate to any cell in column B. Hit Ctrl+Space to select the entire
column. Hold down Shift and then press the Right Arrow to extend your
selection to include column C as well. Type a comma to finish the second
argument. Type a 2 as the third argument, and then a comma. Type 0, close
the parenthesis, and then press Enter.
The results are shown in the partial balance sheet in Figure 15 below.
Figure 15
XREF
Please note that this exercise worksheet relies on the SUBTOTAL function
for adding and utilizes skinny rows.
Since the report was built with formulas, when we paste in the updated trial
balance the following month, the balance sheet amounts refresh
automagically!
TRICK QUESTIONS
Okay, time for a quick intermission between our hands-on exercises. I’d
like to ask you some trick questions about the VLOOKUP function.
Question #1: Where does it look?
When the VLOOKUP function is trying to find the matching value in the
lookup range (as specified by the second argument), where within that
range is it actually looking? I’ll give you two choices: it looks in all cells of
the lookup range, or it looks only in the first column. What do you think?
Answer: The VLOOKUP function looks for a matching value in the first
column of the lookup range—that is, the left-most column within the range
defined by the second argument. After a match is found, the VLOOKUP
then uses the other columns in the range to return its result. But the original
match is performed within the first column only.
Question #2: Does the column number specified in the third argument refer
to the absolute worksheet column or the relative column within the lookup
range?
The third argument of the VLOOKUP function indicates which column has
the value we want to return. This argument is specified as a column index
number, such as 5, 7, or 9. So the question is whether the column index
number refers to the absolute worksheet column—such as column B = 2
and column E = 5—or does it refer to the relative column within the lookup
range, as defined in the second argument? What do you think?
Answer: The column number refers to the relative column within the
lookup range. So if the amount column is the second column within the
lookup range, but happens to be in column E, we would use a value of 2.
Question #3: What happens if a matching value can’t be found?
If the function tries to find a matching value, but a matching value can’t be
found in the first column of the lookup range, what happens? I’ll give you
two choices: Excel returns an error code, or Excel returns the closest match.
What do you think?
Answer: It depends on the value of the fourth argument. If the fourth
argument is FALSE, then the function returns the error code #N/A when a
matching value is not found. If the fourth argument is TRUE, then it returns
the closest match based on the function’s range lookup logic.
The range lookup logic tells the function to search row by row, one at a
time, in order, until it finds a row that contains a value that is less than the
lookup value and where the following row is greater than the lookup value.
At this point, the function stops looking and returns the related value.
According to the Excel help file, “The following largest value that is less
than lookup_value is returned.” This logic is, admittedly, very confusing. It
reminds me of the show The Price is Right, where the contestants need to
guess the highest price without going over. It is easier to make sense of this
logic when applied in an actual worksheet, so we’ll do some additional
exercises shortly that will help.
Chapter 17. The Object Model
1: Classes and Objects
Just as with problem solving, there are various paradigms that can be used
to help think about programming in an organized way. Up until the early
1990’s, the most popular approach was top-down stepwise refinement. In
this method, we start by thinking about the task we want our program to do.
That task is broken down into subtasks, and those are broken down in turn,
until finally we have a task small enough to be programmed easily. These
small pieces can be individually tested and assembled into ever larger
chunks until finally you have a finished piece of software that can
accomplish the original task.
This approach works pretty well but it has one significant flaw. The small
procedures that are developed tend to be quite specialized for whatever the
context of the problem is, and not readily adaptable for use in other
programs. For each piece of software, you have to start over from the
beginning, even if you’ve written programs that do very similar tasks.
The newer model that has been widely adopted and is currently popular is
almost the polar opposite of the top-down approach. In this method, called
object-oriented programming, you start by thinking about the “objects”
your program needs to manipulate. They could be things like elements of
the user interface, files, worksheets, or anything else that is part of the
universe of items available to your program. Each category of objects has
its own characteristics: properties that you might want the program to
change, sub-objects that it can contain, actions that it can take (or that can
be carried out on it), and events you would like it respond to. A category of
objects is called a class. Essentially, you begin your work by determining
the classes of objects you’ll use, and either writing code for them to carry
out all the needed operations, or, if you’re lucky, using a class of objects
that someone else has already developed.
This approach leads to robust, reusable code, in the form of a library of
classes. Most programs use the same kinds of objects, and so a lot of effort
can be put into developing a class with the knowledge that the work will
pay off in the future on other projects. Having a nice library of well-written
classes means that programming is much easier and can avoid a lot of the
low-level detail involved in things like changing a font or a color. In VBA,
there is a huge universe of classes to choose from. All the required elements
are provided and you just need to write the code that manipulates them.
The distinction between a class and an object is a bit subtle. A class of
objects is like a framework or pattern that describes the objects that belong
to the class. An object is an instance of its class. A real-world example
would be the class of chairs. There is a general concept of chairs, which we
could compare to a class. And then there are actual chairs, each of which is
an instance of the class.
Classes and objects form a hierarchy. In VBA, the Application is at the
topmost level of the hierarchy; if you’re working in Excel, the Application
is Excel itself. (There are versions of VBA embedded in many other
Microsoft products, with classes designed for their individual applications,
though there is often quite a bit of overlap.) Excel itself contains other
objects, most prominently Workbooks. Workbooks contain Worksheets,
Charts, and other kinds of things; Worksheets contain Ranges. (A Cell is a
special kind of Range.) Often an object can contain not just one but a whole
collection of sub-objects of a given type. So Workbooks can contain many
Worksheets and Charts; a Worksheet can contain many Ranges.
To appeal to a real-world analogy, think of the Application as being like a
house. A house can include many kinds of objects such as a roof, a front
porch, a back yard, etc., but mainly we would focus on rooms. A room can
contain lots of kinds of things, one of which is chairs. A dining room
probably has many chairs. It may have a cabinet that contains dishes. So
there is a hierarchy of a house and its contents. Each class of objects has its
own properties and things that it’s used for and possibly other objects it
contains. You can’t push an analogy like this too far, but I hope it gives you
a feeling for what the hierarchy is like.
Now, let’s look at a specific example. In Excel, we have a ListBox class.
Every actual ListBox is an object in this class. It has properties such as Left
(horizontal position of upper left corner), Height, Visible, and others. It has
methods, such as AddItem and Clear. And it has events for which there are
built-in functions.
Figure 8 ‑ 1 The Object Browser (under the View menu)
To get a look at all the methods, properties, and events for the class
ListBox, we can use the object browser. This is a feature of VBA that lets
you explore the object hierarchy and find out what you can do with various
classes of objects. The figure shows the object browser featuring a view of
ListBox. The object browser is under the View menu.
Note the small icons distinguishing properties, methods, and events. We’ve
highlighted the Locked property; it’s a property with a Boolean value that
can be set in code or in the Properties window. A note at the bottom tells us
that ListBox is a member of class MSForms; listboxes appear in many
applications, not just Excel.
Figure 38: The Chicken Egg table with a VLOOKUP formula that has been
mislaid
The lookup value is B2, which shifted one cell to the right from when my
formula was in cell D2. My table array also shifted one cell to the right –
from A2:B3 to B2:C3. All of these references are relative.
Let’s see what happens when we change one reference to be absolute.
First, delete the contents of cells D2 and E2. Then type the following
into D2:
=VLOOKUP($A2,A2:B3,1,0)
Now copy and paste that into cell E2. The result is Chicken, but why?
Look at where the lookup value is pointing:
Figure 39: With an absolute column reference, the lookup value is now
correct
The dollar sign ( $ ) makes the reference to column A absolute, so when we
copy and paste the formula into cell E2, the lookup value is still pointing to
Chicken in cell A2.
What happens when this spans over several values, and how do we ensure it
is set up correctly? We will explore another example to answer those
questions.
Rock, Paper, Scissors
To see this happen with more detail, go to the Rock Paper Scissors
worksheet.
Once again, we will try to copy the contents of cells A1 through D4 and
paste them into cells G2 through I4, using VLOOKUP.
In cell B2, type:
=VLOOKUP(A2,F1:I4,2,0)
Now select and copy this cell, then paste it into cell C2. Does everything
look like it should?
When we select cell B2, we see in the formula that:
Ref1 is our lookup value
The table at the right is our table array
The column index is 2
The range is 0
Figure 41: The Rock Paper Scissors worksheet with a misaligned formula in
cell C2
So, what went wrong? We told Excel that all the cell references are relative,
but some of them need to be absolute.
We will continue to look at how information shifts when using relative
references, then we will address how to solve it.
Select and copy cell B2, then select cells B2 through D4 and paste –
simultaneously filling all nine cells with the formula. What did you get?
Do cells B2 through D4 match what is in cells G2 through I4? If they do
match, then you did not enter the preceding formula precisely. The cell
references will shift depending on the cell and, therefore, will result in
different values. The following illustrations demonstrate this.
Cell B2 contains our original formula:
=VLOOKUP(A2,F1:I4,2,0)
Figure 43: The formula pasted one cell to the right has misplaced arguments
In cell D2, the cells in the formula are shifted one more cell to the right:
=VLOOKUP(C2,H1:K4,2,0)
Figure 44: In the adjacent cell to the right, the arguments have shifted again
If we look at the cells beneath B2, we will see a similar behavior.
Once again, for reference, this is cell B2 with our original formula:
=VLOOKUP(A2,F1:I4,2,0)
Figure 45: Our original formula
In cell B3, the columns remain constant in the formula, but the row
numbers shift:
=VLOOKUP(A3,F2:I5,2,0)
Figure 46: The formula pasted in the row below has arguments that are also
down one row
In cell B4, we see the row numbers have shifted again:
=VLOOKUP(A4,F3:I6,2,0)
Figure 47: The formula pasted two rows down has arguments that are also
shifted down two rows
One important point evident here is that the inconsistent formula does not
affect the value outcome until cell C3! This is critical to understand
because, sometimes, if you do not check your formula, you will think you
are copying and pasting correctly – you may even spot-check a few cells
that look right without even realizing the flaw.
I speak from very personal experience on this. It is unpleasant to work hard
on a report that fails to provide the information expected. It is also not good
for building credibility and trust with superiors and colleagues on the
quality of your work.
Introducing the Dollar Sign
When designating a value as absolute, a dollar sign ( $ ) is used. When the
dollar sign is omitted, the reference will shift as the formula is copied and
pasted into other cells. This segment will help you understand when and
why to use the $, and soon you will understand the flexibility it provides.
As stated earlier, using a dollar sign before referencing a cell’s column or
row will make that reference absolute (or unchanged). The $ symbol
informs Excel that we do not want the reference shifting as we paste the
formula into different cells. By adding a $, that reference will not change.
Note that a column, a row, or both column and row can be referenced as
either relative or absolute. That means that in one reference, we can keep a
row the same (absolute), while the column changes (relative) as we copy
and paste from cell to cell.
Now, in cell B2, type the following:
=VLOOKUP($A2,$F$1:$I$4,B$5,0)
Do you see what’s happening there?
In the lookup value argument, A is absolute, while 2 is relative
In the table array argument, both F and 1, as well as I and 4, are absolute
In the column index argument, B is relative, while 5 is absolute
When we copy and paste the formula from cell B2 into cells B2
through D4, the values will match G2 through I4. So, how did we do it?
First, we kept the lookup value’s column A absolute so it always refers back
to column A on any row or column.
Figure 49: An absolute column and relative cell lookup value maintains an
effective formula wherever it’s placed
Both the columns and rows of the table array are absolute, so they will
remain constant throughout.
Finally, the column index uses a relative column and an absolute row.
Cell B2 has B$5 as the column index. When this is copied, its row will stay
the same but the column will shift, as shown in the preceding illustration.
In row 5, cells B through D, we have a footer reference. In a VLOOKUP
formula, when we use a literal number as a column index, that value stays
the same. But when we use a relative reference, the number will shift as the
formula is copied to other columns. By utilizing a footer with a reference as
the column index, we won’t have to manually adjust this argument in every
cell we paste the formula.
Note that while our lookup value has an absolute column reference and a
relative row, the column index has the converse – a relative column and an
absolute row.
This was a lot to tackle, and we are going to look at a familiar example for
another approach at getting our heads wrapped around absolute and relative
references.
Multiplication Table
Consider this old multiplication table from elementary school, available in
the worksheet named Multiplication Tables.
Figure 50: The multiplication table with the working area bordered in black
We will attempt to duplicate cells B2 through L12 using VLOOKUP. This
will help better explain how the relative and absolute references are
interacting as we traverse from cell to cell.
We will use the first column of our multiplication table as the lookup value,
the entire table as the table array, and the first row as the column index.
If we enter the following into cell N2, it should display the value 4:
=VLOOKUP(A2,A1:L12,B1,0)
When we copy the formula in N2, select N2 through X12, and then paste,
we can see where our formula went wrong. Cell O2 should have a 6, but it
shows an 8. This is the result of a relative lookup value that has shifted to
cell B2 (with a value of 4).
Figure 51: With a relative lookup value, the formula goes awry in a new
location
If we adjust the formula in cell N2 to make the lookup value absolute…
=VLOOKUP($A2,A1:L12,B1,0)
…we can once again copy N2 and paste the formula over cells N2
through X12. And, once again, we see errors in the cells; however, they are
different from the errors shown earlier.
If we check the formula in cell O2, we now see 4 instead of 6. Now, the
table array is still relative, so our lookup value is 2. But the table array has
shifted to start in column B, where the number 2 appears in row 1.
Then, based on the column index, Excel moves over three cells to the right
and finds the value 4. The logic only gets messier as we look at the formula
in other cells.
Figure 52: The problem is not corrected by merely adding an absolute
column to the lookup value
So, we will need to rewrite the formula in N2 to make sure both the
columns and rows are absolute, to keep the table array constant regardless
of where it is referenced:
=VLOOKUP($A2,$A$1:$L$12,B1,0)
Again, copy cell N2 and paste over the earlier values in cells N2
through X12.
Figure 53: Making the columns and rows of the table array absolute doesn’t
entirely fix the problem, either
Row 1 of the table looks correct, and if you need to confirm, remember
your twos multiplication table.
But row 2 of the table does not make any sense, and based on the reference
errors, we can see we need to revise this formula further. Check the formula
in cell N3 and you will notice that our column index is not pointing to
row 1. We need to adjust the formula to make the row of the column index
absolute.
Notice that we do not want to make the column absolute, or we’ll see
column B repeated eleven times in N2 through X12. The following formula
is correct:
=VLOOKUP($A2,$A$1:$L$12,B$1,0)
This formula should replicate the multiplication table from 2 through 12.
Chapter 19. Project – Registration Form
The goal of this project is create a registration form, to insert and store
data’s as name, address and phone. There are thousands of ways to create
this form, on this project, will need to create a form that interacts with
Excel worksheet. The form will read, insert and delete data’s from
worksheet.
The following picture shows the form model of this project.
'To loop through all the jpg files in the folder and populate
'the ImageFilenames array with the filenames of the jpg files
Do While Filename <> ""
ImageCount = ImageCount + 1
ReDim Preserve ImageFilenames(1 To ImageCount)
ImageFilenames(ImageCount) = Filename
Filename = Dir
Loop
'Dir() returns a zero-length string ("")
'when no more file in the folder matches
'the pathname, "C:\Photos\*.jpg"
End Sub
'Callback for gallery1 getItemCount
Sub getGalleryItemCount(control As IRibbonControl, ByRef Count)
'To specify the number of times to call
'the getGalleryItemImage procedure
Count = ImageCount
End Sub
'Callback for gallery1 getItemImage
Sub getGalleryItemImage(control As IRibbonControl, _
index As Integer, ByRef Image)
'Each time this procedure is called, index is increased by one
Set Image = LoadPicture("C:\Photos\" & ImageFilenames(index + 1))
End Sub
'Callback for gallery1 onAction
Sub SelectedPhoto(control As IRibbonControl, _
id As String, index As Integer)
MsgBox "You selected Photo " & index + 1
End Sub
Private Sub PrepareItemLabels()
'To build an array of item labels for the drop-dow n
Dim i As Long
ItemLabels(0) = "All Groups"
ItemLabels(1) = "Group 1"
ItemLabels(2) = "Group 2"
ItemLabels(3) = "Group 3"
ItemLabels(4) = "Groups 1 and 2"
ItemLabels(5) = "Groups 1 and 3"
ItemLabels(6) = "Groups 2 and 3"
End Sub
'Callback for dropDown1 getItemCount
Sub getDropDownItemCount(control As IRibbonControl, ByRef Count)
'To specify the total number of items in the drop-down control
Count = 7
End Sub
'Callback for dropDown1 getItemLabel
Sub getDropDownItemLabel(control As IRibbonControl, _
index As Integer, ByRef ItemLabel)
'Set the item labels in the drop-down control
ItemLabel = ItemLabels(index)
'Alternatively, if the item labels are in stored
'the range A1:A7 on Sheet1, use the following code:
'ItemLabel = Worksheets("Sheet1").Cells(index + 1, 1).Value
End Sub
'Callback for dropDown1 onAction
Sub SelectedItem(control As IRibbonControl, _
id As String, index As Integer)
'Determine which group(s) to be visibl e
VisGrpNm1 = "": VisGrpNm2 = ""
Select Case index
Case 0
VisGrpNm1 = "*"
Case 1
VisGrpNm1 = "*1"
Case 2
VisGrpNm1 = "*2"
'Change the appearance of Sheet2 if 3rd item is selected
Call ChangeSheet2Appearance
Case 3
VisGrpNm1 = "*3"
Case 4
VisGrpNm1 = "*1"
VisGrpNm2 = "*2"
Case 5
VisGrpNm1 = "*1"
VisGrpNm2 = "*3"
Case 6
VisGrpNm1 = "*2"
VisGrpNm2 = "*3"
End Select
'Invalidate Group1, Group2, and Group3
'Once invalidated, getVisibleGrp is executed
myRibbon.InvalidateControl "Group1"
myRibbon.InvalidateControl "Group2"
myRibbon.InvalidateControl "Group3"
'Update the status ba r
Application.StatusBar = " Module: " & ItemLabels(index)
End Sub
'Callback for Group1 getVisible
Sub getVisibleGrp(control As IRibbonControl, ByRef Enabled)
'Hide and unhide certain groups of 1, 2, and 3
'based on the selected item from the drop-down control
If control.id Like VisGrpNm1 Or control.id Like VisGrpNm2 Then
Enabled = True 'Visible
Else
Enabled = False 'Hidden
End If
End Sub