Excel07pivot¯o PDF
Excel07pivot¯o PDF
Pivot Tables
& Macros
1
1
1
2
3
4
5
6
7
7
8
8
9
9
EXERCISE 1
11
EXERCISE 2
15
17
17
19
20
21
WORKSHOP DESCRIPTION
Overview
The first leg of the Excel Workshop series was designed primarily for those who had little
experience with Excel. Participants were able to flavor the basics of a spreadsheet, create and
modify worksheets, format and enhance worksheets, manage workbooks and setup their work in
a presentable fashion.
The second leg of the series was designed to cover topics such as conditional formatting,
creating templates, formulas and functions, charting techniques, mail merge and creating lists.
This the conclusion of this series designed primarily for Pivot Tables and Macros. To start off
we will cover a general know how on Pivot Tables, followed by creation of a Pivot Table and a
Pivot Chart. Later on the workshop concludes with recording macros, macro menus and global
macros.
Prerequisites
Excel 2007 Basics (optional)
Excel 2007 Formatting (optional)
Objectives
Sample Example
The best way to understand the concept of a
PivotTable is to see an example.
The example here shows a list of sales figures
spread across two countries.
The list is organized by:
Country,
Salesperson,
Order Date,
OrderID and
Order Amount.
The PivotTable for the same data looks something like this:
The Salesperson column is positioned along the row axis, the Order Amount makes the body of
the table while the Country is set as the selector on the top. The PivotTable shown here makes it
easy to find almost all the information from the sample data shown before. The Order Date and
OrderID are not visible in the PivotTable however can be added in if required.
The dropdown option for the Country category allows you to view the data for a specific
country. Likewise you can narrow down the option for a particular Salesperson as well.
PivotTable Terminology
Understanding the terminology associated with pivot tables is the first step in mastering this
feature.
Column Field: Fields of data
Data Area:
Grand Totals:
Item:
Group:
Collections of Items
Page Field:
Fields of data
Refresh:
Row Field:
Fields of data
Source Data:
Sub Totals:
Creating a PivotTable
Here well create a PivotTable from the sample list
(Salespeople_Example.xls) from the Workshop Series 3
folder.
To create a PivotTable from an Excel source,
Pivoting a PivotTable
To pivot, or rearrange, a PivotTable, drag one or more field headings. To move a field from the
column axis to the row axis, for example, all you have to do is drag its heading from the column
area to the row area.
In addition to transposing columns and rows, you can change the order in which fields are
displayed on the column or row axis. For example, you can drag a heading to the left.
Refreshing a PivotTable
PivotTables are not updated each
time a change occurs in their source
data.
To manually update a table, select
any cell in the table and choose
Options, Refresh on the Ribbon.
If you want Excel to refresh your
PivotTable every time you open
the workbook in which it resides,
choose from the Ribbon, Options,
Pivot Table Options. Then select
the Refresh Data When Opening the
File check box in the PivotTable
Options dialog box under Data
tab. If you want to prevent Excel
from updating the table each time
you open the workbook, be sure
this check box is cleared.
Removing Groups
To remove any group and restore a field to its ungrouped state:
select a grouped item, choose Options > Ungroup.
Creating a PivotChart
You can create a PivotChart by choosing PivotChart under PivotTable Tools, Options and
Tools.
Notice that you can rearrange a PivotChart exactly as you would a PivotTableby dragging
field headings from one axis to another. To add fields, drag them from the PivotTable Field List.
To remove fields, drag them off the chart. To limit the display to particular items in a field,
select those items in the fields drop-down list.
10
EXERCI SE 1
PIVOTTABLE & PIVOTCHART
In this exercise, you will practice creating a PivotTable and a PivotChart by using
the techniques learned till now.
1. From the Workshop Series 3 folder on your desktop, open the file named
exercise1.xls.
2. Create a PivotTable that can give you an up-to-date information on the total
expenses for the various Funds.
3. Summarize the fund distribution for each vendor.
4. Save the file.
5. Next, create a PivotChart on the data from the same file exercise1.xls.
6. Save the PivotChart as a new Worksheet
7. Save your file (using the same filename).
11
WHAT IS A MACRO
A macro is a set of instructions that tells Microsoft Excel to perform one or more actions for
you. Macros are like computer programs, but they run completely within Excel. You can use
them to automate tedious or frequently repeated tasks.
Macros can carry out sequences of actions much more quickly than you could yourself. For
example, you can create a macro that enters a series of dates across one row of a worksheet,
centers the date in each cell, and then applies a border format to the row. Or you can create a
macro that defines special print settings in the Page Setup dialog box and then prints the
document.
Macros can be simple or extremely complex. They can also be interactive; that is, you can write
macros that request information from the user and then act on that information.
There are two ways to create a macro: You can record it, or you can build it by entering
instructions in a module. Either way, your instructions are encoded in the programming
language Microsoft Visual Basic for Applications (VBA).
Recording a Macro
Rather than type macros character by character, you can have Excel create a macro by recording
the menu commands, keystrokes, and other actions needed to accomplish a task. After youve
recorded a series of actions, you can run the macro to perform the task again. As you might
expect, this playback capability is most useful with macros that automate long or repetitive
processes, such as entering and formatting tables or printing a certain section of a worksheet.
When you record a macro, all steps that are needed to complete the actions that you want to
record are recorded by the macro recorder. Navigation on the Ribbon is not included in the
recorded steps.
NOTE : The Ribbon is a component of the Microsoft Office Fluent user interface.
step 1 . If the Developer tab is not available, do the following to display it:
a. Click the Microsoft Office Button, and then click Excel Options.
12
b. In the Popular category, under Top options for working with Excel, select the Show
Developer tab in the Ribbon check box, and then click OK.
step 2 . To set the security level temporarily to enable all macros, do the following:
a. On the Developer tab, in the Code group, click Macro Security.
13
b. Under Macro Settings, click Enable all macros (not recommended, potentially
dangerous code can run), and then click OK.
NOTE : To help prevent potentially dangerous code from running, we
recommend that you return to any of the settings that disable all macros after you
finish working with macros.
step 3 . On the Developer tab, in the Code group, click Record Macro.
step 4 . In the Macro name box, enter a name for the macro.
NOTE : The first character of the macro name must be a letter. Following
characters can be letters, numbers, or underscore characters. Spaces are not allowed
in a macro name; an underscore character works well as a word separator. If you use
a macro name that is also a cell reference, you may get an error message that the
macro name is not valid.
step 5 . To assign a CTRL combination shortcut key to run the macro, in the Shortcut key
box, type any lowercase letter or uppercase letter that you want to use.
NOTE : The shortcut key will override any equivalent default Excel shortcut key
while the workbook that contains the macro is open. For a list of CTRL
combination shortcut keys that are already assigned in Excel, see Excel shortcut and
function keys.
step 6 . In the Store macro in list, select the workbook in which you want to store the macro.
14
TIP : If you want a macro to be available whenever you use Excel, select
Personal Macro Workbook. When you select Personal Macro Workbook, Excel
creates a hidden personal macro workbook (Personal.xlsb) if it doesn't already
exist, and saves the macro in this workbook. In Windows Vista, this workbook is
saved in the C:\Users\user name\AppData\Local\Microsoft\Excel\XLStart
folder. In Microsoft Windows XP, this workbook is saved in the C:\Documents
and Settings\user name\Application Data\Microsoft\Excel\XLStart folder.
Workbooks in the XLStart folder are opened automatically whenever Excel
starts. If you want a macro in the personal macro workbook to be run
automatically in another workbook, you must also save that workbook in the
XLStart folder so that both workbooks are opened when Excel starts.
step 7 . To include a description of the macro, in the Description box, type the text that you want.
step 8 . Click OK to start recording.
step 9 . Perform the actions that you want to record.
step 10. On the Developer tab, in the Code group, click Stop Recording.
TIP : You can also click Stop Recording on the left side of the status bar.
Creating a Macro
Lets create a simple macro that inserts your Department name and University Logo in a
worksheet.
15
step 1 . Begin by opening a new file and saving it to the desktop as Macro.xls.
step 2 . Choose Developer, Code, and Macro which then displays the Record Macro dialog
box.
step 3 . Assign a name to the macro. The default is Macro1 or you can enter your own name.
Lets use UniversityLogo. Note that this name cannot have any spaces.
step 4 . Assign a key combination to the macro by entering a letterin this case, uppercase
L in the Shortcut key edit box.
step 5 . Store the macro in the currently active workbook by making sure the This
Workbook option is selected.
step 6 . Enter a description for the macro in the Description box; in this case, type Inser t
University Logo.
step 7 . To begin recording, click OK. Excel displays the message Recording in the status
bar.
step 8 . Select Page Layout > Page Setup dialog box.
step 9 . Select the Margins tab and change the Top Margin to 1.75.
step 10. Select the Header/Footer tab and click the Custom Header button.
step 11. To the Left section, add the CSUS Logo, to the Center section add the University
Name and your Department Name. Click OK
step 12. Similarly add the Page number to the left section and File path to the right section
of the Footer. Then click OK.
step 13. Click OK to complete Page Setup.
step 14. Click the Stop Recording Macro button on the Developer tab..
step 15. This step is IMPORTANT; if you dont stop the macro recorder, Excel continues
to record your actions indefinitely.
step 16. To test the new macro, open a new worksheet. Type your name in cell A1 and then
press Ctrl+Shift+L. Excel runs the macro and performs the sequence of actions in
the same way you recorded them. Do a Print Preview!!
16
step 1 . Choose Developer, Code, and Macros to display the dialog box.
step 2 . Select the name of the macro, and click Run.
step 3 . You also can use the Macro dialog box to view and edit macros, as youll see in the
next section.
17
As you can see, a module looks like a window that you might see in a word processing program.
The menu bar above the module includes menus for editing, debugging, and running VBA code.
In the module you can review, enter, copy, move, insert, and delete VBA statements and
comments using techniques that are similar to those you use in a word processing program. The
VBA environment is a big place, full of interesting details, but for now lets focus only on the
code weve recorded.
On the right side of the VBE is a window displaying the module containing your code. The first
and last lines of the code act as the beginning and endpoints for the macro youve recorded; a
Sub statement starts the macro and an End Sub statement ends the macro. Youll notice that
special VBA terms, called keywords, are displayed in dark blue.
Here is an explanation
SUB: This is a keyword that begins a Macro and is followed by the name of the
Macro.
COMMENTS: The comments are entered following an Apostrophe. VB ignores
the apostrophe.
STATEMENTS: Each action carried out by the Macro is written in statements.
The syntax of a statement is an Object followed by the Action. Here are some
examples:
RANGE(A6).Select: The Object is the range reflecting cell A6 and the Action
taken is Select.
SHEETS(3).ACTIVATE: An action is something that the object knows how to
perform and so is known as the Method of the object.
18
Editing a Macro
Suppose youve recorded a macro that enters your name and address. Then you discover that
you forgot a step or that you recorded a step incorrectlyyou chose the wrong border format,
for example.
What do you do?
To add code to an existing macro, you can record actions in a temporary macro and then
transfer the code into the macro you want to change. For example, to add to the
UniversityAddress macro a step that sets font options for the address, follow these steps:
19
step 3 . Choose Home, Cells, Format, and Format Cells and click the Font tab. Select Arial,
14-point, and Bold Italic. Then click OK to apply the formats.
step 4 . Click the Stop Recording button in the Developer tab.
step 5 . Choose Developer, Code, and Macros. In the Macro dialog box, select MacroTemp
and click Edit.
step 6 . A window appears that contains the original macro recorded plus the MacroTemp
macro.
step 7 . Select all the code inside the MacroTempfrom the line beginning With through the
line beginning End Withand then choose Edit, Copy.
step 8 . Scroll up to display the UniversityAddress macro.
step 9 . Click at the end of the line that contains the statement: Range(A6).Select.
step 10. Press Enter to create a blank line. Then position the insertion point at the beginning
of the blank line.
step 11. Choose Edit, Paste.
step 12. Scroll back down and delete the entire MacroTemp macro, from the Sub statement
to the End Sub statement.
step 13. Run the original macro and see the difference.
1.
2.
3.
4.
With ActiveSheet.PageSetup
.Orientation = xlPortrait
End With
End Sub
step
step
step
step
step
5.
6.
7.
8.
9.
20
EXERCI SE 2
MACRO CREATION
1. You can create the Macro either by using the standard method or by using the
VB editor.
2. The file to work on is exercise2.xls
3. Change the appearance of the document by updating the font to Comic Sans
MS, size 11, italic and column width of 20.
4. Save the Macro and execute it.
21
RESOURCES
Faculty / Staff Resource Center
Located in ARC 3012. Assistance available on walk-in basis.
Open Lab on Fridays, 1-4 pm (Fall, Winter, Spring)
Open Lab on Thursdays 1-4 pm (Summer only)
FSRC Website
http://www.csus.edu/uccs/fscenter/
Getting Help
University Help Desk
(916) 278-7337 or [email protected]
Academic Technology Consultants
http://www.csus.edu/atcs/contact.htm
Help Desk - Problem Reports & Contact Information
http://www.csus.edu/uccs/helpdesk/contact.stm
Training Requests
[email protected]
Campus Resources
Training
http://www.csus.edu/training
Training Handouts
http://www.csus.edu/training/handouts
Online Tutorials
http://www.csus.edu/atcs/tools/tutorials.htm
Educational Tools
http://www.csus.edu/atcs/tools
Accessibility at Sacramento State
http://www.csus.edu/accessibility
22