Sunteți pe pagina 1din 97

Table of Contents

Introduction:................................................................................................................................................. 5
Introduction to the layout of Excel: .......................................................................................................... 5
The Home Tab ....................................................................................................................................... 5
The Insert Tab ....................................................................................................................................... 8
The Data Tab ......................................................................................................................................... 9
Adding in the Developer Tab (if it is not already there): .................................................................... 11
Wrap up: ................................................................................................................................................. 12
Data Tab and Data Connections ................................................................................................................ 13
Bringing in Data from the Web ............................................................................................................... 13
Properties for a web data connection: ............................................................................................... 15
Bringing in Data from an Access Database ............................................................................................. 18
Charts .......................................................................................................................................................... 21
Creating the Chart:.................................................................................................................................. 21
Small extra step, big difference in appearance: ................................................................................. 23
Changing the Chart Type......................................................................................................................... 25
Changing the Data Shown in the Chart ................................................................................................... 26
Removing Data from a Chart and Choosing the Order Data is Shown In. .............................................. 27
Pivot Tables and Pivot Charts .................................................................................................................... 30
Filters................................................................................................................................................... 37
Pivot Charts ............................................................................................................................................. 38
Introduction to Formulas ........................................................................................................................... 40
Hints & Tricks .......................................................................................................................................... 41
Dragging Formulas down: ................................................................................................................... 41
The double tap: ................................................................................................................................... 42
Money $igns ............................................................................................................................................ 44
No Money Signs .................................................................................................................................. 44
Absolute References: .......................................................................................................................... 45
Relative References: ........................................................................................................................... 45
Basic Formulas ........................................................................................................................................ 46

Adding: ................................................................................................................................................ 46
Subtracting: ......................................................................................................................................... 47
Multiplying: ......................................................................................................................................... 47
Dividing: .............................................................................................................................................. 48
Count:.................................................................................................................................................. 48
COUNTA: ............................................................................................................................................. 48
MAX & MIN: ........................................................................................................................................ 49
Most Used Formulas .................................................................................................................................. 49
GETPIVOTDATA ................................................................................................................................... 49
CHOOSE ............................................................................................................................................... 50
LOOKUP ............................................................................................................................................... 50
MATCH ................................................................................................................................................ 51
LEFT, RIGHT, MID ................................................................................................................................ 51
LEN ...................................................................................................................................................... 52
TRIM .................................................................................................................................................... 53
AND ..................................................................................................................................................... 53
OR ........................................................................................................................................................ 54
ROUND ................................................................................................................................................ 54
CONCATENATE .................................................................................................................................... 54
& .......................................................................................................................................................... 54
VLOOKUP............................................................................................................................................. 56
Time Formulas ............................................................................................................................................ 58
TODAY ................................................................................................................................................. 58
NOW .................................................................................................................................................... 58
YEAR/MONTH/DAY ............................................................................................................................. 58
DATE .................................................................................................................................................... 59
WEEKDAY ............................................................................................................................................ 60
WEEKNUM .......................................................................................................................................... 60
EOMONTH ........................................................................................................................................... 60
NETWORKDAYS ................................................................................................................................... 61
WORKDAY ........................................................................................................................................... 61
Conditional Formulas .................................................................................................................................. 61

IF Statement ........................................................................................................................................ 61
Nested IF Statements .......................................................................................................................... 62
IF Statement with AND Formula ......................................................................................................... 64
IF Statement with OR Formula ............................................................................................................ 65
SUMIF .................................................................................................................................................. 65
COUNTIF .............................................................................................................................................. 66
SUMIFS ................................................................................................................................................ 66
COUNTIFS ............................................................................................................................................ 67
IFERROR............................................................................................................................................... 67
Examples of How Ive used Formulas ........................................................................................................ 68
WORKDAY, TODAY, and WEEKDAY to find out if the last day was Friday .......................................... 68
IF, OR, EOMONTH, WORKDAY, WEEKDAY to figuring the last workday of this month ...................... 69
EOMONTH, TODAY, WORKDAY to figure out the first workday of the next month ........................... 69
MONTH, YEAR, & to get just the month and year of a date ............................................................... 70
Adding time to a date ......................................................................................................................... 70
Converting a name from First Name Last Name to Last Name, First Name ............................... 70
COUNTIF and COUNTA to get the percent of times a certain text string appears in a list ................. 71
COUNTIF, COUNTA, Nested IF Statements, MAX and hiding cells to show the text that appears the
most on average from a list ................................................................................................................ 72
Dividing and Nested IF statements to divide by a certain number, given a certain criteria .............. 74
Combining Nested IF statements and the OR formula to count something only if all criteria are met
............................................................................................................................................................ 74
Introduction to Visual Basic for Applications (VBA) ................................................................................. 75
First some explanation:........................................................................................................................... 75
The Developer Tab .............................................................................................................................. 75
The VBA Editor ........................................................................................................................................ 77
The play, pause, and stop button ....................................................................................................... 77
Modules .............................................................................................................................................. 77
The Insert Menu .................................................................................................................................. 77
Debugging a Macro ............................................................................................................................. 79
The Structure of a Macro .................................................................................................................... 79
Creating Macros ...................................................................................................................................... 79

Comments in VBA ............................................................................................................................... 79


Inserting the button and creating the code for the button: ............................................................... 80
Running Macros With Buttons and Cascading Macros ....................................................................... 81
Copy & Paste Macro............................................................................................................................ 81
Clear Contents Macro ......................................................................................................................... 82
Message Box Macro ............................................................................................................................ 82
Change Cells Fill Color Macro & The Macro Recorder ........................................................................ 84
Clear Fill color of Cells ......................................................................................................................... 84
Insert Text Macro ................................................................................................................................ 85
Insert Formula Macro ......................................................................................................................... 85
Select Worksheet Macro..................................................................................................................... 86
Filtering Macro .................................................................................................................................... 86
Select Used Cells Macro ...................................................................................................................... 87
VBA If statements & VBA formulas ..................................................................................................... 88
If, Else If Macro ................................................................................................................................... 88
For Each Loop Macro .......................................................................................................................... 89
Create New Worksheet Macro ........................................................................................................... 91
Hide/Unhide Worksheet Macro & Error Handling in VBA .................................................................. 91
Error Handling in VBA ......................................................................................................................... 92
Delete Worksheet Macro .................................................................................................................... 93
Create a New Workbook Macro ......................................................................................................... 93
Open an Excel Workbook Macro ........................................................................................................ 94
The final result of the code should look like this: ............................................................................... 97
Conclusion .................................................................................................................................................. 97

Introduction:
Excel is a powerful tool of business. More powerful than I ever expected when I first started using it. In
this tutorial we will go over the layout for Excel 2007, data connections, charts, pivot tables, all sorts of
formulas, including the all-important VLOOKUP and IF statements, and I will give you an introduction to
Visual Basic For Applications (VBA) which is the code used to create macros in order to automate Excel.
After reading this tutorial, you will know Excel better than most people in your office. I will give you
descriptions, hints & tricks, screen shots, and examples for you to use on your way to becoming an Excel
master!
This tutorial comes with a companion Excel Workbook which I use and reference heavily. While
working with this tutorial it is recommended that you also have the companion Excel workbook open.
All the screen shots come from this workbook and everything I talk about in the tutorial is shown on
the workbook.

Introduction to the layout of Excel:


The Home Tab

The above screenshot is of the home tab of Excel. I pointed out some important features that I use all
the time. Ill go from left to right and explain what each of the arrows are pointing out.
1.
The section labeled Font is the formatting area. With this area you can use the buttons to
change the formatting of text including: changing the font, text size, bold the text, italize, underline,
change the color of the cell as well as the text, and add or take away borders from a cell.
2.
In the Alignment section the Merge and center button and Wrap Text button are
important features that are used all the time. Merge and center allows you to merge cells together
and it automatically centers the text that was in those cells (Note: it only keeps the text from the upper
left most cell, if there is text in multiple cells, so make sure that if you are merging cells only one of
them has text.)(See the example below). Wrap Text formats the cell size to show all the text that is in
a cell. For instance if you have a cell that contains a sentence that runs longer than the normal cell size
and you wrap the text it will automatically format it so the cell size adjusts to the amount of text in the
cell and shows all the text in the cell. (See the example below).
Before the merge

After the merge

Before wrapped text

Wrapped text

3.
The section labeled Number is where you format how the data in the cell is displayed. The
cells can take many formats, General is the default format. Below are all the formats for. You can also
use the custom format to change it to a way you want to see it. One way that I have used the custom
format is to change the date from 12/3/11 to Dec-11.

(Text is also a format that can be used)

4.
In the Styles section the conditional formatting button. This is a very important feature that
will format a cell based on certain rules that you set. We will dive into this later in the tutorial.

5.
In the Editing section the Autosum button is an easy way to sum up the values of the
highlighted cells. You can also click the down arrow right next to it and get the average, count,
max (largest number in the set), and min (smallest number in the set).

6.
In the Editing section the Sort and Filter button is an easy way to add filters to your cells that
allow you to then filter different columns to find the data you are looking for. It can also sort columns in
different orders including smallest to largest or alphabetical.

The Insert Tab

1.
In the Tables section is the Pivot tables button. Pivot tables are often used in business. They
make it easy to view large data sets in an easy to use and easy to manipulate table. We will dive into
how to create and use pivot tables later in this tutorial.
2.
In the Illustrations section is the Shapes button. Shapes are used a lot in Excel sheets. There
are a lot shapes available in Excel. As you can see in almost all of my screen shots in this tutorial I use the
arrow shape.
3.
The Charts section is where you create many different types of charts. Well deep dive into
charts and chart creation later on in the tutorial.

The Data Tab

1.
The Get External Data section allows you to manage connections to data sources such as
websites, access, and other sources. When you set up a connection with this section the data will be
brought into Excel.
2.
The Connections section is where you refresh any and all data connections to any source. Data
connections do not automatically refresh when the source data is changed. In order to have the most up
to date data showing in your graphs, pivot tables, or data connections you will need to click the
refresh button. If you want to refresh every connection in your workbook then you need to click the
down arrow on the refresh and click the refresh all drop down and click the Refresh All link in the
drop down menu.

3.
In the Sort & Filter section the Sort button allows you to sort the data in your worksheet. Its
the same function that we discussed on the home page. They just made this a redundant feature so
people could easily access it. The added functionality here is if you click the sort button you can use
the sort function on multiple columns at the same time.
4.
The Filter button also found in the Sort & Filter is where you can add filters to the columns
in your worksheet. Its the same function that we discussed on the home page. They just made this a
redundant feature so people could easily access it. The added functionality here is the advanced filter.
With this you can filter the data, filter and copy to another location, use criteria to filter the data, and
filter for unique records only.

(The screen that pops up when you click the advanced filter)

5.
Text to columns, which is found in the Data Tools section is an awesome feature that allows
you to separate text that is in one column into multiple columns based on a certain criteria including
tab, semicolon, comma, space, other. This criteria is what Excel uses to split the data up. For example if
there is a name in cell A1, lets say Bob Brown, and you want to split it into Bob in cell A1 and Brown in
cell B1
(step 2 of 3 of converting text to colums)

Often when you are dealing with multiple databases that both contain names, the names will be
formatted in different ways. If you need to compare these lists in Excel it can be a pain to manually type
each name up so they are in the same format. An example of this is if in one database the persons
name is formatted as Barack(first column) Obama (second column) and another it is Obama, Barack in
one column. You can use text to columns to separate the Obama Barack one using the comma. This
will separate it into two different columns at the comma and instead of Obama, Barack you will have

Obama (first column), Barack(second column) and then you can just drag the last name to the column
next to the first name and easily compare the two lists.
Before
After

6.
Remove duplicates is another great feature. If you have a column that has multiple rows of
data and some entries are the same you can remove the duplicates and get just the unique values you
are looking for with this feature. All you have to do is highlight the column, click remove duplicates,
select the columns you want Excel to analyze for duplicates, and click OK.
Before
After

Adding in the Developer Tab (if it is not already there):

