Professional Documents
Culture Documents
The following dialog box appears. Excel automatically selects the data for
you. The default location for a new pivot table is New Worksheet.
3. Click OK.
Drag fields
The PivotTable field list appears. To get the total amount exported of each
product, drag the following fields to the different areas.
1. Product Field to the Row Labels area.
2. Amount Field to the Values area.
3. Country Field to the Report Filter area.
Below you can find the pivot table. Bananas are our main export product.
That's how easy pivot tables can be!
Sort
To get Banana at the top of the list, sort the pivot table.
1. Click any cell inside the Total column.
2. The PivotTable Tools contextual tab activates. On the Options tab, click the
Sort Largest to Smallest button (ZA).
Result.
Filter
Because we added the Country field to the Report Filter area, we can filter
this pivot table by Country. For example, which products do we export the
most to France?
Note: you can use the standard filter (triangle next to Product) to only show
the totals of specific products.
3. Choose the type of calculation you want to use. For example, click Count.
4. Click OK.
Result. 16 out of the 28 orders to France were 'Apple' orders.
To easily compare these numbers, create a pivot chart and apply a filter.
Maybe this is one step too far for you at this stage, but it shows you one of
the many other powerful pivot table features Excel has to offer.
Chapter
Pivot Tables
Frequency Distribution
Pivot Chart
Slicers
Calculated Field/Item
What is a Macro?
A Microsoft Office Macro (as this functionality applies to several of the
MS Office Applications) is simply Visual Basic for Applications (VBA)
code saved inside a document. For a comparable analogy, think of a
document as HTML and a macro as Javascript. In much of the same
way that Javascript can manipulate HTML on a webpage, a macro can
manipulate a document.
Macros are incredibly powerful and can do pretty much anything your
imagination can conjure. As a (very) short list of functions you can do
with a macro:
As we stated above, a macro is VBA code, but one of the nice things
about Excel is you can create/record them with zero coding required
as we will do here.
Once this is done, all of your actions are recorded every cell change,
scroll action, window resize, you name it.
The other is in the bottom right corner. The stop icon indicates it is in
macro mode and pressing here will stop the recording (likewise, when
not in record mode, this icon will be the Record Macro button, which
you can use instead of going to the Macros menu).
Now that we are recording our macro, lets apply our summary
calculations. First add the headers.
=SUM(B2:K2)
=AVERAGE(B2:K2)
=MIN(B2:K2)
=MAX(B2:K2)
=MEDIAN(B2:K2)
Now, highlight all the calculation cells and drag the length of all our
data rows to apply the calculations to each row.
Now, we want to get the summary data for the entire sheet, so we
apply a few more calculations:
Respectively:
=SUM(L2:L21)
=AVERAGE(B2:K21) *This must be calculated across all data
because the average of the row averages does not necessarily
equal the average of all the values.
=MIN(N2:N21)
=MAX(O2:O21)
Now that the calculations are done, we will apply the style and
formatting. First apply general number formatting across all the cells
by doing a Select All (either Ctrl + A or click the cell between the row
and column headers) and select the Comma Style icon under the
Home menu.
Next, apply some visual formatting to both the row and column
headers:
Bold.
Centered.
When all is finished, this is what our data sheet looks like:
Since we are satisfied with the results, stop the recording of the
macro.
In order to use our newly recorded macro, we have to save our Excel
Workbook in a macro enabled file format. However, before we do that,
we first need to clear all the existing data so that it is not embedded in
our template (the idea being every time we use this template, we will
import the most up-to-date data).
To do this, select all cells and delete them.
With the data now cleared (but the macros still included in the Excel
file), we want to save the file as a macro enabled template (XLTM) file.
It is important to note that if you save this as a standard template
(XLTX) file then macros will not be able to be run from it. Alternately,
you can save the file as a legacy template (XLT) file, which will allow
macros to be run.
Once you have saved the file as a template, go ahead and close
Excel.
VBA code is actually quite powerful and can manipulate files outside
of the scope of the current document. For example, a macro could
alter or delete random files in your My Documents folder. As such, it is
important to make sure you only run macros from trusted sources.
To put our data format macro to use, open the Excel Template file
which was created above. When you do this, assuming you have
standard security settings enabled, you will see a warning across the
top of the workbook which says that macros are disabled. Because we
trust a macro created by ourselves, click the Enable Content button.
Up next, we are going to import the latest data set from a CSV (this is
the source the worksheet used to create our macro).
To complete the import of the CSV file, you may have to set a few
options in order for Excel to interpret it correctly (e.g. delimiter,
headers present, etc.).
Once our data is imported, simply go to the Macros menu (under the
View tab) and select View Macros.
Once running, you may see the cursor jump around for a few
moments, but as it does you will see the data being
manipulated exactly as we recorded it. When all is said and done, it
should look just like our original except with different data.
The window that opens displays the source code that was recorded
from our actions when creating the macro. Of course, you can edit this
code or even create new macros entirely inside of the code window.
While the recording action used in this article will likely fit most needs,
more highly customized actions or conditional actions would require
you to edit the source code.
3. With the macro recording, import the data file like you normally
would.
4. Once the data is imported, stop recording the macro.
5. Delete all the cell data (select all then delete).
6. Save the updated template (remember to use a macro enabled
template format).
Once this is done, whenever the template is opened there will be two
macros one which loads our data and the other which formats it.
If you really wanted to get your hands dirty with a bit of code editing,
you could easily combine these actions into a single macro by copying