??? What if the source data range expands (or shrinks) the next time we get this data?
In the VBE, add a new subroutine and name it MakeDynamicPivotTable. Copy the code
from the MakePivotTable procedure, and then make the following modifications. Add the
following variable declarations at the top of the MakeDynamicPivotTable procedure:
Dim ws As Worksheet
Dim rngRangeToPivot As Range
Dim sPivotLoc As String
The first variable, ws, will be used to store the new worksheet that we??™ll be adding. The
next variable, rngRangeToPivot, will get the data source range for us regardless of number of
rows. The last variable, sPivotLoc, will hold a string value denoting the range to place the new
PivotTable.
The first thing we??™ll do is get the location of the data range that we??™ll be putting into our
PivotTable. We??™ll do this first because once we add a new sheet, the data viewed by the user
will no longer be active.
CHAPTER 6 n PIVOTTABLES 233
Add the following line of code to assign the current data region (the region where the cursor
is currently placed):
Set rngRangeToPivot = ActiveCell.CurrentRegion
The ActiveCell.Current region property will retrieve the range of the contiguous set of
cells surrounding the cursor location.
Now let??™s add a new worksheet and define the PivotTable location on the new worksheet:
Set ws = Sheets.Add
sPivotLoc = ws.Name & "!R3C1"
We??™re adding a new worksheet and assigning that worksheet to the ws variable. Then we??™re
looking at that worksheet to determine its name and concatenating it to the cell location
where the PivotTable will be place on the new worksheet.
Pages:
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227