0% found this document useful (0 votes)
46 views9 pages

KevinD Unit2FinalDraft

The document is a letter from Kevin Doten to Daniel Taylor proposing an introductory guide to VBA for new co-op students at John Hancock. Kevin explains that when he started his co-op, he struggled without prior VBA experience and had to spend time researching. He suggests creating a standardized VBA training guide for new co-ops to make them more productive earlier. The guide would also reduce questions directed at experienced employees. Kevin hopes Daniel will approve the guide to benefit all future co-ops.

Uploaded by

Kevin Doten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views9 pages

KevinD Unit2FinalDraft

The document is a letter from Kevin Doten to Daniel Taylor proposing an introductory guide to VBA for new co-op students at John Hancock. Kevin explains that when he started his co-op, he struggled without prior VBA experience and had to spend time researching. He suggests creating a standardized VBA training guide for new co-ops to make them more productive earlier. The guide would also reduce questions directed at experienced employees. Kevin hopes Daniel will approve the guide to benefit all future co-ops.

Uploaded by

Kevin Doten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introducing New Co-ops to VBA

Kevin Doten
7 Roberts Road
Enfield, CT 06082

28 May 2019

Mr. Daniel Taylor


Actuarial Associate, Co-op Committee Chair
John Hancock
200 Berkeley Street
Boston, MA 02116

Dear Mr. Taylor:

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

Enc. [How to Code in VBA]


Page 1

How to Code in VBA


This file was written using Microsoft Excel on Windows 10. While the general coding information is still applicable, the format of the
application may change depending on the version or operating system.

Sections:
Click on any title below to be sent to the corresponding section of this document.

1. Two Ways to Access VBA

2. How to Navigate VBA

3. Commenting

4. How to Start Your Code

5. Ranges

6. Loops

7. If Statements

8. Debugging
Page 2

Two Ways to Access VBA:


Method 1:

• 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

How to Navigate the VBA Interface:


• Run your code by pressing the “Play Button” or press “F5” on your keyboard
o You can also run your code line by line by pressing “F9”

• 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.

• This lower section will display more detailed information about


whatever you have selected in the first panel.
• The red box highlights where you can change the names of each
module.

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

How to Start Your Code:


• Your code needs to start with "Sub" followed by a name and two parentheses, and then ends when you
write “End Sub” as shown in the example below.
• The name should be relevant to what you’re doing, to make it easier for you or anyone else to find if it’s
within a worksheet that has many subs.
• Once these essentials are in place your first section should be where you define all of your variables.
There are many different forms of variables that you may encounter, but the most common are:

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

Set output = Sheets("Output") • There are many ways to


words = "It worked!" accomplish the same task such as
adding a string to A1 on Output
row = 1 • The first is more useful when the
column = 1 inputs are systematically
changing
output.Cells(row, column) = words • The second method is often
'output.Cells("A1") = words quicker to type, and easier to
accomplish becomes of its
relation to how cells are named in
End Sub Excel Sheets
Page 5

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

Set output = Sheets("Output")


words = "Hooray :D"
row = 2
columnstart = 1 Tip! If you want to use the first method but don’t want to
columnend = 5 count columns, switch to R1C1 format. This will
row = 1 show both columns and numbers as rows on Excel
column = 1 sheets. File > Options > Formulas > Working with
formulas > Check “R1C1 reference style”
output.Range(Cells(row, columnstart),
Cells(row, columnend)) = words

'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.

• Do-loops repeat the code contained


Sub Multiplication_Table() between a Do in the beginning and Loop
at the end.
Dim output As Worksheet • The condition that the loop relies on can
Dim row As Integer be created with a “While” or “Until”
Dim column As Integer
• Each of these are included after either
Loop or Do depending on your preference
Set output = Sheets("Output")
column = 1

Do 'While column <= 12 -OR- Until


column > 12

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()

Dim output As Worksheet


Dim row As Integer
Dim column As Integer
Dim Outcome1 As String
Dim Outcome2 As String
Dim Outcome3 As String
• When writing any code formatting is
incredibly important and the best way to
Set output = Sheets("Output")
show this is through if statements.
• Keeping your indentation consistent can
Outcome1 = "First statement True"
make your code significantly easier to
Outcome2 = "Second statement True"
read, and the proper format can help you
Outcome3 = "Both statements False"
when debugging.
row = 4 • Additionally, some types of format are
column = 1 actually required for your code to work.
For example, if there weren't any indents
If row / 4 = 2 Then after each "Then" or "Else" then the code
output.Cells(row, column) = would not work properly.
Outcome1
ElseIf row / 4 = 1 Then
output.Cells(row, column) =
Outcome2
Else
output.Cells(row, column) =
Outcome3

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!

1. Running Line by Line


• As mentioned earlier, pressing “F9” will allow you to run each line of your code
individually.
• This can show you exactly where an error occurs or what each individual line does.
2. Break Points
• Break points are set places that your code will stop at when running until you tell it to go
again.
• This can be useful if you know the first section of your code works but want to run it line
by line afterwards.
• To add a break point, click on the gray bar next to your code at the line that you want the
code to stop and remove it by clicking the red dot that takes its place
3. Google
• Google will often be your most useful resource thanks to the vast amounts of discussion
forums that will give you possible answers to your questions.
• The best way to get results is often to write the error code that you receive after running,
but you’ll get the hang of this quickly.
4. Coworkers
• This is likely the most important and most useful resource that you will have while
working at John Hancock or any other company.
• The people you work with may have more experience with coding than you, and even if
they don’t, they might have encountered the same issue.

You might also like