Microsoft Office Excel 2010 Advanced 396
You are able to see when Relative References has been enabled by a slight border around the
icon:
When Relative References has been enabled, you can then record your macro just as if you
would record any other: by selecting Record Macro from the Macros drop-down command.
When you are done recording this macro, select the Stop Recording option from the same
drop-down.
The only difference is that everything you do will be relative, so you cannot make changes to a
specific cell.
Running a Relative Reference Macro
Running a relative reference macro is slightly different than running a regular macro. You need
to be aware of what cell you have selected, as everything that the macro will do will be relative
to that cell. When you have the proper cell selected, you can then go through the process of
selecting the Macros drop-down command, selecting View Macros from the menu, choosing
the macro that you would like to use, and then clicking Run.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 397
The key is to always remember that the macro will run relative to your starting cell. In this
example the macro that was used turns the fourth cell from the right of the starting cell yellow:
The red boxes denote the starting point from which the macro was run. As you can see, it’s all
relative to the starting position.
Assigning a Keyboard Shortcut to a Macro
One of the more useful settings to keep in mind when creating a macro with Excel is the ability
to assign a shortcut key to your macro. This allows you to use macros even faster by bypassing
the Macros drop-down command and using a keyboard shortcut to run a macro.
You can assign a shortcut key when creating a macro or after the fact. When creating a macro,
you may have noticed a field labeled “Shortcut key” inside the Record Macro dialog box:
Inside this field you can type any letter that you like. Once a letter has been entered, you can
run this macro by pressing and holding Ctrl and then tapping the letter that you have chosen.
Be mindful that if you insert a letter that is already used in a standard Excel keyboard shortcut,
such as Undo (Ctrl + Z), it will be overwritten by this shortcut.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 398
You may also apply a shortcut to an existing macro by opening the Macro dialog box and then
clicking on the Options button:
The Macro Options dialog box should then open:
As you can see, this dialog box has the same Shortcut key field as you saw previously when
creating a new macro. Simply type the letter that you would like to assign to this new shortcut
and click OK to apply it.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 399
Copying a Macro from a Workbook or Template
Unfortunately, there is no simple way to copy a macro from a workbook or template within
Excel itself. You must use the Visual Basic Editor, which is included with Microsoft Office.
First, ensure that the workbook with the macro that you want to copy is open in Excel. With the
right workbook open, press Alt + F11 on your keyboard. (This is a great shortcut to remember
as it is a quick way to open the Visual Basic Editor.) When the editor is open, you should see a
screen similar to this:
Next, place your cursor inside the workspace containing the code and press Ctrl + A on your
keyboard to select all the code. Once all the code is selected, press Ctrl + C to copy it.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 400
Now open the workbook that will use the copied macro. You should now see that Excel
workbook available in the original VBA window, inside the Project pane on the left. Select it to
expand its contents:
Because there is no macro on this workbook, you need to create a module to hold the one that
we are copying.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 401
Click the Insert menu and then click Module from the list:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 402
With the new module created, the workspace will select it. It will be blank as there is no code
inside it yet. Right-click inside the workspace and select Paste:
The code that you copied from the previous workbook should now be pasted inside the current
workspace. You can now press Alt + F11 again to close the VBA window.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 403
Now, click View → Macros to verify that the operation worked. You should see the copied
macro inside the Macro window, ready to be used:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 404
Lesson 5.4: Advanced Visual Basic Tasks
Now that we have covered the basics of how to use Visual Basic for Applications, we are going
to examine it in more detail. In this lesson, we are going to focus on the code used for macros in
Excel. By the end of this lesson you should have a good idea of how to declare variables and
much more.
Declaring Variables
Simply put, variables are containers that are used to store values (data). In Excel, this variable
can be declared as any type, describing the kind of values that they hold. For example, a
variable that has been declared as an integer can only contain numerical digits, without decimal
points. There are a number of other data types to choose from, including string, Boolean, and
date data types.
Here is a basic example of declaring a variable in Microsoft Visual Basic for Applications:
This example can be broken down like this. The first line “Dim x As Integer,” creates a variable
“x,” and then declares it as an Integer data type. (Remember that an integer data type is
applied when only whole numbers are used.)
The second line, “x = 2,” is where the variable is assigned a value.
The third line states that the value of cell A1 on the workbook will be that of “x,” or by
extension, 2.
Iteration over a Range
When creating a macro, you may have requirements to insert a particular value amongst a
range of cells on the Excel worksheet. You wouldn’t want to do this manually as it is quite time
consuming, but luckily a macro can be made to do that quite easily! Let’s look at the code for
such a macro:
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 405
As you can see, you only need just a few lines to complete this macro. Let’s focus on the first
line of code:
You will recognize a few items on this line. First, a variable has been declared (cell). The
remainder of this line is telling the macro to look inside the Sheet1 worksheet, between cells A1
and F5. To complete this, a Range attribute has been used, which will select all cells between
the first one, and the second.
The next line defines the values for all cells within the previously mentioned range as 123456:
The result will look like the image below:
As you can see, this can be a very powerful tool. By mastering ranges you can greatly increase
the speed at which Excel spreadsheets are completed.
Prompting for User Input
Excel has the built-in ability to prompt the user for input using macros. This will usually take the
form of a dialog box that will appear in the middle of the user’s screen. Inside this dialog box,
the user will be able to enter information which will be used with the current workbook.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 406
Let’s look at a basic example of a user input dialog:
For this example, suppose that this dialog will appear when a particular macro is run. The user
would then enter any number and click the OK button. Then, the number inside this dialog will
be placed as the value inside a specific cell on the current Excel sheet:
In this case, 42 has been added as a value for cell C1.
Now let’s examine the underlying code that controls user input when coding macros in VBA.
As you can see, there is not much to it. The first line defines a variable and a variable type,
which in this case is an integer. The next line is what controls the user input:
This line is declaring that the “Number” variable is whatever number is entered inside the input
box. The InputBox section of this line controls the dialog itself. Using InputBox you can add text
inside the box (Please enter a number), as well as header text for the dialog (A number is
required).
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 407
The next line is telling the macro where the information entered into the input box will be
placed. In this case, C1 has been chosen as this cell:
The last line in this chunk of code declares that the current cell that has been selected (which
was declared in the previous line) will contain whatever value is held by the Number value:
Using If, Then, and Else Statements
The most commonly-used statements that you will see when working with macros are the IF,
Then, and Else statements. These types of statements are usually called conditional
statements. They are called as such because they are used to execute code when specific
conditions have been met.
For example, if you were working with a worksheet that contained numerical employee
performance test results, you can create a conditional statement that will display if the
employees scored poorly, good, or very good.
Let’s examine some code that will do part of what the above example describes.
As with the other macros that we have covered so far, the top part of the code is used to
declare any variable types that are required for this macro. We would like to concentrate on
the lines below these declarations:
This line declares a value of the performance variable. This is the variable that will hold the
numerical grade that has been entered into cell A1.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 408
Now let’s start examining the If, Then statement on the next line:
The initial portion of this line states that if the value that has been pulled from the workbook
and stored in the “performance” variable is equal to or greater than 80, then this section of
code will run. This is where the Then portion of this statement comes into to play. If a score of
80 or higher has been entered as a value, then the value for the result variable will become
“very good.”
The last portion of this code will insert the value of the result variable into cell B1:
The Else statement is not found in this example because it works like the Then statement, just
in an opposite fashion. For example, let’s return to the employee performance test results
example. You can create a statement that will run if the value found in a range is greater than
or equal to 80. These values will display “very good,” whereas everything else will display “fail:”
By simply adding Else to the end of an If, Then line, you have created an Else statement. If you
do use an Else statement, you should enter End If below this line.
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 409
Section 5: Review Questions
1. What is relative reference used for?
A. To run a macro on a specifically selected cell
B. To run a macro in relation to what cell has been selected
C. To run a macro that is in reference to another
D. None of the above
2. What syntax does Microsoft Visual Basic for Applications use?
A. C++
B. ASP
C. Java
D. Visual Basic
3. What occurs when you record a macro?
A. All steps taken by you in the Excel window are played back
B. Whatever you type in the Excel window is recorded
C. All steps taken by you in the Excel window are recorded
D. All of the above
4. What color are comments in Visual Basic for Applications?
A. Red
B. Blue
C. Yellow
D. Green
5. What does it mean to assign a keystroke to a macro?
A. To assign a shortcut (e.g. Ctrl + E)
B. To add another step to the macro
C. To add an action to the macro
D. To record using relative reference
6. What is the keyboard shortcut to open VBA?
A. Ctrl + F4
B. Ctrl + V
C. Alt + F11
D. Alt + F12
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 410
7. To run a macro, what are the steps that you need to follow?
A. Click the Macro drop-down command, click View Macros, click the macro from the
list, and click Run
B. Click the Macro drop-down command, Click View Macros, click the macro from the
list, and click Edit
C. Click the Macro drop-down command, Click View Macros, click the macro from the
list, and click Step Into
D. Press Alt + F11
8. What is one of the key components of macro security in Excel?
A. Data types
B. Save settings
C. Digital signatures
D. All of the above
9. What best describes a variable?
A. A value
B. A container
C. A data type
D. An attribute
10. Which of these data types are valid in VBA?
A. String
B. Boolean
C. Date
D. All of the above
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 411
Index
Add-Ins .........................................................................................................See Data Analysis Tools
Analysis ToolPak.......................................................................................30, 35, 40, 41, 43, 45, 101
Array Formula ..................................................................................... 329, 344, 345, 352, 353, 379
AutoFill List
Creating a custom list ............................................................................................................. 358
Creating from imported data.................................................................................................. 359
Editing ..................................................................................................................................... 361
Using ....................................................................................................................................... 360
What is? .................................................................................................................................. 355
Classic PivotTable
Adding fields ........................................................................................................................... 190
Pivoting data ........................................................................................................................... 192
Removing fields............................................................................................................... 192, 195
Setting PivotTable Layout ....................................................................................................... 188
Combining Worksheets............................................................................................................... 369
Consolidating Workbooks ........................................................................................................... 365
Consolidation options ............................................................................................................. 368
Pivoting consolidated data ............................................................................................. 372, 376
Data Analysis Tools
Anova tool................................................................................................................................. 36
Correlation tool......................................................................................................................... 37
Default add-ins.......................................................................................................................... 34
Enabling..................................................................................................................................... 32
Help ........................................................................................................................................... 44
Random Number Generation tool ............................................................................................ 40
Rank and Percentile tool........................................................................................................... 41
Solver tool ................................................................................................................................. 50
Data Tables
One input .................................................................................................................................. 50
Specifying one input ................................................................................................................. 54
Specifying two inputs................................................................................................................ 57
Two input .................................................................................................................................. 50
What are?.................................................................................................................................. 50
Euro Currency Tools................................................................................................................ 45, 48
Excel Options Dialog ........................................................................................................... 386, 388
Function Arguments Dialog .................................................................................................. 98, 349
FV Function
Arguments............................................................................................................................... 333
Using ....................................................................................................................................... 334
What is? .................................................................................................................................. 333
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 412
Goal Seek..................................................................................................3, 61, 62, 63, 65, 100, 101
Computing goals ....................................................................................................................... 63
Parameters................................................................................................................................ 62
With Solver................................................................................................................................ 77
Grouping ......................................................................................................................................... 4
Manual grouping....................................................................................................................... 10
Removing grouping ................................................................................................................... 12
Show/hide data........................................................................................................................... 7
HLOOKUP Function ..................................................................................................... 329, 346, 379
What is? .................................................................................................................................. 346
Hyperlinks
Alternate text............................................................................................................................ 83
Hyperlink function .................................................................................................................... 97
Images as hyperlinks ................................................................................................................. 99
Insert Hyperlink dialog.............................................................................................................. 81
Inserting .................................................................................................................................... 80
Place in document .................................................................................................................... 86
Removing .................................................................................................................................. 97
ScreenTip................................................................................................................................... 85
Styling........................................................................................................................................ 90
Using ......................................................................................................................................... 94
What are?.................................................................................................................................. 79
Insert Function Dialog................................................................................................... 97, 348, 349
Logic Functions............................................................................................................................ 334
AND ......................................................................................................................................... 334
AND truth table....................................................................................................................... 335
FALSE....................................................................................................................................... 336
IF 337
IFERROR................................................................................................................................... 337
IFERROR and array formulas................................................................................................... 341
NOT ......................................................................................................................................... 335
NOT truth table ....................................................................................................................... 336
OR............................................................................................................................................ 335
OR truth table ......................................................................................................................... 335
TRUE........................................................................................................................................ 336
Using ....................................................................................................................................... 339
Macro
Adding code to ........................................................................................................................ 393
Assigning shortcut key to........................................................................................................ 397
Commenting............................................................................................................................ 394
Copying between workbooks ................................................................................................. 399
Editing ............................................................................................................................. 383, 390
Macro Dialog........................................................................................................... 384, 390, 398
Prompting for input ................................................................................................................ 406
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 413
Record Macro dialog....................................................................................................... 382, 397
Recording ................................................................................................................................ 382
Relative reference........................................................................................... 381, 395, 396, 409
Running ................................................................................................................................... 385
Security ................................................................................................................................... 386
Microsoft Office Access 2010 .94, 96, 102, 105, 201, 202, 203, 221, 222, 229, 230, 232, 233, 303,
306, 307, 320, 328
Microsoft Office Word 2010 ......................................................................1, 33, 79, 80, 94, 95, 328
Microsoft Visual Basic for Applications ........................................................ See Visual Basic Editor
Outlining................................................................................................................................ 4, 6, 10
Applying ...................................................................................................................................... 5
Clearing ....................................................................................................................................... 7
PivotChart
Adding data............................................................................................................................. 243
And PowerPivot ...................................................................................................................... 319
Chart types.............................................................................................................................. 241
Creating................................................................................................................................... 240
External data source ....................................................................................................... 302, 307
Pivoting data ........................................................................................................... 245, 249, 252
Scenario PivotChart......................................................................................................... 312, 316
Slicer................................................................................................................................ 309, 311
Slicer Connections................................................................................................................... 312
What are?................................................................................................................................ 239
PivotChart Formatting
Axes titles ................................................................................................................................ 297
Chart background.................................................................................................................... 288
Chart layout............................................................................................................................. 295
Chart title ................................................................................................................................ 296
Chart type ....................................................................................................................... 275, 278
Choosing the right chart type ................................................................................................. 278
Data tables .............................................................................................................................. 300
Editing shapes ......................................................................................................................... 285
Gridlines .................................................................................................................................. 301
Legend..................................................................................................................................... 297
Renaming fields....................................................................................................................... 272
Styles ....................................................................................................................................... 281
PivotChart Tools
Analysis ................................................................................................................................... 262
Chart backgrounds .................................................................................................................. 262
Chart layouts ........................................................................................................................... 256
Chart styles.............................................................................................................................. 256
Chart types.............................................................................................................................. 253
Insert object ............................................................................................................................ 259
Labels ...................................................................................................................................... 259
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 414
Shape styles ............................................................................................................................ 263
Size .......................................................................................................................................... 267
Slicer........................................................................................................................................ 269
WordArt .................................................................................................................................. 264
PivotTable
Adding/removing fields........................................................................................................... 117
Changing source data.............................................................................................................. 112
Changing to classic layout....................................................................................................... 188
Clearing ................................................................................................................................... 112
Creating................................................................................................................................... 104
Creating from external data.................................................................................................... 196
Creating in PowerPivot ........................................................................................................... 231
Data source ............................................................................................................................. 105
Defining fields ......................................................................................................................... 106
Defining range......................................................................................................................... 106
Expand/collapse data...................................................................................................... 134, 135
External data warning............................................................................................................. 205
Field List .................................................................................................................................. 117
Field Settings........................................................................................................................... 110
Importing data ........................................................................................................................ 200
Managing external data connections ..................................................................................... 199
Naming.................................................................................................................................... 109
Pivoting data ........................................................................................................................... 128
PivotTable Options dialog ....................................................................................................... 182
PivotTable Tools tabs .............................................................................................................. 108
Refreshing data ............................................................................................................... 112, 205
Report Filter box ..................................................................................................................... 124
Selecting elements.................................................................................................................. 112
Show Values As ....................................................................................................................... 113
Show/hide elements ............................................................................................................... 114
Summarizing values ................................................................................................................ 113
Using Access data............................................................................................................ 201, 203
What is pivoting? .................................................................................................................... 123
What is? .................................................................................................................................. 103
PivotTable Data
AutoSort .................................................................................................................................. 145
Changing source data.............................................................................................................. 154
Clearing filters ......................................................................................................................... 138
Filtering data ................................................................................................................... 136, 137
Group Selection ...................................................................................................................... 149
Grouping ................................................................................................................................. 148
Label Filters ............................................................................................................................. 138
Refreshing data ............................................................................................................... 150, 153
Sort By Value dialog ................................................................................................................ 147
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 415
Sorting data..................................................................................................................... 143, 144
Ungrouping ............................................................................................................................. 150
Value Filters ............................................................................................................................ 139
PivotTable Formatting
Automatic subtotals................................................................................................................ 159
Compact form ......................................................................................................................... 161
Data calculations..................................................................................................................... 168
Field Settings........................................................................................................................... 158
Grand Totals............................................................................................................................ 175
Manual formatting.................................................................................................................. 180
Number formatting................................................................................................................. 170
Report Layout.......................................................................................................................... 175
Subtotals ................................................................................................................................. 173
Table layouts ........................................................................................................................... 171
Tabular form ........................................................................................................................... 162
PMT Function
Arguments............................................................................................................................... 330
What is? .................................................................................................................................. 330
PowerPivot......1, 102, 196, 218, 219, 220, 222, 226, 229, 230, 231, 236, 238, 302, 319, 323, 324,
326, 328
Creating PivotTable................................................................................................................. 231
Creating relationships ..................................................................................................... 230, 231
Downloading and installing..................................................................................................... 218
Importing data ........................................................................................................ 221, 226, 228
Opening................................................................................................................................... 221
Refreshing data ....................................................................................................................... 235
System requirements.............................................................................................................. 218
Viewing data ........................................................................................................................... 234
Scenarios
Choosing a scenario .................................................................................................................. 22
Creating..................................................................................................................................... 19
Creating with Solver.................................................................................................................. 74
Creation options ....................................................................................................................... 20
Editing ....................................................................................................................................... 23
Merge details ............................................................................................................................ 26
Merging..................................................................................................................................... 24
Scenario Manager ............................................................18, 19, 22, 23, 24, 26, 27, 28, 313, 314
Summary report........................................................................................................................ 28
Using multiple ........................................................................................................................... 22
What are?.................................................................................................................................. 17
Selection Pane..................................................................................................................... 216, 267
Slicer.................................................1, 102, 206, 209, 213, 214, 238, 269, 309, 325, 326, 377, 378
Arranging................................................................................................................................. 215
Changing PivotTable association ............................................................................................ 214
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 416
Formatting .............................................................................................................................. 217
Inserting .................................................................................................................................. 206
Moving ............................................................................................................................ 208, 209
Settings.................................................................................................................................... 213
Styles ....................................................................................................................................... 214
Using multiple Slicers.............................................................................................................. 212
With scenario PivotCharts ...................................................................................................... 317
Solver ......................... 3, 30, 34, 35, 45, 50, 65, 66, 67, 68, 71, 72, 73, 74, 75, 76, 77, 78, 100, 101
Adding constraints .................................................................................................................... 70
And scenarios............................................................................................................................ 74
As advanced goal seek tool....................................................................................................... 77
Changing parameters................................................................................................................ 76
Defining constraints .................................................................................................................. 66
Enabling..................................................................................................................................... 65
Executing the operation............................................................................................................ 73
Generating reports.................................................................................................................... 74
Identifying goal ......................................................................................................................... 66
Modifying values....................................................................................................................... 75
Objective formula ..................................................................................................................... 67
Results dialog ............................................................................................................................ 73
Setting changing cells................................................................................................................ 69
Setting constraints .................................................................................................................... 70
Setting objective ....................................................................................................................... 69
Solver Parameters dialog .......................................................................................................... 69
Solving methods........................................................................................................................ 76
What is? .................................................................................................................................... 65
Subtotals
Adding ....................................................................................................................................... 13
Grouping and ............................................................................................................................ 16
Options...................................................................................................................................... 14
Removing .................................................................................................................................. 16
Trust Center ........................................................................................................................ 388, 389
Visual Basic
Code snippets.......................................................................................................................... 407
Comments............................................................................................................................... 394
Conditional statements........................................................................................................... 407
ELSE statements...................................................................................................................... 407
IF statements .......................................................................................................................... 407
Input Box ................................................................................................................................. 405
Iteration .................................................................................................................................. 404
Restricting access to object model ......................................................................................... 389
THEN statements .................................................................................................................... 407
Variables.................................................................................................................................. 404
Visual Basic Editor....................................................................................... 381, 390, 391, 393, 399
© 2005-2011 Velsoft Training Materials, Inc.
Microsoft Office Excel 2010 Advanced 417
Keyboard shortcut for............................................................................................................. 391
Opening................................................................................................................................... 391
Project pane ............................................................................................................................ 392
Viewing objects....................................................................................................................... 392
Workspace .............................................................................................................................. 392
VLOOKUP Function ............................................. 329, 346, 347, 348, 349, 350, 352, 353, 379, 380
As an array formula......................................................................................................... 352, 354
Finding an approximate match ............................................................................................... 350
Finding an exact match ........................................................................................................... 349
Using ....................................................................................................................................... 347
What is? .................................................................................................................................. 346
What-If Analysis .......................................................................................... 18, 26, 28, 54, 100, 313
Data Table ................................................................................................................................. 59
Goal Seek................................................................................................................................... 62
© 2005-2011 Velsoft Training Materials, Inc.