The above screen shot shows you the developer tab. If it is not there then follow the below directions. If
it is there then you can skip this section.
The Developer tab is where you control macros, ActiveX controllers, the VBA editor, and the macro
recorder. Dont worry if you dont know what any of that means right now, well go into later on in the
tutorial. This tab is not shown by default in Excel so we need to add it on.
Click on the Microsoft button in the top left (where you go to save the document). At the bottom of the
drop down menu is a button called Excel Options.

Once you get into the Excel options go to the Popular section. In there it is the third checkbox down. It
says Show Developer Tab in Ribbon. Click this check box and the developer tab will be shown.

Wrap up:
This is the end of the first part of the tutorial going over the layout and important buttons within Excel.
Excel has a lot more features that can be used, in this tutorial I wanted to go over the parts of Excel that
I have seen to be the most useful. I encourage you to explore the other parts of Excel.

Data Tab and Data Connections


With any job in business youre going to be dealing with a lot of data. Good data is the key to a good
business. Getting the correct data in easy to read formats is important for any executive to make the
right decision. The start to all of this, is the data tab in Excel.
Excel is great for data analysis, but often you need to get the actual data from different sources
throughout your organization. Excel can directly connect to tables on websites, Access Databases, Text
documents, SQL connections, and other connections.

Bringing in Data from the Web


One of the most used sources for data is the web. You can automatically get data from any table on any
website as long as Excel can have a direct connection to it (Excel cannot get directly to password
connected sites).
I will run through an example of how to get stock data from http://finance.yahoo.com/ Im going to
look up stock information for Google Inc.

Go to the Data tab and in the Get External Data section all the way to the left click on the From
Web button.

After you click this button it will open up a specially formatted web browser, built specifically for the
purpose of bring in data from websites into Excel. It will look like the screen shot below.

Notice the little yellow arrows all around the page that opens up. This shows the tables that Excel has
identified on the website. You want to click these arrows in order to import this data into Excel.
Once you click one of the yellow arrows it will turn green and highlight the data that will be imported
into Excel. You can click as many of the tables as you want. I am only going to bring in one of the data
tables.

Once you are done selecting the data you would like to import you click the import button. This will set
up a data connection to this table that can be updated as frequently as you would like (Ill show you how
to setup data refresh in a little bit!).

Once you set up the connection the wizard will ask you where you would like this data to go. Before you
do anything lets take a look at the Properties button.

Properties for a web data connection:

1.
This is where you decide how often you want the data from this connection to refresh. You
should always have the Enable Background Refresh button checked because this allows Excel to run a
data refresh in the background, i.e. without you explicitly telling it to refresh each time you need it to be
done.
a.
You can set a data refresh at a time interval. Just click that checkbox and set the time interval at
which you want the data to be refreshed in minutes. The default for this is every 60 minutes.
b.
You can set the data to refresh every time the workbook is opened. I use this feature often.
Once you select this it will also ask you if you want to remove the external data, i.e. the data that you
brought into Excel from another source, before you close the workbook. I have rarely used this feature.
2.
This is where you control the formatting of the data that is brought in. There can be issues when
bringing in data from certain sources. You can control whether Excel automatically adjusts column size,
headers, row numbers, sorts, etc.
3.
This is where you tell Excel how to handle new data when it is refreshed. You can insert new
cells for new data and delete unused cells. This is the selection that I have always kept it on because it
fits my requirements. You can also insert entire rows for new data and overwrite existing data.

Once you have decided on the properties you want your data connection to have, click OK. Once you do
this your data connection is established, it will refresh whenever you told it to, and you will have the
data you were looking for, easily accessible right in your Excel workbook.

Bringing in Data from an Access Database


Access is used as a small database system by a lot of businesses. It makes it easy for the people of the
business to store data in one centralized area, gather data from multiple people using forms in Access,
and easy filtering of data. Taking data that is stored in an Access database and manipulating it in Excel
for reports is a common practice in a lot of organizations. Fortunately, since they are both Microsoft
products they are easily integrated with each other.
The database we are pulling from in Access looks like this:

First step is going to the Data tab and clicking the From Access Button which is located on the left
side on the Get External Data part of the Data tab.

This will bring up a menu to select the database for which you are trying to bring in the data from.

After you select the database that youd like to have data come from, the next step is to select which
table in that database youd like your data to come from.

The next step is a menu that is asking how you would like this data to be displayed in excel. You can
have it in: A table, a PivotTable Report, or a PivotChart and PivotTable Report.
A table appears in Excel exactly as it does in Access. A PivotTable Report and PivotChart appear as a
normal Pivot Table and Pivot Chart, which well go over later.

After you hit ok the data will be pulled in from Access in the form that you have chosen. I chose to have
it imported in as a table.

This is the final result:

Charts
Charts are a huge part of Excel. Dealing with data in Excel can be tedious and involve looking at a lot of
numbers. Data visualization using charts in Excel makes this process a lot easier for everyone involved.
Visualizing large datasets makes it easy for you or your colleagues to see the data and trends. This is the
data that will be needed for you or them to make the correct, data-based, decision. Lets take a look at
how to create charts in Excel and how to make them look good.

You can access charts by going to the insert tab and the 3rd section is labeled charts. Below are some
examples of how to make charts. I used the most widely used charts for my examples: Column, Line, Pie,
and Bar.
For the examples I will use made up sales data over the year for a sales department.

Creating the Chart:


For this example I will use a column chart. The process is the same for all of the charts. No matter what
type of chart you create you can change the layout and style the same way. I personally use the same
style and the same layout no matter what type of chart. Well go over which style and layout I use a little
bit later in this part of the tutorial.

Highlight all the data. Select the column chart from the chart area. Below is the output when you select
the column chart:

Excel automatically grabs the data it believes that you are trying to display. Most of the time Excel is
correct, but there are some times where you will have to make some changes.
As you can see this graph does not look the best. Lets format it and dress it up a bit.

First thing we are going to do is stretch it out so all the columns can easily be viewed. On the
screen shot above the arrows are pointing to 3 little dots on the border of the chart. These are the
points at which you can stretch the graph. If you grab from the right or left side you can stretch out to
the right or left. If you grab the top or bottom you can stretch out up or down. If you grab any of the
corners you can stretch the chart out diagonally.

Next lets choose the chart layout. First click on the graph and the Chart Tools section should
show up to the right of your last tab up top in the menus. In Chart Tools click on the Design tab. In
the Design tab is the Chart Layouts section all the way to the left. This is the section where you can
choose the layout of your chart that you would like. I like layout number 3 and it is pretty much the only
layout that I use. That being said Excel has many layouts and they are all useful. More than likely you will
want to change the chart layout from the default as it is definitely not the best way to present your data.

This is the result:

Small extra step, big difference in appearance:


Many people dont take this extra step, but this makes the columns in the graph look a lot better.
Adding chart styles to your charts makes it a lot better. My favorite style is style 26 which embosses the
columns in the graph.
To get to this style click the down arrow in the Chart Styles menu.

The result:

One last step that can help a lot when you are analyzing data, having the columns can help you identify
trends, but it can cause problems when you are trying to see the actual data amounts being reflected by
the columns.
Adding data labels to the columns solves this problem. To do so simply right click over each of the
columns where you want to add data labels to.

The final result shows all the data, helps you identify trends, and its easy on the eyes. The final result of
your work will look like this:

Changing the Chart Type


Lets say you set up your column chart and you made it look perfect, but youre not sure whether this is
the right graph type to properly display your data. Excel 2007 and later makes this easy to do.

On the left side of the Design tabs menu is a button to change the chart type. When you click this
button it will bring up the menu that allows you to choose what type of graph you would like to change
it to.

Lets use the same data we used for a column chart, but lets change it from a column chart to a Pie
chart.

The above chart shows the same exact data that was in our column chart, but instead it is showing the
data in a pie chart. The type of graph you want to show depends on the type of data you want to show.
Sometimes a bar graph is better than a line graph. With just a few clicks of the mouse you can compare
the same data in different graph types and make the best possible selection for your chart.

Changing the Data Shown in the Chart


Not only can you change the chart type, you can also change the data thats shown in the chart. Using
the same data lets change the pie chart we just created from showing data by month to showing data
by sales person.
This can easily be done using the Switch Row/Column button in the design tab.

With the graph that we would like to change highlighted (Just click on the graph) click this button and it
will change the data from being shown by month to being shown by sales person.
Original Graph:

After the Change (with just one click of a button!):

Removing Data from a Chart and Choosing the Order Data is Shown In.
Sometimes when you insert a graph Excel grabs more data than you intended it to, or a person leaves a
company and you dont need to see their data in the graph anymore, but you need to keep it for
records. Excel makes it easy to remove data from the graph.
Lets say Mark leaves the company and you dont need to show his data anymore.
On the design tab click the Select Data button

On the screen that pops up when you click the select data field. From here you can add, remove, and
edit the data that is shown in the graph. We no longer need to see Marks data so lets remove him
from the graph.

To Remove Mark click the edit button over the Horizontal Axis Labels

It will then show you the data that is currently in the graph. Simply remove Mark from the data that is
being pulled into the graph. Change the Axis range from E3:E6 to E3:E5

This will only remove the horizontal axis label. To remove the data click the edit button above the
Legend Entries and select the range you want to be shown in the graph. For our example youll have to
do this for each month.
Your final result should look like this:

You can also change the order in which data is shown. For that well go back to a column chart. If you
want to see the current month first instead of January, use the arrows to move the data to the position
you would like it to be in.

If the current month is December this is what your graph would look like.

Pivot Tables and Pivot Charts


Pivot tables are a powerful feature of Excel. Pivot Tables are a feature that allows tables of data to be
rearranged in many ways for different views of the same data.
When you download raw data from a database it will usually have way more information than what you
are looking for.
For example well create a pivot table on a lot of stock data that I got from Yahoo! Finance. (To do this I
just simply downloaded data for random stocks into a CSV file directly from Yahoo! Finance. This data is
shown on the companion Excel Workbook on the Stock Data tab down the bottom.)
To start go to the insert tab and click on the Pivot Table button.

The Create PivotTable Screen will popup. From here you can tell Excel where to put the pivot table
and make sure that the selected range is the range you want it to be. Excel will start with a default
selection in the Table/Range field. This selection will either be all of the data on the selected sheet or
it will be the area that is currently highlighted on the current sheet. Either way it is easy to adjust to
show the exact data that you want to be shown.

Once you selected the data you want and the location you want for the pivot table, click OK. Excel will
then create the pivot table and the field list will show up that will allow you to drag and drop the fields
to the proper area.

On the screenshot above the arrow on the left is showing you the empty pivot table.
The bottom two arrows on the right are showing you the field list. The field lists are the columns of the
data and the name of the field list is the top cell of each column. To show the data in the fields you
simply drag the field you want from the top into the proper square down the bottom. Well go more into
more detail in a little bit.
First lets look at the PivotTable Tools menu that shows up when you create a pivot table. Note: When
you first create a pivot table it will automatically take you to this area. If you need to get back to this
section, just click on the pivot table and that tab will show up in the upper right.

Going from left to right:


1.
The PivotTable section is where you can change the PivotTable Name for your table. Naming
your table can help you stay organized, especially in sheets that have multiple pivot tables. This will be
PivotTable1 by default. The number will change for the number of pivot tables in the Excel Workbook.
2.
The Data section is where you can refresh the data and change the data source. If you have to
do a report on a daily basis youll often have data ranges that change. This is the section you use to show
the proper data in the pivot table. When you change the source data of a pivot table it does not
automatically update to show the proper data. Youll need to click this button which will refresh the
pivot table and show the correct data. Also, when you are changing data, it will often go outside the
range of the data that the pivot table is reading. To correct this just click the Change Data Source button
and change it to the correct range.
3.
The PivotChart button is how you create a pivot chart. Well go over pivot charts after we go
over pivot tables.
4.
The Show/Hid section is where you can choose to show or hide the Field List which is the
menu that should show up on the right where you decide which columns are shown in which parts of
the pivot table. The Field List is what the arrow is point to in the below screen shot.

I added a column called Trader to our stock data. Each stock (each row of data in our worksheet) has a
trader that is responsible for it. I am going to create a pivot table that shows how many stocks, that have
a P/E ratio (dont worry you dont need to know what a P/E ratio is. It is just a stock term) each trader is
responsible for.
The Final Result should look like this:

