End+Down Versus End+Up
You might be tempted to find the final row by starting in cell A1 and using the End key in conjunction with the down-
arrow key. Avoid this approach. Data coming from another system is imperfect. If your program will import 500,000 rows
from a legacy computer system every day for the next five years, a day will come when someone manages to key a null
value into the dataset.This value will cause a blank cell or even a blank row to appear in the middle of your dataset.
Using
Range(“A1”).End(xlDown)
will stop prematurely at the blank cell instead of including all your data.This
blank cell will cause that day’s report to miss thousands of rows of data, a potential disaster that will call into question
the credibility of your report.Take the extra step of starting at the last row in the worksheet to greatly reduce the risk of
problems.
Using Super-Variables: Object Variables
In typical programming languages, a variable holds a single value. You might use
x = 4
to
assign a value of 4 to the variable
x
.
Think about a single cell in Excel. Many properties describe a cell. A cell might contain a
value such as 4, but the cell also has a font size, a font color, a row, a column, possibly a for-
mula, possibly a comment, a list of precedents, and more. It is possible in VBA to create a
super-variable that contains all the information about a cell or about any object. A statement
to create a typical variable such as
x = Range(“A1”)
assigns the current value of A1 to the
variable
x
.
However, you can use the
Set
keyword to create an object variable:
Set x = Range(“A1”)
You’ve now created a super-variable that contains all the properties of the cell. Instead of
having a variable with only one value, you have a variable in which you can access the value
of many properties associated with that variable. You can reference
x.Formula
to learn the
formula in A1 or
x.Font.ColorIndex
to learn the color of the cell.
Understanding Versions
Pivot tables have been evolving. They were introduced in Excel 5 and perfected in Excel 97.
In Excel 2000, pivot table creation in VBA was dramatically altered. Some new parameters
were added in Excel 2002. A few new properties such as
PivotFilters
and
TableStyle2
were
added in Excel 2007. Therefore, you need to be extremely careful when writing code in
Excel 2007 that might be run in Excel 2003 or Excel 2000 or Excel 97.
Just a few simple tweaks make 2003 code run in 2000, but a major overhaul is required to
make any code run in Excel 97. Because it has been 10 years since the release of Excel 97
(and because Microsoft has not supported that product for 5+ years), this chapter focuses on
using only the pivot cache method introduced in Excel 2000. At the end of the chapter, you
briefly learn the PivotTable Wizard method, which is your only option if you need code to
run in Excel 97.
Chapter 11
Using VBA to Create Pivot Tables
236
11
12_0789736012_CH11.qxd 12/11/06 6:26 PM Page 236
Summary of Contents for VBA
Page 60: ......