Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Pivot Table Macro - How to describe the Active range (so that themacro can be used on new sets of data)

Hello - I am new to macros, and I have come up with the following
code, in trying to build a macro that simply creates a pivot table
based on the active range. But I am having a problem in that I don't
know how to request the pivot to run for the active region in general
(so that I can reuse the macro on multiple tabs and multiple
worksheets), rather than the specific sheet that I originally create
the macro in.

Sub TestPasteAdmits8()

Range("A13:A14").Select
Selection.EntireRow.Delete
Range("A12").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:= _

' I BELIEVE THAT WHAT FOLLOWS IS MY PROBLEM (I NEED THIS TO REFER TO
THE ACTIVE SELECTION AND NOT THE RANGE FOR THAT PARTICULAR SHEET)

"Texas!R12C1:R137C21").CreatePivotTable TableDestination:="",
TableName:= _
"PivotTable9", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard
TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable9").AddFields RowFields:= _
"Treatment Setting Code", ColumnFields:="Eff Start Dt"
With ActiveSheet.PivotTables("PivotTable9").PivotFields ("Ext Rec
Num")
.Orientation = xlDataField
.Caption = "Count of Ext Rec Num"
.Function = xlCount
End With
Cells.Select
Selection.Copy
Sheets.Add
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats,
Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
End Sub

Does anyone happen to know what code I should substitute in the above
macro so that I can reuse the macro on each report, regardless of the
title of the tab?

Thanks for any thoughts you may be able to provide!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default Pivot Table Macro - How to describe the Active range (so that the

Try this instead. Create a Range object, get the range of the source data,
and then use that range object to create the PivotTable:

Dim PTsource As Range
Set PTsource = Range(Range("A12"), _
Range("A12").End(xlToRight).End(xlDown))

ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _
PTsource.Address).CreatePivotTable TableDestination:="", _
TableName:="PivotTable9", DefaultVersion:=xlPivotTableVersion10

This code is also more efficient than using multiple selects.

HTH,
Matthew Pfluger

"Dave K" wrote:

Hello - I am new to macros, and I have come up with the following
code, in trying to build a macro that simply creates a pivot table
based on the active range. But I am having a problem in that I don't
know how to request the pivot to run for the active region in general
(so that I can reuse the macro on multiple tabs and multiple
worksheets), rather than the specific sheet that I originally create
the macro in.

Sub TestPasteAdmits8()

Range("A13:A14").Select
Selection.EntireRow.Delete
Range("A12").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:= _

' I BELIEVE THAT WHAT FOLLOWS IS MY PROBLEM (I NEED THIS TO REFER TO
THE ACTIVE SELECTION AND NOT THE RANGE FOR THAT PARTICULAR SHEET)

"Texas!R12C1:R137C21").CreatePivotTable TableDestination:="",
TableName:= _
"PivotTable9", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard
TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable9").AddFields RowFields:= _
"Treatment Setting Code", ColumnFields:="Eff Start Dt"
With ActiveSheet.PivotTables("PivotTable9").PivotFields ("Ext Rec
Num")
.Orientation = xlDataField
.Caption = "Count of Ext Rec Num"
.Function = xlCount
End With
Cells.Select
Selection.Copy
Sheets.Add
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats,
Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Range("A1").Select
End Sub

Does anyone happen to know what code I should substitute in the above
macro so that I can reuse the macro on each report, regardless of the
title of the tab?

Thanks for any thoughts you may be able to provide!

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
Use icon sets relatively in a pivot table Darlene[_2_] Excel Discussion (Misc queries) 1 March 29th 10 08:31 PM
Selecting a range of values on pivot table attribute with a macro Ryan Hartnett Excel Discussion (Misc queries) 1 October 24th 06 10:21 PM
Create univeral pivot table macro - use with varying lenght data sets rsulliva Excel Programming 1 September 14th 06 01:47 AM
Macro to update pivot table data range Mike_M Excel Programming 2 May 25th 05 04:02 PM
Creating a pivot table from different sets of data using a macro Éidhne in Ireland Excel Programming 1 November 21st 03 05:20 PM


All times are GMT +1. The time now is 09:40 AM.

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"