Lets see how I got there.


First, I decided which fields I wanted to show in the pivot table. My objective is to simply see the
number of stocks a given trader is responsible for. I only want to show stocks that have a P/E ratio
(meaning the cell under the P/E Ratio column is not blank or NA).
I dragged Trader to the Row Labels box on the bottom left of the field list, this will show me the
traders name in the Row Labels that are all the way to the left on the pivot table. I then dragged
description to the Values box on the bottom right of the field list. I did this because Excel will simply
count each non-empty cell in the range I defined when we were creating it. This field it does not
specifically matter which field I choose as long as it has NO blank cells in it. Each stock on our list has a
description therefore I could use this column to get an accurate count of the number of stocks. The last

step for this table was to drag the P/E to the Report Filter on the top left of the field list. I did this so it
would add a filter to the pivot table.

Side note: The values box can show you the count (what I am using), sum, average, max, min, product,
count numbers, standard deviation (stdDev), standard deviation of Row Label (stdDevp), Variance (Var),
Variance of Row Label (Varp). To do this click the down arrow on the field (that is what the arrow in the
above screen shot is pointing at). Once you do that, a drop down menu will pop up and the bottom
option will be Field Settings, click that. Another menu will show up which is where you can choose
how you want the pivot table to display your data.

I am trying to show the count of stocks that each trader has. So I am sticking with the default setting of
Count.
Once you moved everything into the proper area on the Field List (Im talking about the Row Labels,
Column Labels, Values, and Filters) your pivot table will be created and should look like this:

The whole point of a Pivot Table is to slice up large amounts of data into easy to use tables. Slicing the
data that you need is extremely easy with pivot tables. When a table is created the ability to filter the
data in the filters, row labels, and column labels youll notice arrows are put there automatically
and when you click on these arrows a menu shows up. On the below screen shot I clicked the arrow on
the Row Label to bring up the filtering options for the Row Labels

Going from top to bottom:


1.
With this section you can sort the data alphabetically. You can sort it from A to Z or Z to A.
2.
With this section you can add filters to either the Row Labels or the values that are shown in the
pivot table.
3.
With this section you can choose which rows are shown in the pivot table. If the checkbox is
selected it will be shown in the pivot table. If not it will not be shown in the pivot table.

Filters
Lets take a deeper look at the filters that can be applied to a pivot table.
Lets look at the Label Filters first. To get here I clicked the arrow on the Row Labels and on the menu
that shows up I clicked on the Label Filters.

You can filter your data in a lot of different ways using this menu as you can see in the screen shot
above. When you apply these filters they will be applied to the row labels in the pivot table.

Using the value filters you can filter the data that is in the values box. Using our example Im going to
use a value filter to show only the traders that are responsible for more than 15 stocks. (Obviously this is

easy to do in this pivot table without a filter, but its easy to imagine where this could be useful with
larger datasets).

I used the Greater Than filter to show only the traders that are responsible for more than 15 stocks.

Notice the filter is applied and the data is shown the way I wanted it to be shown.
One neat trick of the pivot table is that if you want to see the 20 stocks that John is responsible for,
just double click on the 20 and a new sheet will be created with the 20 stocks.

Pivot Charts
Pivot charts are a powerful way to show the data that is represented in the pivot table. A Pivot Chart
has all the qualities of a normal chart, but it is designed to allow you to read the data from the pivot
table and it shows only the data you want shown. One reason you would use a Pivot Chart rather than
a normal chart on a pivot table is because a Pivot Chart automatically excludes the Grand Total at
the bottom of a pivot table. A normal chart would not make this distinction.

Lets create a pivot chart on the pivot table we just made.


