251
Handling Additional Annoyances When Creating Your Final Report
variables to the original workbook, new workbook, and first worksheet in the new work-
book. At the top of the procedure, add these statements:
Dim WSR As Worksheet
Dim WBO As Workbook
Dim WBN As Workbook
Set WBO = ActiveWorkbook
Set WSD = Worksheets(“Pivot Table”)
After the pivot table has been successfully created, build a blank Report workbook with this
code:
‘ Create a New Blank Workbook with one Worksheet
Set WBN = Workbooks.Add(xlWorksheet)
Set WSR = WBN.Worksheets(1)
WSR.Name = “Report”
‘ Set up Title for Report
With WSR.Range(“A1”)
.Value = “Revenue by Market and Year”
.Font.Size = 14
End With
Creating a Summary on a Blank Report Worksheet
Imagine that you have submitted the pivot table in Figure 11.9, and your manager hates the
borders, hates the title, and hates the words “Invoice Date” in cell T2. You can solve all
three of these problems by excluding the first row(s) of
PT.TableRange2
from the
.Copy
method and then using
PasteSpecial(xlPasteValuesAndNumberFormats)
to copy the data to
the report sheet.
11
In Excel 2000 and earlier,
xlPasteValuesAndNumberFormats
was not available.You had to
use Paste Special twice: once as
xlPasteValues
and once as
xlPasteFormats
.
C A U T I O N
In the current example, the
.TableRange2
property includes only one row to eliminate, row
2, as shown in Figure 11.9. If you had a more complex pivot table with several column fields
and/or one or more page fields, you would have to eliminate more than just the first row of
the report. It helps to run your macro to this point, look at the result, and figure out how
many rows you need to delete. You can effectively not copy these rows to the report by
using the
Offset
property. Copy the
TableRange2
property, offset by one row. Purists will
note that this code copies one extra blank row from below the pivot table, but this really
does not matter because the row is blank. After copying, you can erase the original pivot
table and destroy the pivot cache:
‘ Copy the Pivot Table data to row 3 of the Report sheet
‘ Use Offset to eliminate the title row of the pivot table
PT.TableRange2.Offset(1, 0).Copy
WSR. Range(“A3”).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
PT.TableRange2.Clear
Set PTCache = Nothing
12_0789736012_CH11.qxd 12/11/06 6:26 PM Page 251
Содержание VBA
Страница 60: ......