Microsoft Office Excel 2010 Advanced 346
Lesson 4.2: Using the VLOOKUP Function
Excel provides two lookup functions that you can use to quickly retrieve information from data
organized in a table. The functions are called HLOOKUP (horizontal lookup) and VLOOKUP
(vertical lookup). In this lesson, you will learn how to use the VLOOKUP function to find data,
limit VLOOKUP to an exact match, and find the closest match with VLOOKUP.
Understanding VLOOKUP and HLOOKUP
The VLOOKUP function will look in the leftmost column of a table for a value you specify. When
it finds the value you specify, it will return a value that is located in the same row, a specified
number of columns into the table. It is called VLOOKUP because it looks vertically down a
column for a match, and then retrieves data from somewhere across that row.
HLOOKUP is similar, but it will look horizontally across the upper row of your table, and then
retrieve data from somewhere below in the column. Since Excel is designed with more cells in
the vertical direction than in the horizontal direction, and because vertical table design is more
intuitive for most people, VLOOKUP is generally used more often than HLOOKUP.
Using VLOOKUP to Find Data
The best way to learn how lookup functions work is to look at an example. Here we have a table
of ticket prices for flights to different countries. To simplify matters, the data range for the
table has been given a defined name (“price”) that can be used in functions and formulas.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 347
The arguments for the lookup function are: VLOOKUP(value to match, lookup table name or
range, number of the column in the table containing the relevant data, true or false).
If we activate cell F1 and enter =VLOOKUP("England",price,2) into the formula bar, F1 will show
the value 550:
The lookup function looked vertically down the leftmost column of the lookup table (price) until
it found a match for the text string “England.” The function then returned the value that is in
the second (2) column of the table, in the row where the match was found. You should notice,
that England, price, and 2 are the exact arguments used in the function.
For this example, the true or false argument was left out. The relevance of the true or false
argument in the VLOOKUP function will be discussed shortly.
To use the VLOOKUP function correctly, you need to have your spreadsheet data laid out
properly in table form with at least two columns. The first column in the table will contain the
keys (identifiers that the VLOOKUP function will examine for a match). In the example just
shown, the keys are the names of the countries. This first column can be referred to as the
lookup column.
The other columns in your table will contain data that corresponds to the column of keys. Your
table can be several columns wide, and you can specify which column VLOOKUP will retrieve
data from by putting a number corresponding to the given column in the function. In the
previous example, we wanted VLOOKUP to return the ticket price, so we used the number “2”
(for the second column) as an argument in the function. If your table has 10 columns and you
want to return data from the ninth column, you would use 9 as an argument.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 348
You do not have to use text values (like the country names used here) in your lookup column. If
it is more appropriate, numbers or dates will serve just as well.
If you want some help when you are using VLOOKUP, use the Insert Function dialog:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 349
You will find the VLOOKUP function in the Lookup & Reference category. If you click OK in the
Insert Function dialog, you will see the helpful Function Arguments box:
Simply enter the function arguments in the fields provided.
How to Find an Exact Match with VLOOKUP
As mentioned briefly before, the VLOOKUP function can have a final argument of either TRUE
or FALSE. If you specify this final argument as FALSE, VLOOKUP will search for an exact match in
the lookup column. If it cannot find an exact match, no data will be returned.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 350
In the following example, the function =VLOOKUP("jpn",price,2,FALSE) has been entered in cell
D1. This means that VLOOKUP will search the first column of the table for an exact match of
“jpn,” and if it is found, it will return the corresponding value in column 2.
Because there is no country that exactly matches “jpn,” no value will be returned to cell D1. If
you change the function so that the first argument is “Japan,” an exact match will be found. The
value 925 is returned to cell D1.
If there are two or more exact matches, the data corresponding to the first matching value
found in the lookup column will be returned.
Finding an Approximate Match with VLOOKUP
If the final argument in your VLOOKUP function is TRUE, VLOOKUP will search the lookup
column for an approximate match to the search value you enter in the function. If there is an
exact match to your search value in the lookup column, the corresponding item from that row
will be returned. However, if no exact match is found, a value corresponding to what Excel
deems is an approximate match will be returned.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 351
For example, if you enter “jpn” as the search value for the table of ticket prices, the value
associated with the country Japan (925) will be returned. “Japan” is the closest match to “jpn”
that Excel can find in the lookup column.
Similarly, if you enter “Ity” as the search value for the function, the airfare corresponding to
Italy will be returned:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 352
This feature can be useful if you have a lengthy lookup column and you aren’t quite sure how to
spell what you are looking for; however, there are no guarantees when looking for an
approximate match.
If you omit the final argument in your VLOOKUP function, it will default to TRUE, and search for
a closest (approximate) value if an exact match cannot be found. If you use the TRUE argument
(intentionally or by default), the values in the lookup column should be sorted in ascending
order. Otherwise, you may get unexpected results. If you are using text values in your lookup
column, avoid using leading or trailing spaces, as this may also produce unexpected results.
If you use FALSE as the final argument to your function (for an exact match), the lookup column
does not have to be sorted.
Using VLOOKUP as an Array Formula
You have just seen how to use VLOOKUP to find a data value in a row that corresponds to the
presence of a search value in a lookup column. Now that you know how to return a single data
value with VLOOKUP, the question becomes, how do you use VLOOKUP to find multiple values?
Study the following worksheet. It is an extension of the previous examples in this lesson.
The range of data in the Air Fare table is called “ticketData.” This range contains information
regarding the availability of first and second class seats. If there are seats available, the value is
“yes,” and if there are no seats available, the value is “no.”
Suppose you want to have the VLOOKUP function return the data for the ticket price, and for
the seats that are available. You can perform this task by using VLOOKUP as an array formula.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 353
Here is a VLOOKUP function that will retrieve three data items:
=VLOOKUP("Japan",ticketData,{2,3,4})
As before, the first argument is the search value: “Japan.” The next argument is the range of
data to be searched. (The range has been named “ticketData” to make things easier.)
As you already know, the next argument is supposed to identify the column from which
VLOOKUP retrieves the data if a match is found. This time, a set of three numbers has been
given as the argument “{2,3,4}” rather than just one number. Our intention is to have VLOOKUP
retrieve multiple values from the table of data.
To make this function work, we must first select a set of three cells to receive the data. Next,
we will add the VLOOKUP function to the formula bar:
To make the function work as intended, we will now enter it as an array formula (by pressing
Ctrl + Shift + Enter). We know the function has been successfully entered as an array formula by
the curly braces that appear around it:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 354
The VLOOKUP function now returns multiple data items from the table based on a match with
the search value “Japan:”
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 355
Lesson 4.3: Using Custom AutoFill Lists
As you probably already know, Excel’s AutoFill feature can come in handy when you need to fill
rows or columns of cells with incrementally changing values. All you have to do is enter a value
in one cell, and then enter another value in an adjacent cell that is greater or smaller than the
first value by some increment. Once you do this, you can fill the rest of the row or column by
selecting the two starting cells and clicking and dragging over the cells to fill.
Did you know that Excel lets you go one step further by creating own custom AutoFill lists? In
this lesson you will learn what a custom AutoFill list is, as well as how to create, use, and modify
one.
What is an AutoFill List?
An AutoFill list is a series or sequence of numbers or text strings that can be filled into cells
automatically when you drag your mouse pointer down a column or across a row.
By default, Excel has the capacity to AutoFill numbers and dates that increase or decrease by a
fixed increment. Excel’s default AutoFill feature can also handle text and number combinations
that change by a fixed amount, like Region1, Region2, Region3, etc.
Excel’s AutoFill feature is convenient for many situations, but what if you want to have cells
automatically filled with specific values that do not change by a fixed increment? What if, for
example, you want to have a series of different product names, or a sequence of addresses or
alphanumeric ID numbers entered into your cells with AutoFiIl?
For these types of situations, Excel 2010 allows you to create a custom AutoFill list that does
not need to increase or decrease by a fixed increment. With a custom AutoFill list, Excel will
automatically fill in the particular data items that you specify in your list, which can contain
words, text strings, or word/number combinations.
A custom AutoFill list will work the same way as default AutoFill lists:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 356
Creating a Custom AutoFill List
To create a custom AutoFill list, start by clicking File → Options:
In the Options dialog, choose the Advanced category:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 357
Now, scroll to the bottom of the main window and click Edit Custom Lists:
You will then see the Custom Lists dialog:
The “Custom lists” area contains AutoFill lists that already exist.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 358
In the “List entries” area, you can create a list by entering a series of words, abbreviations, or
text strings, separated by commas:
If you prefer, you can also press the Enter key after each word you type in the “List entries”
area so that each word is on its own line.
When you click Add, the custom list will be added to the “Custom lists” area on the left. There,
you will see the text or numbers that you just added, in a comma separated list.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 359
You can also select a range of cells from your worksheet and import them as a custom list. This
is a great way to create a custom AutoFill list from existing headings or labels in your
worksheet. You must remember, though, that the selected cells can only contain text (not
numbers).
To create a list using this method, just select the list items that you want from your worksheet,
open the Custom Lists dialog, and click Import. (You can also display the dialog first, place your
cursor in the Import box, and then select a range of data from the worksheet with your mouse.)
When the correct cell range is entered into the Import box, click Import to create a list:
Once you have finished creating custom lists, click OK to close the dialog.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 360
Using a Custom AutoFill List
You can use a custom AutoFill list in the same way that you would use the default AutoFill
feature. First, select a cell and enter the first term in your custom list. Next, select an adjacent
cell and type the second item in your list.
When you have entered enough data to allow Excel to identify your list, select the cells
containing the entries you have made. Place your mouse pointer on the small square in the
bottom right of the selection.
Your pointer should turn into a cross (+). Hold your mouse button down and drag over the cells
that you want to fill with your list items. When you reach the end of your list entries, the list will
be repeated from the beginning until you stop dragging:
You can just as easily AutoFill down columns as you can across rows.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 361
Modifying a Custom AutoFill List
You can make changes to a custom AutoFill list by invoking the Custom Lists dialog just as
before. If you select a list from the “Custom lists” field on the left, you will see the individual list
entries on the right:
In the “List entries” field, you can add new entries, remove individual items, or change existing
entries.
When you are finished modifying your custom list, click Add to implement the changes you
made. You should see the new, modified list replace the original one in the field on the left.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 362
Deleting a Custom AutoFill List
To delete a custom list, select it from the area on the left and click Delete:
You will see the following warning:
When you click OK in, the list will be removed from Excel. Be careful because there is no way of
recovering deleted lists! You will need to re-create it if you want to use it again.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 363
Lesson 4.4: Linking, Consolidating, and Combining Data
To properly manage your workbooks, you should be able to combine or consolidate data from
more than one workbook or worksheet. By doing this, you can create a complete and unified
picture of data even from information that may be in multiple places. In this lesson, you will
learn about linking and consolidating workbooks and worksheets.
Linking Workbooks
As you know, a cell reference in Excel (like “A7”) simply refers to the cell at the intersection of
column A and row 7. A cell reference like “Store!A7” refers to the cell at column A and row 7 in
the worksheet named Store. Taking this one step further, the reference “[Sales.xlsx]Store!A7”
refers to cell A7 in worksheet Store in the Sales workbook.
This kind of linking can be useful if you have several sheets that reference the same pool of
data. If you create links from the different sheets to the source data, the linked cells in the
other worksheets or workbooks will be updated automatically when you update the source
data. (It is important to note that both workbooks must be open at the same time for updating
to occur.)
To link two workbooks, open both of them. First, select source data from the source workbook.
Next, click Home → Copy. Now, switch workbooks and right-click on the cell in the destination
workbook that will contain the linked data. (If you are linking a range of data, right-click on the
top left cell of the destination range.)
Click Paste Link:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 364
Alternatively, click Paste Special and then click Paste Link in the dialog:
The linked data will now appear in the destination workbook. If you update the source data, the
linked data will be updated as well.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 365
Here are two different workbooks:
Cell F13 in the Workbook2 has been linked to cell B5 in Workbook1. As well, B3:B12 in the
Workbook2 has been linked to the range I2:I11 in Workbook1.
Consolidating Workbooks
When you consolidate workbooks, you are combining, condensing, and summarizing data from
multiple sources into one destination. For example, a large company may have sales or profit
data broken down into several different geographical locations and stored in different
workbooks. Each workbook would have the same layout and structure (template), but the data
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 366
in each will be different. With Excel, you can consolidate the different workbooks, summarizing
and totaling the data in one place to get a unified view of your information.
To consolidate workbooks in Excel, first open the workbooks, and then choose a destination
workbook where the data will be consolidated. It is a good idea to set up the destination
worksheet to be just like the worksheets that will supply the data, so that the destination
worksheet has the same labels and uses the same layout and cell locations for the same types
of data. (This is not mandatory, but it can make the consolidation process a lot easier.)
Below you can see three open workbooks: DestinationWorkbook, WorkBookSource1, and
WorkBookSource2. Notice that all three workbooks have a parallel structure in terms of
columns, rows, and labels:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 367
To consolidate the workbooks, we will select cell B2 in DestinationWorkbook (the workbook
with the empty cells) and choose Data → Consolidate:
This will display the Consolidate dialog:
The Function menu contains a variety of functions that you can use to combine data. In this
example, the default SUM function is used.
The Reference box is where you enter the ranges of data to be consolidated. You can enter the
ranges manually or by selecting them from the different workbooks with your mouse or
keyboard. (The latter method is recommended as it is much easier.) Once a range is entered,
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 368
click Add to add it to the “All references” box. This field will contain all the workbook cell
references that are to be consolidated.
For this example, we will click the Reference box and select B2:E14 from WorkbookSource1.
This range will be displayed in the Reference field in the Consolidate box. Clicking Add button
will add it to the “All references” box. We will then select the same range from
WorkBookSource2 and click Add. The Consolidate dialog now looks like this:
At the bottom of the dialog, you will notice the phrase “Use labels in,” with two checkboxes
below it:
You can check these boxes to consolidate sheets based solely on row labels or column labels.
That means that data in columns or rows that have the same labels will be consolidated even if
the labels are under different column letters or row numbers in the source workbooks. (This is
why consolidation can be is easier when all the worksheets involved have the same labels and
design layout!) For this example, the checkboxes are empty so the consolidation will be based
on the actual cell locations.
If you check the “Create links to source data” box, the information in the destination workbook
will be updated if the cell content in the contributing source workbooks is changed (like linking
the workbooks).
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 369
When we click OK, the source workbook data will be consolidated and combined into the
destination workbook:
Combining Worksheets
The methods for combining worksheets belonging to the same workbook are very similar to the
methods for combining different workbooks. You can link data between worksheets, or you can
consolidate worksheets.
To link cells in worksheets belonging to the same workbook, select the source cells, right-click,
and click Copy:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 370
Next, right-click on a cell in the destination worksheet (preferably at the upper left of the
destination range) and click Paste Link or Paste Special:
(If you click Paste Special, the Paste Special dialog box will appear. When it does, click Paste
Link. If you click the Paste Link command directly, you don’t have to do anything else.) The
linked data will now appear in the destination worksheet at the cell or range you specified:
Linking cells is one way of combining worksheet data. You can also consolidate multiple
worksheets belonging to the same workbook. Consolidating worksheets in the same workbook
is a lot like consolidating multiple different workbooks. First, set up your worksheets so they all
have a similar layout. Next, select a cell at the upper left of the destination range and click Data
→ Consolidate.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 371
In the Consolidate dialog, click in the Reference box, and then select a range of cells from
another worksheet in the workbook. Click Add to add it to the “All references” box:
Add any additional references as necessary. If you want to link the data in addition to
consolidating it (so that it is updated when changes are made to the source cells), select the
“Create links to source data” box:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 372
Once you are ready, click OK. Your worksheets will now be consolidated in the destination
range.
Pivoting Consolidated Data
To close off this lesson on linking and combining, we will now turn to the task of pivoting
consolidated data.
This method requires using the PivotTable and PivotChart Wizard, which is not accessible from
the ribbon. First, press Alt + D. This should display the following message:
Now, let go of Alt + D and press the “P” key (think “P” for PivotTable). This should open the
PivotTable and PivotChart wizard. To start, select the “Multiple consolidation ranges” radio
button. In this example, the “PivotChart report (with PivotTable report)” option is also selected.
This will automatically create a PivotChart and a PivotTable based on a consolidation of the
data ranges to be specified.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 373
Click Next. In this step of the wizard, you can have a single page field created automatically, or
you can choose to create your own page fields.
Since we are creating a PivotTable and PivotChart based on consolidated ranges, it might be
difficult to create page fields that are effective for both. Also, remember that only numerical
data can be consolidated, so any text labels that are not row labels or column labels in the
source data will not appear in the consolidated data. (You will see a column or row of zeros
instead of text.) However, for this example, we will accept the default option: “Create a single
page field for me.”
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 374
Once you are ready, click Next. You will now choose the ranges to consolidate:
This works just like the process used for standard data consolidation. First, click in the Range
box, and then select a range of data. Remember:
The data should be in a table-like format, with no empty columns.
The column labels and the row labels in the range will be carried over to the resulting
PivotTable, but any other text will not be.
All of the ranges should have the same basic structure and the same column and row
labels.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 375
When a range has been chosen, click Add to add the range to the “All ranges” area. The
following image shows the wizard dialog after four ranges have been added for consolidation.
This image shows the same four ranges of data as they appear in four separate worksheets:
Notice how the ranges have the same column and row headings, and the same table-like
structure.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 376
Once you are ready to proceed to the final stage of the wizard, click Next. In this step of the
wizard, you can choose to display the PivotTable report either in an existing worksheet, or a
new worksheet:
You can see that we have chosen a new worksheet. To create the PivotTable/PivotChart report,
click Finish. Here is the resulting PivotTable, PivotChart, and Field List for the consolidated data:
The PivotTable and the PivotChart reflect the sum of the corresponding data values in the
source worksheets. (The SUM function is the default option and cannot be changed in the
wizard.)
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 377
Because we have a PivotTable and PivotChart, we can use Slicers to help analyze the
consolidated data. To do this, click in the PivotChart and click Analyze → Insert Slicer:
For this example, we will select the Row and Column fields (which contain text labels):
Once we click OK we will see the Slicers:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 378
Now the consolidated data can be analyzed by clicking the various field labels in the Slicers. For
example, here is the Slicer arrangement to show the Production Capacity and the Units Shipped
for Product 2 and Product 5, and the results:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 379
Section 4: Review Questions
1. Why is VLOOKUP used more than HLOOKUP?
A. Because HLOOKUP is slow compared to VLOOKUP
B. Because you can only use HLOOKUP with numerical data
C. Because most often, worksheet data is logically arranged in vertical columns
D. There is no such thing as HLOOKUP
2. Which function might you use to calculate the eventual worth of an investment plan?
A. PMT
B. FV
C. An array formula
D. Any of the above
3. When using the FV or PMT functions with monthly payments and an annual interest
rate of 6%, you would use which of the following interest rate arguments?
A. .6
B. 6% * 12
C. 6%
D. 6%/12
4. Which three functions can be used to consolidate data?
A. Average, Correlation, Count
B. Sum, Future Value, Max
C. Sum, Average, Count
D. Average, Sum, PMT
5. Which error type will prevent the Sum function from working?
A. Division by zero
B. Value error
C. Both of the above
D. Neither of the above; the Sum function will work regardless of errors
6. What key combination do you use to enter a function or formula as an array formula?
A. Ctrl + Shift + Enter
B. Ctrl + Alt + Enter
C. Alt + D + P
D. Ctrl + A
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 380
7. What key combination do you use in Excel 2010 to invoke the PivotTable/PivotChart
wizard?
A. Ctrl + Shift + Enter
B. Alt + D and then P
C. Ctrl + P
D. Ctrl + C
8. What argument would you include in the VLOOKUP function to find an exact match to
the lookup key?
A. True
B. Exact
C. False
D. Find
9. How can you recognize an array formula in Excel?
A. {formula or function}
B. (formula or function)
C. [formula or function]
D. /formula or function/
10. Which of the following statements is true about custom AutoFill lists?
A. They do not need to change by a fixed increment
B. They can include text and text/number combinations
C. They work the same way as default AutoFill lists
D. All of the above
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 381
Section 5: Macros, Visual Basic, and Excel Programming
In this section you will learn how to:
Record, edit, and play a macro
Modify macro security settings
Use the Visual Basic Editor when working with macros
Add code and comments to macros
Use and record relative reference macros
Assign a keystroke to a macro
Copy a macro from a workbook or template
Declare variables
Iterate over a range
Prompt for user input
Use If, Then, and Else Statements
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 382
Lesson 5.1: Creating a Basic Macro
Like other applications in the Microsoft Office family, Microsoft Excel has the ability to use
macros. Macros are very useful if you perform a task repeatedly, as they are used to automate
processes and increase productivity. In this lesson, we will explore the basics of macros.
Recording a Macro
The first step in the creation of a macro is to record one. When a macro is being recorded, Excel
will store any action in the Excel window in the macro that you are creating. When you have
completed all the actions that need to be recorded, you will stop recording. Now you can run
this macro to repeat the recorded actions as necessary.
To record a macro, click View → Macros → Record Macro:
The Record Macro dialog should open:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 383
In this dialog, you can choose a name for the macro that you will be recording, where this new
macro will be stored, and add a description of its actions to it. When you are ready to record a
macro select the OK button. Once the OK button has been selected Excel will then record all of
your actions inside the Excel window.
Once you have completed all of the actions that you need included in the macro, select the
Macros drop-down command again, and click Stop Recording:
At this point the new macro will be complete.
Editing a Macro
Once a macro has been recorded, you can edit it easily in Excel. First, click the Macros drop-
down command, and then click View Macros:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 384
Then the Macro dialog should open:
Select the macro that you would like to edit from the list and click the Edit command on the
right:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 385
Then the selected macro will open in Microsoft Visual Basic for Applications. We will cover how
to work with this application later on in this section.
Running a Macro
Once a macro has been recorded, you can run it at any time by clicking the Macros command
directly on the View tab:
Selecting this will the launch the Macro dialog, in which you can choose the macro that you
would like to run. After you have chosen a macro, click the Run button:
The selected macro will then complete the tasks that you have previously recorded. As you can
see, this can greatly improve efficiency when working with long, repetitive tasks in Excel. With
one click you can complete dozens of steps in just a few seconds!
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 386
Understanding Macro Security
Excel includes several safeguards that help protect you against viruses that can be transmitted
through macros. One of the most important safeguards that Excel uses is the digital signature;
these are used so that users can verify that macros are from a trustworthy source. If you open a
workbook that contains macros, you can verify their source before enabling them. This can help
prevent virus infection as macros from untrustworthy sources are at high risk of containing
malicious code.
Instructor Tip: Microsoft Excel security settings may be applied at an administrative level.
Therefore, as a general user, you may not have access to these settings. If this is the case,
you must contact your network administrator to change these settings.
To access macro security settings, there are two methods that you can use. The first is to use
the Developer tab. This tab is disabled by default. To enable it, click File → Options:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 387
Then, navigate to the Customize Ribbon category and select the check box beside Developer:
Click OK to implement your changes. You can now access macro security settings by clicking the
Macro Security command inside the Code group:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 388
The second way to open this dialog is to click File → Options, and then click the Trust Center
category. Click the Trust Center Setting button:
The Trust Center dialog window will then be open on your screen. Select the Macro Settings
category if it has not already been selected. No matter which method you use to open this
window, it will look similar to this image:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 389
Inside this dialog, you want to concentrate on the Macro Settings section. Here there are four
radio buttons to choose from; each of these options handles macros in different ways.
These four buttons are arranged in descending order by most secure to least secure, with the
default setting being “Disable all macros with notification.” The default settings are a happy
medium between disabling all macros and enabling all macros.
If you disable all macros, you will not be able to use macros at all, losing any functionality that is
provided by them. However, enabling all macros is very dangerous; this setting will greatly
increase the risk of running malicious macros. The middle two options are generally the more
commonly selected.
The third option, “Disable all macros except digitally signed macros,” is a good choice. This will
run any macro that has been digitally signed by its author, but not any that are unsigned. This
ensures that the macro being run is from a trusted source.
Back inside the Trust Center dialog, you may have noticed a checkbox under the Developer
Macro Settings section:
This checkbox is used to control access to the VBA object model from any macro. By ensuring
that this box is deselected, you can prevent malicious code from self-replicating. If there is a
macro that requires access to this object model, this check box must be selected. By default it is
deselected, and is best left as such, unless in unique circumstances.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 390
Lesson 5.2: Visual Basic and Macros
In this lesson, we will delve deeper into macros with Visual Basic for Applications. We will first
go over the Visual Basic Editor. Then, we will explore how to add code and comments to your
macros.
Opening the Visual Basic Editor
Like many other functions in Excel, there are a number of ways to open Microsoft Visual Basic
for Applications (VBA). The most common method is to use the Macro dialog box, which can be
accessed by selecting View Macros inside the Macros drop-down command on the View tab:
When you have the Macro dialog open, you can then select the macro that you would like to
open in VBA, and then click the Edit button:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 391
The Visual Basic Editor will then open on your screen with the macro code available for editing:
Another way to open the editor quickly is to open the workbook that contains the macro that
you would like to edit, and then press Alt + F11 on your keyboard. Either method can be used
when you are ready to edit a macro in VBA.
Understanding the Visual Basic Editor
Once the Visual Basic Editor opens, it may be a little overwhelming at first, as it does appear
very complex. It includes a wide variety of options to edit your macro and a variety of different
views. For this lesson, we will only focus on specific components inside VBA that are used to
create and edit your macro.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 392
When the Visual Basic Editor is opened while using Excel, you will see some code inside the
workspace (as long as the workbook contains a macro):
This is the code for the macro in the workbook that you have open in Excel. In this example, it is
fairly short as the macro completed a simple function. (The more complex a macro is, the
longer the code.)
You can view the whole workbook by looking inside the Project pane at the top left of the
window:
As you can see, there is an entry for each sheet inside the workbook, under the Microsoft Excel
Objects folder. Any macros that are in this workbook are listed under the Modules folder. You
can open any of these objects by double-clicking on them within the pane.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 393
The area that we will be focusing on will be the workspace inside the editing window, as this is
where any new code is added or existing code edited. It should be noted that you do not need
to save modified code; it will be saved when the workbook itself is saved using Excel.
Adding Code to your Macro
Adding code to your macro is a fairly straight forward process when using the Visual Basic
Editor. You need to only place your cursor to where you would like the code to be inserted and
enter it. When you are done, close the editor window and the new code will be applied.
VBA allows you much more control over your macros, as it allows you to tailor its underlying
code to your needs. To edit existing code, you will require some familiarity with Visual Basic
code syntax, but we will cover some of the basic elements of VB code in the next lesson.
Adding Comments to Visual Basic Code
If you have used any type of computer code language before, then you should be familiar with
comments. Comments can be thought of as notes that are added to the code. These notes can
contain whatever you would like, such as reminders and notices to those other than yourself
who may modify this code.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 394
You have probably already seen comments in Visual Basic yourself, but you may not have
realized it. Comments appear as green text inside the VBA workspace and are preceded by an
apostrophe (‘):
When an apostrophe has been placed at the start of a line, anything on that line will become a
comment and not recognized by VBA as code, so it will be ignored. This means that if you would
like to disable a line of code, but not remove it, you can comment it out. Just place an
apostrophe at the beginning line of that code:
It should be noted that when creating a comment line it will not change to green until you
move your cursor off of that line.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 395
Lesson 5.3: More Macro Tasks
There are a number of more advanced settings that can be taken advantage of when creating a
macro. In this lesson, we will cover relative reference macros, as well as how to copy macros
and assign keystrokes.
What are Relative References?
When you use Excel to record a macro it will record only what you did, not how you did it. This
works for most types of macros that you will be creating, but not all. What do you need to do if
you wanted a macro to record in more detail?
Imagine that you would like to move from cell A1 to cell C1; you can do this by pressing the
right arrow key on your keyboard. By default Excel will not record that this key was used;
instead, it only records that C1 has been selected.
Now imagine that you have recorded these actions, but you would like to use this macro on A2
so that it would select C2. However, when the macro is executed, C1 will be selected. This is
because instead of recording that you moved two cells to the right, it only knows that C1 has
been selected.
To remedy this issue, Excel includes the ability to record macros relatively. If we recorded the
macro created above using relative references, it would know to move to the right of any cell
selected, instead of blindly selecting C1 all the time.
Recording a Relative Reference Macro
If you would like to record a macro relatively, relative references need to be enabled. You can
do this by selecting “Use Relative References” from the Macros drop-down command on the
View tab:
© 2005-2011 Velsoft Training Materials, Inc.