First click on the pivot table and then click on the Pivot Chart button on the options tab (this shows up
when the pivot table is clicked.

Once you click this button it takes you through the normal chart creation procedure which we went over
in the last lesson.
This is what my final chart looks like:

Introduction to Formulas
Excel 2007 has 343 built-in formulas. With these formulas you can figure out much of the information
and do a lot of data manipulation that you will need to do on a day to day basis. It would be a very long
tutorial if I went through and explained each formula. Instead, I picked some of the most popular Excel
formulas. I split the formulas up into different sections and tried to group them together by subject. The
sections are: Introduction, General Formulas (including the all powerful VLOOKUP), Time Formulas, IF
Statements, combining different formulas and examples of how I used formulas to do my multiple jobs
that involved Excel.
The first little trick I want to tell you about is how to find all of the formulas. You can go the Formulas
tab on the menu up top. This tab has all the formulas listed by functions and there are some other
functions on this tab that you can use for formulas.

An easier way of doing this though is just clicking on the fx in the formula bar up top. This will bring up
a popup where you can search for the formula or type in what you are trying to do and see if Excel has a
built in formula to help you accomplish your task.

Hints & Tricks


Dragging Formulas down: When you click on a cell you should notice that there is a little square in
the bottom right of the thick border outline that shows up (If you dont see a square trust me its at the
bottom right of the highlighted cell). If you click and hold down while over this square you can drag
whatever is in that cell to its left or right or up or down.

One cool way to use this is to get the months of the year without writing all of them out. I wrote Jan.
and Feb. in cells B2 and C2 respectively. Im going to click on the tiny square, hold down, and drag to the
right. Excel will then automatically fill in the rest of the months of the year for me.

The Result:

This can be used for dates, text, numbers, formulas, etc. It is a great time and headache saver. For
formulas it can help you prevent errors and from typing the same thing over and over again.
Note: For this example I wanted to drag out the months of the year. I manually typed in Jan. into cell
B2 and Feb. into cell C2. I then highlighted both cells B2 and C2 and while holding down the mouse on
the bottom right of cell C2 I dragged to the right. Since both cells were highlighted Excel knew that I
wanted to go in order. If I had only highlighted cell C2 and dragged to the right it would have filled in
Feb. for all of the cells.
The double tap:
Using that same mini square at the bottom right of the thick border on a highlighted cell, you can
double click that square and it will automatically fill in whatever is in that cell using the cells to the left of
it as a guide. (Note: this only works when you are trying to fill in data below the current cell. When
you double click on the bottom right it will only work in the downward direction. It will NOT work if
you are trying to go any other direction).
Im going to stick with the months for examples sake. This time I put the months vertically and I want to
put the month number next to the month.

Im going to double click on the square and it will fill in the month numbers as long as there is something
in the cell to the left of it.
(Hint: Notice I have both the 1 and the 2 highlighted before I double click or dragged using the square.
This tells Excel I want it to go in the normal order, so if its a date it will go to the next date as you drag
down, if its a number itll go to the next number, etc. If I had just highlighted the 1 and dragged down
it would have a 1 in every cell or if I did that for the example above it would have been all Jan.)

The Result:

Watch out though: The cells to the left are Excels guide when you double click on the square.
Think of it like the row you are double clicking the square on is a train and the cells to the left of it are
the rail road tracks. Wherever there is a space is where it will stop, regardless of if thats actually the last
used cell or not. In the screen shot below I took out the month of June so there is a space. I then double
clicked on the bottom right of cell C3 (the one next to Feb.). Excel then filled in the numbers until
there was a space after May which is where it stopped.

Money $igns
Note: The ranges used in the Money $igns section are for example only. They dont refer to anything
on the companion Excel Workbook.
Money signs inside formulas tell Excel whether you want to use a cell reference, otherwise known as a
range, as a relative reference or an absolute reference. Well go over both below.
This is how you would use $ with a range if you wanted an absolute reference, meaning the referenced
cells would not change as you dragged them: $E$2:$E$26
No Money Signs
If there are no money signs in a cell reference it will change the formula each time you drag the formula
right, left, up, or down. In the below screen show I put the formula =A2 in cell B2. I then dragged this
formula down and it changed each time. In cell B3 the formula changed to =A3. If I had dragged it over
to cell C3 the formula would change to =B3. It changes each time you drag it. This is a form of relative
reference.

Absolute References:
Absolute references means you do not want neither the letter nor the number in a cell reference to
change. To accomplish this we put a $ in front of both the letter and the number. This lets Excel know
that as we drag this formula or reference from one cell to the next we do not want this cell reference to
change at all.
In the below screen shot of an absolute reference I put it in so that it will always read cell D1. I then
dragged this formula down and it is still reading cell D1 because of the $ being in front of both the letter
and the number.

One area you always want to use an in is in the range part of a VLOOKUP (that is where the arrow is
pointing). =VLOOKUP(F11, $A$10:$B$33, 2, FALSE). A VLOOKUP is usually written in one cell and then
dragged down a list. If you drag the formula down you would have to change the formula for each cell.
When you drag the formula you need to make sure the reference range does not change as you drag it
down. This is where the money signs come in handy. (Dont worry if youre not fully wrapping your mind
around this yet, when we go over VLOOKUPs you will understand this a lot better.)
Relative References:
A relative reference means you want part of the cell reference to change as you drag the cell right,
left, up, or down.
If you have a $ sign in front of the letter in the cell reference it will always keep that column. In the
below screen shot I set it up so it will always read the column G but as I drag it down it will go up in
number. For instance in cell G2 I put the formula =$G1. If I drag this same formula down to cell $G3 it
will read =$G2.
In the screen shot you see that I dragged the formula over to cell H2. This is to show that when I drag
this formula right or left it will still reference column G because of the money sign in front of the letter.
So in cell H2 the formula reads =$G2 and in cell H3 you can see the formula reads =$G3.

If you have the money sign in front of the number only it will change the column as you drag it, but not
the row number.
In the below screen show I put the formula =A$8 in cell B9. I then dragged it to the right to show you
that the formula changes. In cell C9 the formula reads =B$8 and as you can see in cell D8 the formula
reads =C$8. I then dragged the formula down. In cell C9 the formula reads =B$8 and it cell C10 and C11
the formula also reads =B$8. The $ sign prevented the row number from changing in the cell reference
as we dragged it down.

Make sure you have a decent understanding of money signs before you move on.
Money signs are extremely important for formulas when you are dealing with formulas. If you dont use
money signs youll have to change the formula to make sure its referencing the right cells every time
you drag it.
Go to the companion Excel workbook to the Cell References tab and play around with money signs
and dragging formulas around.

Basic Formulas
IMPORTANT: FOR THE REMAINING SECTIONS WELL BE USING THE EXCEL WORKBOOK THAT CAME
WITH YOUR DOWNLOAD.
The first group of formulas I want to introduce are: the basic math formulas, counting cells, getting the
highest and lowest number out of a group.
Adding:
There are two ways to add. You can use the SUM formula or you can just reference two cells and put a
plus sign in between.
Formula

Syntax

=SUM(2,2)

=SUM(Value 1, Value 2, etc.)

=SUM(B2, B3)

=SUM(Range 1, Range 2, etc.)

=SUM(B2:B5)

=SUM(Range1:Range2)

=2+2

=Value 1 + Value 2 + etc.

=B2+B3

=Range 1 +Range 2 + etc.

All of the above formulas can be used to add either Ranges or Values together to get a sum. The first
one uses the SUM formula with actual values in the formula (2 plus 2 will be added together), the
second formula uses the SUM formula with Ranges, whatever is in cells B2 and B3 will be added
together.
The third formula uses a range of cells in the formula. Whatever is in cells B2, B3, B4, and B5 (the range
from B2 to B5 = B2:B5) will be added together. When you are using a range like this, you have to use the
SUM formula to get what the desired result.
You can also just use values and put a + sign in between them to get the result you are looking for. The
fourth formula in the table uses this method to add 2+2 together.
The fifth formula does this using ranges, so whatever is in cell B2 will be added to cell B3.
Subtracting:
Formula

Syntax

=2-2

=Value 1 - Value 2 - etc.

=B2-B3

=Range 1 -Range 2 - etc.

There is no actual formula for subtracting two numbers or two ranges. Instead all you have to worry
about is using the - sign (the dash) between two numbers or two ranges as shown above.
Multiplying:
Formula

Syntax

=PRODUCT(2,2)

=PRODUCT(Value 1, Value 2, etc.)

=PRODUCT(B2, B3)

=PRODUCT(Range 1, Range 2, etc.)

=PRODUCT(B2:B5)

=PRODUCT(Range1:Range2)

=2*2

=Value 1 * Value 2 * etc.

=B2*B3

=Range 1 * Range 2 * etc.

Multiplying is the same as adding in terms of how its done in Excel. The differences: You use the
PRODUCT formula instead of the SUM formula and you use * instead of +.
Dividing:
Formula

Syntax

=2/2

=Value 1/ Value 2/etc.

=B2/B3

=Range1/ Range 2 - etc.

Dividing is like subtracting in terms of how it is done in Excel. Just like with subtracting there is no
specific formula to divide numbers or ranges. The differences: You use the / instead of - .
Count:
The COUNT formula counts the number of cells in a range with a number in it.
In the Excel workbook that came with your download take a look at the COUNT formula on the
Introduction to Formulas tab.
Formula

Syntax

=COUNT(M7:M11)

=COUNT(Range 1:Range 2)

=COUNT(M7, M9, M11)

=COUNT(Range 1, Range 2, etc.)

The formula on our workbook is looking at the range between M7 and M11. In this range is the numbers
1 to 5. The results of the formula will show that the count of cells with numbers in it between M7 and
M11 is 5.
If you only want to count certain cells you can also use this formula. Instead of putting a : in between
two cells separate each cell you want to count with a comma, as in the screenshot above.
COUNTA:
The COUNTA formula counts the number of cells in a range that are not empty, regardless whether that
range contains text, formulas, or numbers.
Formula

Syntax

=COUNTA(P7:P11)

=COUNTA(Range 1:Range 2)

=COUNTA(P7, P9, P11)

=COUNTA(Range 1, Range 2, etc.)

Take a look at the formula on the Excel workbook. The COUNTA formula is looking at range P7 to P11. In
that range there are three cells that have numbers in them. The result of the formula is 3, the number of
non-empty cells in range P7 to P11.
Just like with the COUNT formula you can also just count individual cells, instead of a range of cells, by
putting a comma in between the cells that you would like to use the COUNTA formula on.
MAX & MIN:
The MAX formula shows you the largest number in a given range and the MIN formula shows you the
smallest formula in a given range.
Formula

Syntax

=MAX(A20:A24)

=MAX(Range 1:Range 2)

=MIN(D20:D24)

=MIN(Range 1:Range 2)

Take a look at the Excel Workbook. The MAX formula refers to the range between A20 and A24 which
has the numbers 1 through 5 in it. The largest number in this range is 5 and therefore the result of the
MAX formula is 5.
The MIN formula is looking at the range between D20 and D24 which has the numbers 1 through 5 in it.
The smallest number in this range is 1 and therefore the result of the MIN formula is 1.

Most Used Formulas


In this section I will go over some of the most used formulas from my experience.
Formulas covered: GETPIVOTDATA, CHOOSE, LOOKUP, MATCH, LEFT, RIGHT, MID, LEN, TRIM, AND, OR,
ROUND, CONCATENATE, &, VLOOKUP
GETPIVOTDATA
Formula: GETPIVOTDATA(Data_field,pivot_table,field,item)
The GETPIVOTDATA formula allows you to get data from a pivot table into a normal cell. The purpose of
this is so that no matter what happens in the pivot table (if a certain row moves or anything) it will
always grab the correct data each time the pivot table is refreshed.
You never have to hand write it. In the cell where you want the formula to go simply put the = sign and
then click on the pivot table data that you want to show in that cell. It does not matter if the pivot table

is on a different sheet from where you put the formula. Just click the tab that the pivot table is on and
then click the pivot table data you want to grab.

Take a look at the Formula Used in the screenshot above. This formula is grabbing data from the pivot
table on the Charts worksheet (tab down the bottom). It is grabbing only the data for the trader Steve.
No matter what happens in the future: if Steve moves up or down in the pivot table, if data is refreshed,
or anything happens to the pivot table data for the trader Steve, it will grab the correct data. This
simple fact is the reason you use this formula instead of just putting an = sign and the range that you are
trying to grab, when the data changes this formula changes with it.
CHOOSE
Formula: CHOOSE(index_num,value1,value2, etc)
The CHOOSE formula allows you to choose a value or an action based on an index number. In our
formula the index number I used is todays weekday number using the WEEKDAY(TODAY()) formula
combination. This formulas result will change each day, but for examples sake lets say today is a
Tuesday. The WEEKDAY(TODAY()) formula will return 3 and the CHOOSE formula matches this number
and chooses the right value based on this is number (where they match).
In other words the CHOOSE formula matches the index_num to the values you have listed out. You
can make the index_num dynamic, meaning itll change all the time, like we did using the
WEEKDAY(TODAY()) formula combination or you can just have it be a single number.
The CHOOSE formula is usually used in combination with other formulas where you could have different
outcomes. Usually the index_num is set to be the result of another formula and then they use the
CHOOSE formula to match it properly.

LOOKUP
Formula: LOOKUP(lookup_value,lookup_vector,result_vector | LOOKUP(lookup_value,array)
The LOOKUP formula allows you to lookup a value based on a reference value. Take a look at the Excel
workbook. If we put the name of any of the stocks from the Stock Data sheet into cell B14 it will show
you the one day return of that stock. The lookup_value is the reference point you are trying to find, in
our formula the lookup_value is the value of cell B16. The lookup_vector is the range of cells you
want it to look in to compare the lookup_value to, in our formula the lookup_vector is the list of
stock names in column A. The result_vector is the result you want the formula to show if it finds a

match of the lookup_value in the lookup_vector, in our formula the result_vector is the range of
cells that holds the one day return for each stock.

MATCH
Formula: MATCH(lookup_value,lookup_array,match_type)
The MATCH formula allows you to find the row and/or column number of a cell that matches the criteria
you lay out. In our workbook we use the match formula to find the row number of the stock we used in
the lookup formula. We are referencing cell B14 where you typed in the stock name. Whatever stock
name you give (thats on our stock data worksheet) the match formula will return its row number. You
can also use the MATCH formula to find the column number of something in a certain row. Simply tell
Excel what row you want it to look in and it will return the column number. In our formula we had it
look for P/E in row one on the stock data worksheet.

LEFT, RIGHT, MID


Formula: LEFT(text,num_chars) | RIGHT(text,num_chars) | MID(text,start_num,num_chars)
The LEFT, RIGHT, MID formulas all allow you to cut a text string short starting from the left, the right, or
the middle. On the workbook I used these formulas to shorten some of our stock names from the stock
data tab. As expected the LEFT formula starts at the left so you tell it what you want it to shorten and
how many characters, starting at the left, you want it to show. In the formula we shortened Accelrys
Inc. to Accelrys using the left formula and telling it to show the first 8 characters using the formula
=LEFT(B30,8).
In our formula we referenced cell B30 which held the text Accelrys Inc.. You dont have to reference a
cell we could have put Accelrys Inc. in the text part of the formula and gotten the same result. This is
the same for the RIGHT and MID formulas.
The RIGHT formula starts from the right. We used the RIGHT formula to shorten Information Systems
Associates to Associates by telling Excel to grab only the first 10 characters from the right using the
formula =RIGHT(B35, 10).

The MID formula is a little bit different in that you have to tell Excel what character to start out at and
then how many characters after that starting point you want to see from the left. In our workbook we
used the MID formula to show the middle of the text string Information Systems Associates to formation
Syste by telling it to start at character 3 and showing the next 15 characters. We did this using the
formula =MID(B40, 3, 15)
Note: spaces are considered characters so remember this when you are putting in your character
counts.

LEN
Formula: LEN(text)
The LEN formula gives you the character count of a text string. In our workbook we use the LEN formula
to get the character count of the data cell A40 which is the cell we used in the last section for the MID
formula. That cell should show formation Syste. Using the formula =LEN(A40) we find out that
formation Syste is 15 characters long. Remember spaces count as a character!
An interesting experiment is to combine this formula with the LEFT, MID, and RIGHT formulas:
Our goal is to get rid of the Inc. part, which is 5 characters long, including the space. We want to do it
in a dynamic way so that we could just drag it down a list, instead of changing the num_chars part of
the LEFT formula for each cell.
We use the formula: =LEFT(B30, LEN(B30)-5) to accomplish this.
Lets break it down:
This formula will give you Accelrys. The data in cell B30 is Accelrys Inc.. The LEN(B30)-5 part of the
formula we used gives us the character count of the data in cell B30 and subtracts 5 characters from it.

In our case for Accelrys Inc. the LEN formula gives us 13 subtract 5 and it gives us 8. The LEFT formula
will then grab the first 8 characters of the data in cell B30.
We could use this for the an entire list of companies and instead of manually removing it over hours we
can let a formula do the work for us in seconds.
(How could we use this on a list where some companies have inc. at the end and others dont. Hint:
Wait until we get to IF statements!!)

TRIM
Formula: TRIM(text)
The TRIM formula removes all spaces from a text string, except single spaces in between words. This
formula is useful because sometimes when you are downloading data from a database into Excel it can
add in unwanted spaces. Using the TRIM formula will get rid of these unwanted spaces.
Our example on the companion Excel workbook shows this. We put a sentence saying this is what trim
does. Notice this space at the beginning of a sentence. This tiny space can cause problems by messing
up other formulas that you have in your sheet so the TRIM formula is useful for that. We use the
formula =TRIM(B51) to get rid of the unwanted space. This is easier to do manually for one cell, but you
can imagine how great this formula can be for an entire database full of data with extra spaces all over
the place.

AND
Formula: AND(logic1, logic2, logic3, etc)
The AND formula checks to see if all arguments are true. If they are all true it returns TRUE, if they are
not all true it will return FALSE. It works like this if argument 1 is true AND argument 2 is true return
TRUE if either argument 1 or argument 2 are not true, then return false. I only used two arguments for
examples sake, but this formula can handle multiple arguments. I often use this formula in combination
with IF statements to test multiple arguments and return the correct value.

OR
Formula: OR(logic1,logic2,logic3, etc.)
The OR formula checks if any of the arguments in the formula are true. If any of them are true, it returns
TRUE, if none of them are true it returns FALSE. It works like this if argument 1 is true OR if
argument 2 is true return TRUE if they are not then return FALSE. I often use this formula in
combination with IF statements to test multiple arguments and return the correct value.

ROUND
Formula: ROUND(number, num_digits)
The ROUND formula allows you to round numbers to whatever decimal place youd like or to make sure
a number is a whole number. The number is the number you want to round, you can either add in the
number you want to round itself or a reference to a cell number. The num_digits part is the number of
digits you want it rounded to. If you want it be a whole number, like we did in the example formula on
the Excel workbook you put a 0, if you want it rounded to one decimal point you put a 1, etc.

CONCATENATE
Formula: CONCATENATE(text1,text2,text3,etc.)
The CONCATENATE formula allows you to join multiple text strings into one text string. On our
workbook we combine the value of two different cells into one cell. We combine what is in cell B79 and
B80. The formula combines the two cells to form one sentence in cell A79.

&
& is another way of combining the values of two different cells, just like the concatenate formula. I am
showing you both because I use both. The & is easier to use for simpler things like combining a first
name and a last name, the CONCATENATE formula is easier to use when you are combining multiple
cells to make a sentence or paragraph.

Instead of putting a concatenate formula before and then putting the two cells in the formula, you just
put cell reference & cell reference. On our workbook we used the & to grab just the month and the
year from a date. Instead of showing todays date it shows todays month and year in cell A85.

VLOOKUP
Formula: VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)
The VLOOKUP is one of the most used formulas in Excel across all sorts of different industries. The V in
VLOOKUP stands for vertical. It allows you to compare two lists. The formula looks for a value in the left
most column of a worksheet of data and if it finds that value it returns the value from the same row in a
column specified by you. This is a big one and an important formula so it gets its own tab on the
companion Excel workbook.
Lets take a look at the anatomy of a VLOOKUP:
The lookup_value is the value you are looking for, to see if it is in both lists. The table_array is the
table that you are going to have the formula look in. This is just a reference to the cells you want to look
in. The Col_Index_Num is the column number you want the formula to return if it finds a match. The
Col_Index_Num is relative to the first column which is 1. If you start in column A and you want to
return whatever is in column C you would put a 3 in the column. The Range_Lookup is a TRUE or
FALSE argument that tells Excel whether you want an exact match or a partial match. Ive personally
never used anything but an exact match, which is the FALSE argument.
The Example on the workbook
The example on the workbook compares two lists. One list has a list of trader names and the traders ID
number. The other list has the stock name and the traders ID number. Our objective is to compare the
first list to the second list and match the trader name to the stock on the second list. We do this using
the formula: =VLOOKUP(F11, $A$10:$B$33, 2, FALSE).

Our Data lists for the VLOOKUP example


Lets break it down:
The lookup_value in our formula is F11 for the first row of the formula (Remember this is a relative
reference because there are no $, so this cell reference will change when we drag the cell in down). Cell
F11 is the ID number on the second list. The ID number is our unique identifier.
The unique identifier has to be the same on both lists that you are comparing. Numbers are usually best,
but it can be text. This is what you use to compare the two lists. In our example we use the traders ID
number as our unique identifier because it is on both lists and there is only one ID number for each
trader on the list, making it unique.
The table_array is the area that you want Excel to look in to compare the two lists. In our example we
we excel to look in the range $A$10:$B$33. It will look in the first column in the table_array for the
lookup_value. The col_index_number is the column number you want returned, relative to the first
column in the table_array. Lets use our example to understand this. The first column in our
table_array is column A, this is column number 1. When the VLOOKUP finds a match we want the data
next to it to be the return value, so we put 2 as our col_index_number because we want the value in
the 2nd column of our table_array returned whenever there is a match. The range_lookup value is
the true or false argument saying whether you want the lookup_value to be an exact match or a
partial match. We used FALSE because it has to be an exact match in order for it to be returned.

In our example it is looking for the value of cell F11 in the range $A$10:$A$33. If it finds a match it will
return what is next to that cell in the range $B$10:$B$33, because the col_index_number is 2. In our
example cell F11s value is 15. The VLOOKUP looks for 15 in the range $A$10:$B$33 it finds a match in
cell A25 and it returns what is in cell B25, the traders name: Mike. We then drag this formula down the
entire list and we easily get the trader name matched to a the stock they are responsible for.
Think about the many ways this formula could be useful for what you do at work!

Time Formulas
Time formulas are useful in many ways. In this section well discuss how to find out different dates you
are looking for, months, days, years, end/beginning of the month, weekday, week number, and a few
other useful time formulas.
TODAY
Formula: TODAY()
This one is nice and easy, but extremely useful formula that allows you to find out the date of today.
This will change each day to show the current days date. All you have to do is put in the cell =TODAY()
and youll get todays date every time.

NOW
Formula: NOW()
The NOW formula is just like the TODAY formula in that its nice and simple. In the cell you simply type
in =NOW() and it will return the current date and time at the time the formula is put in the cell. This will
be updated each time the workbook is opened or each time you double click in the cell and then hit
enter.

YEAR/MONTH/DAY
Formula: YEAR(serial_num) | MONTH(serial_num) | DAY(serial_num)
The YEAR, MONTH, and DAY formulas allow you to get the year, month, or day from a date. On our
Excel workbook we gave two examples for each. We combined the NOW formula we talked about above
with each of these formulas and we just took the year, month, or date of a date in another cell. I wanted
to show you the power of combining formulas to find what you are looking for. To find the current year

we used the formula =YEAR(NOW()), to find the current month we used the formula =MONTH(NOW()),
to find the current day we used the formula =DAY(NOW()). On our Excel workbook we also put a date in
a cell and grabbed the different parts of the date. For the year we put the date 12/13/2012 into cell B12
and then we put the formula =YEAR(B12) into cell A12 and we got 2012.
Note: You cant put a date directly into these formulas i.e. (YEAR(12/26/2011)) because it will return
an error. Instead you have to put that date into a cell and then reference that cell in order to get what
you are looking for.

DATE
Formula: DATE(year,month,day)
The DATE formula returns the date of a reference point. On our Excel workbook I set up a formula that
will always show the first day of the current month. I did this by combining the YEAR, MONTH, NOW,
and DATE formulas together into the formula =DATE(YEAR(NOW()), MONTH(NOW()), 1). You can also
put the direct parts of the date in the formula to get the date: =DATE(2011, 6, 30) will give you
6/30/2011.

WEEKDAY
Formula: WEEKDAY(serial_number, return_type)
The WEEKDAY number allows you to find the week day number, from 1 (Sunday) to 7 (Saturday), of a
reference date. On our Excel workbook I used the formula =WEEKDAY(TODAY()) to get the week day
number of the current day. So, for instance if today is a Monday the formula will return 2. I also had the
formula reference a cell that contained the date of 12/25/2011, this was cell B33. The formula I used is
=WEEKDAY(B33) and it returned 1 because 12/25/2011 was a Sunday.

WEEKNUM
Formula: WEEKNUM(serial_number, return_type)
The WEEKNUM formula is a versatile formula. It allows you to get the Week Number of a reference
date. On our Excel workbook we have two WEEKNUM formulas. We used =WEEKNUM(TODAY()) to find
the week number of today in terms of the year. At the time of this writing it is the 52nd week of the
year. We also used =WEEKNUM(DAY(TODAY())) to get the week number of today in terms of the month.
At the time of this writing its the 4th week of December.

EOMONTH
Formula: EOMONTH(start_date, months)
The EOMONTH is a useful formula that tells you the last day of the month of the reference date. On our
Excel workbook I used the formula =EOMONTH(TODAY(),0) to find the last day of the current month.
The TODAY() is the reference date and the 0 shows that you want the current month. If you want next
month you would put a 1 in there. If you want the previous month you would put a -1. You could use
any number and it will give you the number of months away relative to the current month (if I put 12
months it will give me the last day of this month one year from today).

NETWORKDAYS
Formula: NETWORKDAYS(start_date,end_date,holidays)
The NETWORKDAYS formula allows you to find the number of work days between two reference dates.
On our Excel workbook I used a combination of NETWORKDAYS, DATE, YEAR, MONTH, and NOW
formulas to find out the number of work days in the current month. I used the formula
=NETWORKDAYS(DATE(YEAR(NOW()), MONTH(NOW()), 1), DATE(YEAR(NOW()), MONTH(NOW()), 31)).

WORKDAY
Formula: WORKDAY(start_date,days,holidays)
The WORKDAY formula returns the date before or after the specified number of workdays. In our Excel
workbook I used the formula =WORKDAY(TODAY(), 0) to show the workday date of the current day. This
means if it is a Monday through Friday it will show todays date. If today is a Saturday or Sunday it will
show Mondays date. It skips the weekend dates automatically. I put the 0 in the formula because I want
todays workday date. If I put in a 1 it will give me tomorrows date (If tomorrow is a Saturday or Sunday
it will go forward to Mondays date).

Conditional Formulas
Conditional formulas allow you to handle different situations with one simple formula. The basis of
conditional statements are IF statements and variations of it.
IF Statement
Formula: IF(logical_test,value_if_true,value_if_false)
The IF statement formula is one of my most used formulas, it allows you to handle a multitude of
situations with one simple formula. The logical_test is what you are trying to test. You can test any
situation in excel. This is a TRUE or FALSE argument. So if the test situation is TRUE it returns true, if the
test situation is false it returns FALSE. The value_if_true part of the formula is what is returned if the

logical_test returns TRUE. The value_if_false is what the formula returns if the logical_test
returns FALSE.
On our Excel workbook in the IF statements logical_test tested if cell B3 says yes. The
value_if_true is what the IF statement returns if the logical_test returns TRUE. If it says yes the
formula will respond with Im glad your having a great day!, if it says anything else other then yes it
will respond with Im sorry your having a bad day. The formula I used to do this is =IF(B3=yes, Im
glad your having a great day!, Im sorry your having a bad day).
One technique I use to make IF statements easier is to say it like a sentence: If B3 equals yes then say
Im glad your having a great day! If B3 does not equal yes then say Im sorry your having a bad day).
Note: Make sure you put around any text that you use in this formula.
You can also use two cells in the logical argument. In the second IF statement on the workbook we do
this to see if cell A7 equals cell B7.

