Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Use icon sets relatively in a pivot table | Excel Discussion (Misc queries) | |||
Selecting a range of values on pivot table attribute with a macro | Excel Discussion (Misc queries) | |||
Create univeral pivot table macro - use with varying lenght data sets | Excel Programming | |||
Macro to update pivot table data range | Excel Programming | |||
Creating a pivot table from different sets of data using a macro | Excel Programming |