KevinD Unit2FinalDraft
KevinD Unit2FinalDraft
Kevin Doten
7 Roberts Road
Enfield, CT 06082
28 May 2019
I am a current co-op working in the US Pricing department of John Hancock, and I am writing to offer
you my introductory guide to learning VBA in Microsoft Excel. Through my own experience, it has become
apparent to me that John Hancock does not currently have any tools to assist new co-op students who have
never used VBA before.
When I first started my co-op, I was immediately given a difficult task within VBA despite rarely using
the program in the past. This forced me to spend my first few days researching through Google and asking
seemingly endless questions to my superiors before making any progress. If I was formally introduced to VBA
in the beginning, I would have been productive sooner in my term and experienced less confusion. Additionally,
having common questions answered within this guide will limit the amount of simple questions being directed
at more experienced employees, which would allow everyone to focus on their own work. However, I have
noticed other co-ops and their managers having a different experience. It is not uncommon for a co-op’s
manager to not have work when their co-op first starts, which leads to the co-op feeling bored and the manager
feeling guilty. This guide could serve as an assignment that co-ops are given at the beginning of their term, so
managers do not have to stress over getting them an assignment immediately.
I hope that after reviewing this document, you will decide to include this guide as a training tool for all
future co-ops. As time moves on, and new co-ops develop skills within VBA, they should be encouraged to add
to and improve this document so that in the future, it only becomes more useful. This process of revision over
the years will ensure that only necessary information is included, and confusion is limited. However, I
recommend giving the co-ops a PDF version of this document at first, so the format of some images does not
change, but to keep a Word document that they can edit.
I am available at the email address [email protected], and will make sure to respond as soon as
possible. Thank you for taking the time to consider my request. I look forward to hearing from you in the future.
Sincerely yours,
Kevin Doten
Kevin Doten
Sections:
Click on any title below to be sent to the corresponding section of this document.
3. Commenting
5. Ranges
6. Loops
7. If Statements
8. Debugging
Page 2
• Right click on any of the sheets at the bottom of your workbook and
select “View Code” as shown to the right.
Method 2:
• Click “Developer” at the top of your screen and click on the ‘Visual Basic’
icon on the far left as shown below.
o This is not always typically a default setting, so you may have to add
it by selecting File > Options > Customize Ribbon > Click the box
next to “Developer” in the right-hand column under “Main
Tabs” Figure 1
Figure 2
• Stop your code by pressing the blue “Stop Button” or press “Esc” on your keyboard
Figure 3
Tip! On the left of your screen you should see two panels similar to the image below. If you do not see
either of these then click the “Project Explorer” and “Properties Window” as shown in the blue
square on Figure 3.
Page 3
• This top panel shows all of the information about files that you have
open.
▪ Workbooks are the highest level of structure and represent
each file you have open.
▪ Objects are the sheets that you have in the current
workbook.
▪ Modules are where you will write the majority of your
code.
Figure 4
Commenting:
• Comments are written within code to explain what each section does so that when someone else has to
read or use your code, they aren’t completely lost.
• It’s common to see comments at the beginning of a script explaining its overall intention and then other
sections to explain methodology, or where data is being pulled from.
o For example, it may be clear to you what your integer “j” is doing, but the next person to use
your workbook may not realize as quickly.
• To add a comment, use an apostrophe at the beginning of what you want to write, and all of your text
will appear green until you start a new line.
o Anything that is commented will not be read when the code is running so it won’t affect your
output.
Tip! Use comments when your debugging to remove lines temporarily. By “commenting-out” a line or
section, you can change how your code works for a test without having to rewrite what you had before
running the test.
Page 4
1. Worksheets: The sheets within the workbook that you write your code
2. Strings: This is how you define any text (non-number) you need used in your code
3. Integers: These are always whole numbers.
a. Note: If you define a decimal as an integer it will round.
4. Variants: These are any numbers that are not integers.
Sub The_Basics()
• Define your variable by using
the format Dim
'This code is intended to
(Name_of_Variable) As
demonstrate how to write a basic
(Variable_Type)
script after defining your variables.
• Think of “Dim” as the way VBA
says “define
Dim output As Worksheet
• Defining your variables should
Dim row As Integer always be the first section of your
Dim column As Integer code
Dim words As String
Ranges:
• Ranges are a collection of cells that can be affected at the same time rather than working cell by
cell. You’ll commonly use ranges when working with large tables while cells are used more
often for exact, specific changes.
Sub Using_Ranges()
• Just like cells, there are two
'This code is very similar to the first different ways to write ranges.
code, and this is purposeful. • Each format has strengths and
‘In general, it is very useful to weakness for the same reasons as
reference old, similar code and cells
repurpose it for whatever you need to • Ranges more clearly show how
accomplish. the first method can take more
time to write.
Dim output As Worksheet
Dim row As Integer
Dim columnstart As Integer
Dim columnend As Integer
Dim words As String
'output.Range("A2:E2") = words
End Sub
Page 6
Loops:
• One of the main uses of coding is to make a very tedious and boring task into a short script
through the use of loops.
• Loops are functions that cycle through a section of code based on pre-set conditions
o The two types, (1) For-loops and (2) Do-loops can both be shown in the same script by
creating a multiplication table.
For row = 1 To 12
output.Cells(row, column) = row *
column
Next row • For-loops run for as many numbers as you
list
column = column + 1 • When the loop begins, the original
variable is the first in the list, and after
each “Next,” it becomes the next number
Loop While column <= 12 'Until in the list
column > 12 • You can also stop this loop based on
certain conditions by including an “Exit
End Sub For”
Tip! Basic operations work the same as normal formulas in Excel. You can use [+, -, /, *] to perform basic
operations. Most things that can be done in Excel formulas can also be done in VBA, but often will not
have the exact same nomenclature. If there’s ever a function you wish you could do in VBA just ask a
coworker or Google it! Chances are someone else has wanted to do the same thing and will have an
example.
Page 7
If Statements:
• If Statements are functions that can take an expression, and based on the outcome, produce one
of two results.
o So, if the statement you write is true then outcome A will occur, and if not, B will.
o You can combine two or more if statements to create 'nested ifs,' which can create even
more branching paths.
o To accomplish this in VBA, you'll need to use 'ElseIf' as shown below
Before you run this code, read through it and see if you can determine what the outcome will be!
Sub If_Statements()
End Sub
Page 8
Debugging:
• Debugging is an inevitable part of coding and often the most important. If you’re new to coding, this
may seem like a daunting task, but here’s a few helpful ways to get started!