Nested IF Statements
Formula: IF(logical_test,value_if_true, IF(logical_test,value_if_true,value_if_false))
Nested IF statements are nothing more than if statements nested inside of another if statement. This is
a powerful way to handle even more situations then a single if statement.
On our Excel workbook we used this formula: =IF(A3="I'm glad your having a great day!", IF(A8="yes it
does!", "All your statements up top are true", "Cell A7 does not equal Cell B7"), "I'm sorry your having a
bad day")
Lets break it down using our sentence technique
Heres what this says: IF cell A3 equals Im glad your having a great day! then if A8 equals yes it
does! then respond with All your statements up top are true if A8 does not equal yes it does! then
respond with Cell A7 does not equal cell B7. If cell A3 does not equal Im glad your having a great
day! then respond with Im sorry your having a bad day.

Lets break down how this formula got made:


You want to start with what is going to be your innermost IF statement, the answer you are looking to
find out.
Step 1:
IF(A8="yes it does!", "All your statements up top are true", "Cell A7 does not equal Cell B7")
Step 2:
After your first IF statement is created you wrap another IF statement around it, with the first IF
statement being the value_if_true part of the second if statement.
=IF(A3="I'm glad your having a great day!", IF(A8="yes it does!", "All your statements up top are true",
"Cell A7 does not equal Cell B7")
Step 3:
You have only added in the value_if_true part of the second IF statement. We now have to add the
value_if_false part. The second IF statement becomes the value_if_true part of your first if
statement. You must now put the value_if_false part of the first IF statement at the end to close out
the first IF statement.
The formula should look like this:
=IF(A3="I'm glad your having a great day!", IF(A8="yes it does!", "All your statements up top are true",
"Cell A7 does not equal Cell B7"), "I'm sorry your having a bad day")
As your going through writing your formula one of the tricky parts to keep track of is the parentheses.
While writing the formula Excel will bold both parentheses to show you where the opening parenthesis
is that you are currently closing.
A practical example:
One of the ways I use nested IF statements a lot is to see if a date in a cell is the same year and same
month as today.
I use this formula: =IF(YEAR(NOW())=YEAR(C23), IF(MONTH(NOW())=MONTH(C23),"yes", "The month
does not equal today's month"), "The year does not equal today's year")
The sentence technique:
This formula says IF the year right now equals the year in cell C23 then if the month right now equals the
month in cell C23 then respond with yes. If the month now does not equal the month in cell C23
respond with The month does not equal todays month. If the year now does not equal the year in cell
C23 then respond with The year does not equal todays year.

If you are going to use this to test the conditions of multiple cells (by say dragging a formula along the
side a list of data) then you may run into blank cells. The problem here is that it will respond with the
value_if_false part of the first IF statement. This could give you false positives. I usually add in one
more if statement around it.
The formula:
=IF(C23="","",IF(YEAR(NOW())=YEAR(C23),IF(MONTH(NOW())=MONTH(C23),"yes","The month does not
equal today's month"),"The year does not equal today's year"))
The beginning IF statement is now one that says this:
IF cell C23 is blank () then respond by leaving the cell blank.
Youll then leave that IF statement open and put an extra closing parenthesis at the end of the IF
statement so it looks like this )) these two close the 3rd IF statement and the 1st and 2nd IF statements.

IF Statement with AND Formula


Formula: IF(AND(logic1, logic2, logic3, etc),value_if_true,value_if_false)
Using a combination of IF Statements and the AND formula makes for a powerful combination. On our
Excel Workbook we used this combination in cell A26.
We used the formula:
=IF(AND(B26="Happy Holiday's", B27="Happy New year!"), "I hope you have both!", "You changed it!")
Lets use the sentence technique to figure what this is saying:
IF cell B26 equals Happy Holidays AND cell B27 equals Happy New Year! respond with I hope you
have both!. If either cell B26 does not equal Happy Holidays or cell B27 does not equal Happy New
Year! respond with You changed it!.
Using the AND formula in the logical_test part of the IF statement allows you to test two or more
logical arguments in one IF statement.

IF Statement with OR Formula


Formula: IF(OR(logic1, logic2, logic3, etc),value_if_true,value_if_false)
Using a combination of IF statements with the OR formula is a powerful combination. On our Excel
workbook we used the formula:
=IF(OR(WEEKDAY(TODAY())=1, WEEKDAY(TODAY())=7), "I hope your having a great weekend", "I'm sorry
it's a weekday")
I used a combination of IF, OR, WEEDAY, and TODAY formulas to determine IF today is a weekend day or
a weekday.
The Sentence Technique:
This is what its saying: IF the WEEKDAY number of TODAY equals 1 OR the WEEKDAY number of TODAY
equals 7 then respond with I hope your having a great weekend if neither statement is true respond
with Im sorry its a weekday.

SUMIF
Formula: SUMIF(range,criteria,sum_range)
SUMIF is a built in combination of an IF statement and the SUM formula. It is used to sum a certain
range based on criteria that you layout.
The formula:
=SUMIF($B$42:$B$44, "John Smith", $C$42:$C$44)
It works like this:
It looks in the range, in our case cells B42 to B44, for the criteria you set, in our case John Smith. It
then sums up the numbers in the sum_range, in our case cells C42 to C44 wherever it finds John Smith
next to it.

COUNTIF
Formula: COUNTIF(range,criteria)
As you might have guessed the COUNTIF formula works a lot like the SUMIF formula.
The formula we used on our Excel Workbook:
=COUNTIF(B50:B53, "John Smith")
This says:
Look in the range, B50:B53 and count the number of times the criteria, John Smith appears in
this range.

SUMIFS
Formula: SUMIFS(sum_range, criteria_range1, criteria1, criteria_range2, criteria2, etc)
The SUMIFS formula allows you to sum up a range of cells based on multiple criteria. It works just like a
SUMIF formula, but with more criteria.
Our Formula
=SUMIFS(D60:D63,B60:B63,$B$60,C60:C63,$C$60)
Note: I used all cell references in this formula to highlight that you can do this. Using this method can
make your formulas a lot more dynamic and easier to drag and drop instead of manually creating
formulas.
This formula says:
Sum up range D60 to D63 everywhere you find John Smith (the value of cell B60) in range B60 to B63
AND where you find Sales (the value of cell C60) in range C63.
In the example on the workbook I am trying to sum up the number of sales each salesman has had, but I
run into a problem where multiple people from different departments share the same name. I dont

want to count what John Smith from Human Resources has for John Smith from Sales, so I use the
SUMIFS formula to make sure it sums up only what is next to John Smith from Sales in the reference
columns.

COUNTIFS
Formula: COUNTIFS(criteria_range1, criteria1, criteria_range2, criteria2, etc.)
The COUNTIFS formula works just like the COUNTIF formula, just with more criteria.
Our Formula:
=COUNTIFS(B68:B71, $B$68,C68:C71, $C$68)
Continuing with the same list as above we are trying to count the number John Smiths in the Sales
department.
Our formula says this:
Count only the number of times you see John Smith (the value of cell B68) in the range B68:B71 and
where there is also Sales (the value of cell C68) in the range C68 to C71.

IFERROR
Formula: IFERROR(value, value_if_error)
The IFERROR formula is a useful formula to avoid having errors show up from the results of your
formulas. Sometimes errors show up in a cell letting you know that its not applicable (#N/A) or that you
are trying to divide by 0 (#DIV/0), but you dont want them to show up or you just want an easier way to
identify where this is happening. You especially dont want errors showing up and have your boss
thinking its your fault. This is where the IFERROR statement comes in handy.
On our Excel Workbook I gave three examples of how you could use this formula: one to have the cell be
blank if the result of the formula is an error, one to show ERROR if the result of the formula is an error,
and one without the IFERROR statement to show you what would normally happen.
I tried to divide 20 by 0 giving me a #DIV/0 error.

The formulas:
=IFERROR(20/0, "")
=IFERROR(20/0, "ERROR")
=20/0
The first formula says:
Divide 20 by 0, if the result of that formula is an error leave the cell blank (). The 2nd formula says if the
result of the formula is an error put ERROR in the cell. The third formula is the division formula all by
itself, which results in a #DIV/0 error message being shown in the cell.
Note: I use the first formula the most often. If there is an error I usually just want the cell to be blank

Examples of How Ive used Formulas


In this section I want to show you some specific examples of how Ive used formulas to solve issues that
Ive had when it came to generating reports.
WORKDAY, TODAY, and WEEKDAY to find out if the last day was Friday
The formula: WEEKDAY(WORKDAY(TODAY(), -1))
For reports I had to show weekending information, so I used this formula to determine if the last
workday was Friday (you could also just determine if today is a Monday, its just as easy either way
really). I would then use this formula in Macros I wrote using VBA (Ill teach you that in the next section)
to determine if the previous workday was a Friday, if it was the macro formed the week ending data for
the previous week.
This formula gives us the weekday number (WEEKDAY) of the previous workday. Remember the
anatomy of the workday formula is =WORKDAY(start_date, days, holidays). I used the TODAY() formula
for the start_date and put a -1 for the days part of the formula because I wanted the previous day. I
did not include holidays in this example. This gives us the previous work day. We get the week day
number by wrapping the WEEKDAY formula around the WORKDAY formula.

IF, OR, EOMONTH, WORKDAY, WEEKDAY to figuring the last workday of this month
The formula:
IF(OR(WEEKDAY(WORKDAY(EOMONTH(TODAY(),0),0))=7,WEEKDAY(WORKDAY(EOMONTH(TODAY(),0
),0))=1), WORKDAY(EOMONTH(TODAY(),0),-1), WORKDAY(EOMONTH(TODAY(),0),0))
I also had to have end of month reports so I used this formula to figure out the last day. I used an IF
statement and an OR statement to make sure that the last day being calculated was not a weekend day.
Heres what this formula is saying:
IF the WEEKDAY number of the last WORKDAY at the end of the month (EOMONTH) equals either 7 OR 1
then respond with the formula WORKDAY(EOMONTH(TODAY(),0),-1). IF the WEEKDAY number of the
last WORKDAY at the end of the month (EOMONTH) does not equal either 7 OR 1 respond with the
formula WORKDAY(EOMONTH(TODAY(),0),0).
The difference in between the two formulas that the IF statement responds with is the 0 and -1 in
the WORKDAY formula. If the weekday number equals either 1 or 7 we want it to show us the previous
workday, if it does not we want it to show us todays workday.

EOMONTH, TODAY, WORKDAY to figure out the first workday of the next month
The formula: WORKDAY(EOMONTH(TODAY(), 0), 1)
I used this formula so I could get the first workday of the next month.
This formula combination grabs the last day of the month (EOMONTH) and in that EOMONTH formula
we use TODAY as the start date and a 0 so it grabs the last day of month. We then wrapped the
WORKDAY formula around the EOMONTH formula so that we would only get work days and not
weekend days. We put 1 for the days because the EOMONTH generates the last day of this month
adding in the 1 gives us the next workday, which would be the first work day of the next month.

MONTH, YEAR, & to get just the month and year of a date
The formula: MONTH(B29)&" "&YEAR(B29)
I have used this method in multiple instances. Especially when you need to graph data by month, this is
an easy way to do that without manually typing in the month this date is in.
There is a date in cell B29. The MONTH formula grabs the month of the date and then I used the & &
to join in the YEAR which is grabbing the year of the date. The in between the two & puts a space in
between the month and the year. The result of the formula should look like this 12 2011. If there was
no in between the & it would look like this: 122011 (if you wanted that you would only need one &
so your formula would look like this: MONTH(B29)&YEAR(B29).

Adding time to a date


The formula: TODAY()+30
This is not a complicated thing to do, but a lot of people dont realize that Excel can be this sophisticated
with formulas. You can add days to a date just by putting =date + days in a cell. On our Excel workbook
I used the TODAY formula just to have a date and then added 30 days to it so I could tell what the date is
30 days from today.
Excel is sophisticated enough to realize that you are trying to add days to date and not trying to do an
arithmetic equation.

Converting a name from First Name Last Name to Last Name, First Name
This situation happens all the time where you grab data from one database and the names are in there
as First Name Last name and you grab data from another database where the names are in there as
Last Name, First Name. This can be a pain if you are trying to compare data based on names and way
to tedious to fix if you are dealing with a database with thousands of lines.

This can easily be done using text to columns and & to join them back together in the right order. In
this example I converted the First Name Last Name to Last Name, First name, but you can do it vice
versa using the same method.
Step 1 Use Text to columns to separate the First name and Last name at the space between the two
names (check the box next to spaces).
Step 2 The two names will now be in two different columns
Step 3 You want to use & to rejoin them in the right order and insert a comma in between the last
name and the first name. Itll look like this: =cellwiththelastname&,& &cellwithfirstname
On our Excel workbook the first name is in cell B47 the last name is in cell C47.
Our Formula:
=C47&,& &B47
Using this method we changed John Smith into Smith, John. I will usually copy all the cells I did this in
and then paste it where its supposed to be using the past special method to paste the values. This will
get rid of the formula, but keep the result.

COUNTIF and COUNTA to get the percent of times a certain text string appears in a list
Formula: COUNTIF(B50:B54, "Exceptional")/COUNTA(B50:B54)
This is useful to convert text into a number so you can use it as a rating. On our Excel workbook I used a
list of ratings: Exceptional, Meets Expectations, Average, Below Average. Lets say this was a product
rating system that a person put into the database.
I need to figure out the percent of times the product was considered Exceptional.
I used the COUNTIF formula to count the number of times Exceptional appeared in range B50 to B54. I
then used the COUNTA formula to count the number of non-blank cells in range B50 to B54. I then
divided the result of the COUNTIF by the result of the COUNTA to give me the percent of the time
Exceptional showed up in the list. Unfortunately, for this product manager the product was
considered Exceptional only 20% of the time in our example.

COUNTIF, COUNTA, Nested IF Statements, MAX and hiding cells to show the text that appears
the most on average from a list
Formula: IF($B$73=MAX($B$73:$B$76), $A$73, IF($B$74=MAX($B$73:$B$76),
$A$74,IF($B$75=MAX($B$73:$B$76), $A$75,IF($B$76=MAX($B$73:$B$76), $A$76, ""))))
This is expanding on the last formula. This is done in two parts. The first part I put below the list and
then hide it. To unhide it just highlight the whole row, right click, and click unhide.

Notice on our workbook it goes from row 78 to row 83. Thats because the cells in between have been
hidden.

This is an easy way not to have your formulas messed with. Once these formulas are created there is no
reason to change them, so hiding them will prevent you from making a mistake or from others messing
with your formulas.
In the hidden area is the same formula that we used in the last part. The only difference is that I used a
cell reference to get our criteria for the COUNTIF formula. I did this because I built the formula in cell
B79 and then dragged it down to cell B82. Notice the $ signs so I could keep the ranges I didnt want to
change and notice in the criteria part of the COUNTIF formula theres only a $ sign in front of the letter

and not the number. Thats because as I dragged it down I wanted the criteria to change with the row
number.
Formula used:
=COUNTIF($B$67:$B$71, $A79)/COUNTA($B$67:$B$71)
I then used a combination of nested IF statements and the MAX formula to figure out which rating
appeared the most and return the proper text string.
Formula Used:
=IF($B$73=MAX($B$73:$B$76), $A$73, IF($B$74=MAX($B$73:$B$76),
$A$74,IF($B$75=MAX($B$73:$B$76), $A$75,IF($B$76=MAX($B$73:$B$76), $A$76, ""))))
Here is what it says:
IF cell B73 is the highest value (MAX) in the range B73 to B76 then respond with A73. If not, then IF B74
is the highest value (MAX) in the range B73 to B76 then respond with A74. If not, then IF B75 is the
highest value (MAX) in the range B73 to B76 then respond with A75. If not then IF B76 is the highest
value (MAX) in the range B73 to B76 then respond with A76. If none are true (shouldnt happen) leave a
blank cell.

Dividing and Nested IF statements to divide by a certain number, given a certain criteria
Formula used: 100/IF(B75=4, 40, IF(B75=3, 30, 20))
I used this method because different people had different levels. They were supposed to handle a
different amount of the products if they had a higher level. I used this formula so I didnt have to
constantly change the formula instead I just had to change their level in the cell.
As you can see from the formula I put the persons level in cell B75. If their level is 3 the formula will be
100/30, giving me 3.33. If their level is 4 the formula will be 100/40 giving me 2.50. This made it easy to
handle changes and also to drag the formula down for the whole list instead of manually typing in the
formula for each cell for thousands of lines.

Combining Nested IF statements and the OR formula to count something only if all criteria
are met
Formula used: IF(OR(B95="ACTIVATED", B95="IN SERVICE"),IF(C95="COMPLETE", IF(D95="NEW",
IF(OR(E95="Product 1", E95="Product 1 (Special)", E95="Product 1 (change)"), "TOTAL", ""), ""),
""),"")
This is a method I use all the time to count a product only if all the criteria are met.
Heres what it says:
IF B95 equals Activated OR In Service, then IF C95 equals COMPLETE, then IF D95 equals new
then IF E95 equals Product 1 OR Product 1 (Special) OR Product 1 (change) then respond with
total. If any of these criteria are not true then respond with a blank cell.
You can then run a COUNTIF statement looking for TOTAL and you could get the total number of
products that are new, activated or in service, completed orders for any variation of Product 1.

Introduction to Visual Basic for


Applications (VBA)
Visual Basic for Applications (VBA) is the code that you use to write macros to automate Excel. In this
section I will introduce you to the developer tab, recording macros, VBA code editor, VBA code, and
running macros that I created. I created a bunch of different example macros and well dive in to each
one, along the way youll learn a lot about VBA code. Remember though, this is only an introduction;
there is much, much more that this code can do that is beyond the scope of this particular tutorial.

First some explanation:


The definition of a Macro: A single, user-defined command that is part of an application and executes a
series of commands.
This basically means that a macro is something you set up, using VBA code, to automate operations in
Excel that you would otherwise do manually. You then initiate the macro and it runs a series of
commands that handles different operations in Excel.
Object Oriented Programming (OOP) this is a computer programming paradigm that is used in almost
all modern coding languages. Im not going to dive into the ins and outs of it, but this pertains to VBA
because it is object oriented programming. Well, it is not truly an OOP language, but it does deal with
objects.
In Excel objects are things that you want to manipulate and methods are what you use to manipulate
the objects. It usually works like this: object.method For instance we would use: Activecell.copy to copy
what is in the currently selected cell. Activecell is your object and copy is your method.
The Developer Tab

Going from left to right:


The Visual Basic button - This button allows you to get the VBA editor which is where all the code is
stored. You can edit, create, and delete whole macros or just lines of code from the editor (well go over
that in depth next). You can also open the VBA editor by holding in the alt key and pressing the F11 key
on your keyboard.

The Macros button This button is where you can control your already written macros from. You can
run macros, step into them so you can run the macro line by line, edit the macro (opens up the VBA
editor), delete, and go to your options. Below is a screen shot of what pops up when you click this
button.

The menu that pops up when the Macros button is clicked

The Record macro button You click this button and then manually do whatever you want the macro to
do. While this is recording it will automatically write the VBA code for whatever you are doing in Excel.
This is an extremely useful button.
The insert button- This button brings down a drop down menu that shows you all sorts of controls you
can add into Excel that will allow the user to control macros. I use this specifically to insert an ActiveX
command button that allows me to start macros with the click of a button (This is what I did with all the
buttons on the VBA tab on the Excel Workbook).

An example of an ActiveX command button


Design Mode button- You have to click this button to interact with the buttons in order to edit them,
move them, or view the code that is behind them. If you try to click on the button without the Design
mode button clicked it will take this as you trying to click the button and run the macro.

The VBA Editor


The VBA editor is a new screen that pops up when you click the Visual Basic button. This is the
interface youll use to write the VBA code, edit the code, or see the code that was written when you
recorded a macro. This is the area that code is stored in.

The play, pause, and stop button


The play, pause, and stop button allows you to control the macro. You can play it, pause, and stop the
macro from running. You can also stop a macro that is in progress using the stop button or by pressing
the Esc button.
Modules
Modules are just separate sheets that allow you to store blocks of code in there. Separating your code
into separate blocks is a best practice to use as it allows you to stay more organized. The more
complicated your macros are, the more code you will have and the harder it will be to stay organized.
Seperating your code into separate blocks and separate modules can save you a lot of time.
The Insert Menu
I use the insert menu to insert Modules, which is where I store the code. I usually try to separate large
sets of macros into multiple modules simply for organization purposes. You could write as many lines of
VBA code as you want in one sheet or one module if you wanted, the code doesnt care, but the best
practices of coding show that you want to split code sets into individual functions so it makes it easier
for you to edit the code.

Project Explorer: The project explorer allows you to explore the VBA code that is stored in different
open workbooks.
This should be shown on the left side of the VBA editor by default but if it is not: click the View menu
and on the drop down menu that appears click the Project Explorer button to make it appear. You can
also use the short code of holding down the Ctrl key and pressing the letter R.

The View menu where you can show or hide the project explorer

The project explorer

The above screenshot is the view of the project explorer for the example workbook. Module 1 is
where I store all the example code. Sheet 1 (VBA) is where all the code for each of the buttons is
stored. If the Project Explorer is not visible in the editor either hit the control button (Ctrl) +R or go to
View and click Project Explorer.
Debugging a Macro
When creating or editing a macro you can go into the VBA Editor to any of the and hit the F8 button.
This allows you to go line by line running the macro. This is extremely helpful when solving errors that
will inevitably popup when you are programming. A good idea is to run the macro line by line using this
method right after you create it. This can easily allow you find any errors that could occur.
The Structure of a Macro
Sub macro_name
Code you want to execute
End Sub
When you are creating a macro this is the structure it will look like in the editor. If you manually create it
you will start each macro with the Sub line and end each macro with a End Sub line. Right next to
the Sub line will be the name of the macro. You can choose any name you would like. I usually like to
name my macros to describe whatever it is that the macro is going to do. For example in our macro
where I show you how to copy and paste it looks like this: Sub copy_macro(). I want you to point two
things: the () is automatically put in by the editor and that there is an underscore in between copy and
macro. There cannot be any spaces in the macro name. An underscore I an easy way to have spaces in
between so you can have an easily readable macro name.
The code that you would like to be executed goes in between the Sub and End Sub lines.

Creating Macros
All of the code for the following macros is on the example Excel Workbooks VBA editor. Ill reference
this code frequently, so please use this editor and the code in it to learn how to create macros.
Comments in VBA
On the VBA editor you can write in comments around the code that actually runs the macros. Comments
are helpful for you to keep track of what the code is doing and if anyone else ever has to look at your
code it can help them keep track of what is going on in the code. This can be done simply by putting an
apostrophe () in front of the comment you want to write. This tells the VBA to skip this line and go to
the next line without an
in front of it.

Inserting the button and creating the code for the button:
In order to insert the button you go to the insert button and the drop down menu that appears when
you click it, click on the top left most button under ActiveX controls. This will insert a blank button onto
the worksheet.

Youll then want to click the Design mode button so you can edit the button. After clicking the design
mode button you can right click on the button and go to Commandbutton Object. Once you hover
over that a menu will pop out to the right and it will say Edit. Click this and you will be able to write
what you want on the button. Thats how I put Copy Macro on the button that runs this macro.

The Edit button

The View Code button

To write the code for the button youll have to write click on the button again (make sure your still in
design mode.) this time click view code. This will bring up the VBA editor and take you directly to the
Sub for this particular button. This code is kept on sheet where the button is being stored, in our case
Sheet 1 which is the VBA tab. When you create a button and go into the VBA editor where a private sub
is created: Private Sub Commandbutton1_Click(). The Commandbutton number will change for each

button you add (so if we add another button to this sheet it will be Commandbutton2_Click()). This
code will be run only when the button is clicked.
Running Macros With Buttons and Cascading Macros
One awesome thing to do is to trigger macros using other macros. This allows you to keep separate
operations in separate macros and still accomplishing a lot.
The code we use to do this:
Application.run(macro_name).run
The above line of code will trigger the macro that you want to start running. When we created our
button we have to tell the button which macro to trigger when it is clicked. In the Private sub that is
created when you create a button you place this code and replace the macro_name with the name of
the macro you would like to run (remember you get there by clicking the View Code button when you
right click on the button).
You can also place this code at the end of your macros to trigger another macro. This way you can
separate your different operations into different macros for faster operation and better organization.
Simply place this line of code at the end of one macro and trigger the next macro you would like to
execute.
Copy & Paste Macro
Writing the code for the macro: Well go line by line of the code and step by step to make a macro that
copies and pastes information from one cell to another. Open up the VBA and go to the Sub names
copy_macro at the top.

Were going to copy the values of cells A5 and A6 and paste it into cells B5 and B6.
The next line of code is Range(A5:A6).select. This tells Excel to select cells A5 and A6. This is the
same as if you were to click on cell A5 and drag down to cell A6, highlighting both cells A5 and A6.
The next line is Selection.copy. This tells Excel to copy whatever cells are currently selected to the
clipboard.
The next line is Range(B5).select. This selects cell B5, which is where I want to paste the data.

The next line is Activesheet.paste. This tells Excel to paste whatever is on the clipboard in the
currently selected cell.
The last line of code before the End Sub is End. This stops the macro and prevents it from going any
further. If this line of code wasnt there it would try to go back to the code for the button and re-run the
whole macro. The last part is only necessary if you have cascading macros or you triggered your macro
using a button.
Clear Contents Macro
The clear contents button is the same as hitting the delete button, it will clear whatever is in the
selected cells. In our case it is going to select the cells B5 to B6 (which is where we pasted into in the last
macro example) and clear the contents of those cells.
The code for this macro:

Range(B5:B6).Select selects the range that you want to clear contents


Selection.clearcontents deletes the data in the cells that are selected.
End as always is there to stop the code and prevent it from trying to continuously re-run the macro.
Message Box Macro
The Message Box allows you to have a message box pop up on the screen to notify the user of
something. This can be used to notify the user of something that may be happening or it can be used to
get information from the user.

When you are creating a message box you can customize it for whatever you are doing. The Prompt is
whatever is going to be said in the message box, the Buttons As VbMsgBox Style = VbOKOnly tells the
code what buttons to have on the message box. The default is to only have an Ok button on the
message box. You can also have a bunch of other combinations. I have used the vbYesNo and
vbYesNoCancel. As you may have guessed they will put yes and no buttons on there and
vbYesNoCancel will put yes, no, and cancel buttons on the message box. When you get to that
part of the code it will pop up with a menu that you can scroll with all the different options (check the
screen shot below). I encourage you to play around with the different button and input types for

message boxes to see what works for you. You can also control the different parts of the box: The title,
Help File and context. The last three are all very optional and I honestly have never had to use them.

The drop down menu that appears to choose the button combination

On our Excel Workbook we had this message box pop up when you click the Message Box button.

Heres the code for how we got here:

This is a simple message box so I didnt get into the optional parts of it.
The Message box code is simply this: MsgBox (Your starting to learn VBA to make macros!) and you
can see how it works with the message box. Your starting to learn VBA to make macros! is what is
shown in the message box that pops up. I didnt define the type of buttons that I wanted to be seen, so

it shows the default which is vbOkonly. So, it only shows the Ok button. When I click the Ok button the
message box will disappear.
Change Cells Fill Color Macro & The Macro Recorder
The color me macro changes the fill color of a cell. When you click this button it will change the fill color
of cell K5 to red. This is a macro that is easily recorded using the macro recorder. You simply just click
the Record Macro button, name the macro, and then click cell K5 and change the background color to
red as you normally would. If you did that the code below would be generated. I do this for every macro
I create where I want to change the fill color. Its just easier and I dont have to worry about finding out
the color code number for the color I am looking to have it changed for.

This code is using a With statement. This allows you to select a certain cell or range and using the
with statement you can manipulate this selection in multiple ways and write less code. If we didnt use
the with statement the code would look like this: Selection.Interior.Pattern = xlSolid,
Selection.Interior.Color = 255, etc. You can see how this can save you a lot time and possible
frustration by not having to repeatedly type the same code over and over again.
Clear Fill color of Cells
This is the same method as above. We just simply changed the color we want the fill to be 0. This
makes it so that there is no fill color in the selected code.

We selected the same cell and used the same with statement. We simply changed the color to 0.
Insert Text Macro
In order to insert text into a cell we use the FormulaR1C1 method.

We first select the cell where we want to insert text using the Range.select line. In our case we want
to insert text into cell P5.
We then use the FormulaR1C1 method to insert text. The code: ActiveCell.FormulaR1C1 = (TEXT)
inserts the text you define in the code into the currently selected cell. In our case we inserted You
inserted Text using a macro! with the formula ActiveCell.formulaR1C1 = You inserted Text using a
macro!
Insert Formula Macro
Inserting a formula with the FormulaR1C1 method is the same basic principal as inserting text. There is
simply a difference in what actually gets inserted. Where you put the text in the code youll put the
formula that you want in there. Unlike putting a formula in a cell in an Excel Worksheet where it would
be something like P5, you have to use the R(Row #)C(column #) format. R stands for Row and C stands
for Column. To get cell P5 you would put R5C16 (Row 5 Column 16).

Notice the two arrows with the code in the screen shot above. With this formula I am inserting an IF
statement into the cell. In VBA we used the R(Row#)C(Column #) format R5C16 to get cell P5. Also,
where ever you would put a in a normal Excel formula you have to put in VBA code because quotes
are used for multiple reason in VBA code. This is how you tell the code that you want a quotation mark
inserted into the cell.
Normal If Statement:
IF(P5=You inserted Text Using a Macro!, You inserted Text!, You didnt run the insert text macro!)

VBA If Statement:
IF(R5C16= You inserted Text Using a Macro!, You inserted Text!, You didnt run the insert text
macro!)
See the differences? Now try it yourself!
Select Worksheet Macro
To select a worksheet you simply need one line of code and the sheet name.

The code to select a sheet is: Sheets(WORKSHEET NAME).Select. You can also use
Sheets(WORKSHEET NAME).Activate or Worksheets(WORKSHEET NAME).select
In our Excel Workbook I wanted to select the Examples sheet so that is the worksheet name I put into
the code.
I also added in a message box that lets you know that you successfully ran the macro that selected the
Examples sheet.
Filtering Macro
You can write VBA code that will filter a given range based on a given criteria. On our Excel Workbook I
wanted to filter the cells in range F10 to F17 so it showed only non-blank cells. In order to create this
macro I just used the macro recorder because it is much easier and can save you a lot of time and
possible frustration.

This filter code filters only based on one criteria: non-blank cells (<>).
There is code that can handle multiple criteria. See the below screen shot to see the code that can
handle filtering for multiple criteria. We used this macro to filter to only show the cells that have either
Filter this list or only non-blank cells.

Select Used Cells Macro


The select used cells macro allows you to select all used cells in a worksheet. It is the same as using the
Select all or ctrl+a to select all used cells manually.

I use Range(E10).select to select the upper left most used cell which happens to be cell E10.
I then used Range(Selection, Selection.End(xlDown)).Select in order to select from cell E10 to the last
used cell in that column, which in our case was cell E15. This is the same as going to cell E10 and hitting
ctrl+shift+ down arrow.
I then used Range(Selection, Selection.End(xlToRight)).Select in order to select the used cells to the
right. This will take you to the last used column to the right. In our case this was column F.
After this all the used cells are selected. I use this method all the time to highlight all the used cells on
one worksheet, copy the selected cells, and paste it into a new worksheet/workbook.

VBA If statements & VBA formulas


IF statements in VBA are slightly different then using IF statement formulas in Excel, although it is still
the same basic logic.

Date in VBA is the same as the TODAY() formula in Excel, WEEKDAY operates the same way in VBA
as it does in the Excel formula. The code gets the week day number of todays date. I then use a VBA If
statement to figure out if today is a Saturday (Weekday = 7). If it is then a message box pops up saying
Its a Saturday wahoooooo!. If it is not a Saturday it will respond with a message box saying Its not a
Saturday :(.
A VBA if statement looks like this:
If (logical argument) then (this sends it to the code below if the logical argument is true, if not it sends it
to the line that says Else)
(Code to run if logical argument is true)
Else
(Code to run if logical argument is false)
End If
If, Else If Macro
If, Else If macros work much the same as a VBA IF statement with one tiny, but powerful difference. In a
normal VBA statement it works like this: if logical statement is true, run this code, else run this code, end
if. An if Else if macro works like this: if logical statement 1 is true, run this code, else if logical statement
2, is true, run this code, end if. There can be as many Else Ifs as you want, I just chose two for
examples purpose.
In our example Macro we used an if, else if macro to determine what day it is and show the appropriate
message box.

The Macro Works Like This:


We use the Weekday(Date) code (remember Date is the same as the TODAY() formula). This
gives us the weekday number. The formula says IF the weekday number of today equals (number of the
day 1 = Sunday 7 =Saturday) show this message box else if show this message box, etc.
This allows us to determine the day of today and show the message box right below this logical
statement.
For Each Loop Macro
Loops are an important part of any programming language. It allows you to loop through a set range of
cells and manipulate each cell as needed or test each cell to see if it meets a certain criteria. In the
example on our worksheet our object is to change the fill color to red in all the cells that say color me
in the range P11 to P17.

We start the For Each loop using the line of code: For Each cell in in Range(P11:P17). This tells Excel
that we are starting a loop and that we want it to loop through all of the cells in between P11 and P17.
Note: The rest of the code we right: the IF statement, the with statement, and the Activecell.offset
will be run on each cell in the range that is being looped through.
Our next step is to use a VBA IF statement to test whether or not the currently selected cell contains the
text color me. If it does then it moves on to the with statement. If it does not it goes directly to the
line that says End If. We do this with the line of code: If Activecell.value = color me then
If the active cell says color me the code moves on to the with statement. This is actually the same
code from the color me macro, I literally copied and pasted this code. As a refresher this with
statement changes the fill color of the cell to red.
If the active cell does not say color me the code moves to the End If line of code and just continues
on in the loop.
The next line of code is: Activecell.offset(1,0).select. This line of code selects the cell directly below the
active cell. The offset method allows you to offset the currently selected cell by a specified number of

rows, or a specified number of columns, or both. It works like this: offset(rowoffset, columnoffset).
The rowoffset and columnoffset are the number of rows or columns away from the currently
selected cell you want to be selected.
We put the offset method in there because the next time the loop goes through we want the next cell
to be tested and the color changed. If we didnt offset the cell it would continuously change the color of
the same cell.
The line of code after the offset method is the Next line. This is part of the For each loop and it tells
the loop to move on to the next cell in the range you specified. It does this until it reaches the last cell in
the range you specified, after which it moves on to the next line of code which is the End method to
stop what the macro is doing.
Create New Worksheet Macro
This macro creates a new worksheet and changes the name of the worksheet.

The line Worksheets.Add is the line of code we use to create a new worksheet and we then used the
line Activesheet.Name = New Sheet to change the name of the worksheet that we just created. If we
did not have this line Excel would automatically change the worksheet name to something similar to
Sheet1.
Once you create a new worksheet that becomes the activated sheet. That is why we are able to use the
ActiveSheet object to change the sheet name.
Hide/Unhide Worksheet Macro & Error Handling in VBA
In these two macros I show you how to hide and unhide a worksheet. This makes it so it is either visible
or not visible to the user. I also introduce you to error handling in VBA which is code you can specifically
put in there to tell the code what to do in the event of an error.

First Ill show you how to hide the sheet and then Ill explain error handling. To hide a sheet simply use
the Sheets object and set the visible method to false. We do this using the line of code:
Sheets(New Sheet).Visible = False. To unhide it we use the code Sheets(New Sheet).Visible =
True

Error Handling in VBA


Error handling is code that you can put in a sub to tell the code what to do if an error occurs, you tell it
how to handle it. VBA has a few built in methods: On Error resume next, On error go to, and on error go
to 0. On Error Resume Next, which we use in our code tells VBA to simply ignore the error and resume
the macro at the next line of code.
On error go to allows you to put a specific error handling function in the macro, Ill show you an example
of this in two more sections.
On Error go to 0 is rarely used because it is the default for Excel. If this is the case it will display Excels
built in messages, which are usually not helpful unless you are extremely knowledgeable about the inner
workings of Excel.
I specifically put the On Error Resume Next code in our macros because if you run the hide/unhide
macro or the delete worksheet macro, without first creating the new sheet, it will cause an error.
(Shown below)

To prevent this from happening I put in the On Error Resume Next which would send the code to the
End line and end the macro without anything happening at all.
Delete Worksheet Macro
Deleting a worksheet is much like adding a worksheet. I use the On Error Resume Next line to prevent
an error message from popping up if you did not create the new worksheet, as discussed above.
I then use the line Sheets(New Sheet).Delete to delete the new sheet that we created in the Create
a worksheet macro.

Create a New Workbook Macro


This is a simple method to create a new, blank, unsaved Excel workbook. In order to create a brand new
workbook with VBA all you need is to put the line of code Workbooks.Add in your macro.

Open an Excel Workbook Macro


This macro is somewhat complicated and it involves different steps. Ill lay out the code and well go
through this line by line.

The first line On Error GoTo errhandler: is how we handle errors. What this does is tell the VBA code:
In the event of an error go the section of code labeled errhandler:. This automatically sends the code
to the bottom portion which is labeled errhandler: The errhandler: then sends the code to the lines
below it; which is a message box saying You did not put a filename in the code. And an End line
which stops the macro.

The purpose of handling errors this way, for this macro specifically, is because you need to put a file
name in that you want to open (Ill teach you how to do that). The code sends a message box that asks
you if youve done this, if you click yes it will try to execute the code. However, if you do this, and
there is no file name there, it will cause an error. If this happens you are sent to the errhandler: which
sends out a message box letting you know the error that occurred and stopping the macro from running.
I stop the macro in this case because there is no point in continuing the macro if there is no file name in
the code.

Ok, lets move on from Error Handling.

The next line is a combination of the code for a message box and a VBA IF statement.

The message box code is: MsgBox("You have to put a file name in this one, did you do that?",
vbYesNo). Notice I changed the button type from the default vbOkonly to vbYesNo. This will make
it so the buttons on the message box are Yes and no.
I then wrapped a VBA IF statement around this message box code that says: If the answer to the
message box is No then run the next message box otherwise end the if statement. I didnt explicitly
put an else part to the IF statement. Instead if the IF statement is false then it just goes right to the
end if line of code.
If the answer is no, it runs the message box that says Put a filename into the code below. Check the
tutorial for what to do.

I then put an End line because if the answer is No, you have not put a file name into the code then
there is no point in continuing the macro.
After this I put the End If line so that the VBA code knows that it can stop executing the if statement.
After all of this we finally get to the code to open up the file.
The code to open the file name looks like this: Workbooks.open Filename:=(FILE NAME HERE)

The easiest way to get the file name is to go to the file location, right click the file, and go to the
properties.

Above is a screen shot of the properties window that comes up when you right click on the file. You are
looking for the location of the file. It should look something like: C:\Users\username\Desktop. The
user name is what is blacked out in the picture. At the end of Desktop youll have to add a backslash and
the actual name of the file. I opened a file named test.xlsx from my desktop, so my file path would be:
C:\Users\username\Desktop\test.xlsx. If I put that line into the code it will open up the test.xlsx
file located on my Desktop.

The final result of the code should look like this:

Notice the filename location. You can select any Excel file located on your computer, grab the location
like I showed you, add in the file name, and run the code. This macro will then open up this file.

Conclusion
Microsoft Excel is a powerful program that can do a lot of things. I tried to cover the basics and what I
believe will help you out the most with this tutorial. That being said this is only the tip of the iceberg and
there is still a ton more that Excel can do. Ill be making more advanced tutorials for the Excel Super User
and Advanced VBA/Macro development, so keep an eye out for that. In the meantime though I
encourage you to dive deeper into both Excel and VBA in order to enhance your skills, thank you for
reading and I hope this tutorial has proved useful.

S-ar putea să vă placă și