Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Unfixing source data range for a pivot table using vba

When I create a pivot table for the first time, the cell references are fixed
in R1C1 format. I would like to have a macro that will enable me to create
the same pivot table, and at the same time, update those references to the
range of data that would have changed from time to time. Right now I'm stuck
when trying to change the source data code in the Visual Basic editor.

Currently using MS excel 2003.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Unfixing source data range for a pivot table using vba

Hi,

If I understand your query correctly then the source data for the pivot
table changes and you want a macro to be able to recreate the pivot table
with the new source data range.

The following code clears the old pivot table, selects the source data and
creates the new pivot table.

There are other ways of defining the source data range but I would need to
have more info about the range. The following method works provided there are
no blank cells in the top row or left column.

You might need to edit the macro and replace the cell reference in
Range("A1").Select with the top left cell in your source data. (Include the
column header because it is used in the pivot table)

The pivot table code was recorded and the source data range replaced with
the range variable 'rng1'. Ensure you replace the range including the double
quotes and leave the closing bracket.

Sub Macro4()
Dim rng1 As Range

Sheets("Sheet1").Select

'Delete old pivot table first
'Set following range to include your existing pivot table
'the range to clear can be larger than the pivot table
Range("G1:Q17").Clear

Range("A1").Select 'Top left cell of source. (On column header)

'The following code was recorded by holding the Ctrl and
'Shift and then pressing right arrow and then down arrow.
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

'Assign the selected data to a variable
Set rng1 = Selection

'Replace the source data range with the range variable
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, _
SourceData:=rng1) _
.CreatePivotTable _
TableDestination:="'Sheet1'!R1C8", _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

'For some reason in xl2002 the new pivot table is blank until selected
'and hense the following line.
Range("J7").Select
End Sub

Hope you can extract enough info from the example to do what you want. If it
fails, then post the code you have an I'll see if I can help some more.

Regards,

OssieMac



"freespirited_74" wrote:

When I create a pivot table for the first time, the cell references are fixed
in R1C1 format. I would like to have a macro that will enable me to create
the same pivot table, and at the same time, update those references to the
range of data that would have changed from time to time. Right now I'm stuck
when trying to change the source data code in the Visual Basic editor.

Currently using MS excel 2003.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Unfixing source data range for a pivot table using vba

Thanks heaps! I've spent a couple of days trying to find the solution to
this..

"OssieMac" wrote:

Hi,

If I understand your query correctly then the source data for the pivot
table changes and you want a macro to be able to recreate the pivot table
with the new source data range.

The following code clears the old pivot table, selects the source data and
creates the new pivot table.

There are other ways of defining the source data range but I would need to
have more info about the range. The following method works provided there are
no blank cells in the top row or left column.

You might need to edit the macro and replace the cell reference in
Range("A1").Select with the top left cell in your source data. (Include the
column header because it is used in the pivot table)

The pivot table code was recorded and the source data range replaced with
the range variable 'rng1'. Ensure you replace the range including the double
quotes and leave the closing bracket.

Sub Macro4()
Dim rng1 As Range

Sheets("Sheet1").Select

'Delete old pivot table first
'Set following range to include your existing pivot table
'the range to clear can be larger than the pivot table
Range("G1:Q17").Clear

Range("A1").Select 'Top left cell of source. (On column header)

'The following code was recorded by holding the Ctrl and
'Shift and then pressing right arrow and then down arrow.
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

'Assign the selected data to a variable
Set rng1 = Selection

'Replace the source data range with the range variable
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, _
SourceData:=rng1) _
.CreatePivotTable _
TableDestination:="'Sheet1'!R1C8", _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion10

'For some reason in xl2002 the new pivot table is blank until selected
'and hense the following line.
Range("J7").Select
End Sub

Hope you can extract enough info from the example to do what you want. If it
fails, then post the code you have an I'll see if I can help some more.

Regards,

OssieMac



"freespirited_74" wrote:

When I create a pivot table for the first time, the cell references are fixed
in R1C1 format. I would like to have a macro that will enable me to create
the same pivot table, and at the same time, update those references to the
range of data that would have changed from time to time. Right now I'm stuck
when trying to change the source data code in the Visual Basic editor.

Currently using MS excel 2003.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 Pivot Table Changes the Source Data Range Marilyn Excel Discussion (Misc queries) 0 September 14th 09 07:44 PM
Using cursor keys to select Pivot Table Source Data Range GuitrDad Excel Discussion (Misc queries) 0 September 12th 07 06:20 PM
Increasing the Source Data range for an existing Pivot Table Shams Excel Worksheet Functions 2 October 10th 06 05:22 PM
Pivot Table data source "data source contains no visible tables" Jane Excel Worksheet Functions 0 September 29th 05 08:28 PM
Change the range of a pivot table data source Tony White[_2_] Excel Programming 3 July 11th 05 07:46 PM


All times are GMT +1. The time now is 05:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"