![]() |
Selecting current range for pivot macro
I'm trying to write a macro to create a standard pivot from a report
and need some help with defining the range. I've recorded the actions that I want using an example of the data. It all appears to be fine and reasonably easy (if sometimes feeling long winded), except the range selection at the start. The start of the recorded macro looks like this... ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ TableDestination:="", TableName:="PivotTable1", DefaultVersion:= _ xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select With ActiveSheet.PivotTables("PivotTable1") .ColumnGrand = False .RowGrand = False End With I'm presuming that VBA doesn't recognise the normal "select current range based on current cell" action of the Pivot Table wizard. In the above, it's explicitly defined the range even though I didn't actually have to think about that when recording. How do I replicate the "select current range..." action for a generic macro (because the number of records will vary each time)? I presume the best way would be to define a variable, use some method to push the current range into the variable, and then point the pivot at it. But I don't know how. Or maybe, since the columns will stay the same, I should look at counting the rows and defining the range that way (Offset comes to mind, but I've also read mentions of the Resize function - but, if it is applicable, I can't find a clear explanation of how.) Any help will be greatly appreciated. ta Matt |
Selecting current range for pivot macro
You need to supply a range for your data argument
The easiest way to do this is to use the currentregion method of the range object. Assuming your data is in a sheet called 'Data'. starting in cell A1 the arguement would be: sheets("Data").cells(1,1).currentregion "Math" wrote: I'm trying to write a macro to create a standard pivot from a report and need some help with defining the range. I've recorded the actions that I want using an example of the data. It all appears to be fine and reasonably easy (if sometimes feeling long winded), except the range selection at the start. The start of the recorded macro looks like this... ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ TableDestination:="", TableName:="PivotTable1", DefaultVersion:= _ xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select With ActiveSheet.PivotTables("PivotTable1") .ColumnGrand = False .RowGrand = False End With I'm presuming that VBA doesn't recognise the normal "select current range based on current cell" action of the Pivot Table wizard. In the above, it's explicitly defined the range even though I didn't actually have to think about that when recording. How do I replicate the "select current range..." action for a generic macro (because the number of records will vary each time)? I presume the best way would be to define a variable, use some method to push the current range into the variable, and then point the pivot at it. But I don't know how. Or maybe, since the columns will stay the same, I should look at counting the rows and defining the range that way (Offset comes to mind, but I've also read mentions of the Resize function - but, if it is applicable, I can't find a clear explanation of how.) Any help will be greatly appreciated. ta Matt |
Selecting current range for pivot macro
Thanks.
How would I fit that into ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ TableDestination:="", TableName:="PivotTable1", ? Thanks again. On 4 Feb, 14:14, DomThePom wrote: You need to supply a range for your data argument The easiest way to do this is to use the currentregion method of the range object. Assuming your data is in a sheet called 'Data'. starting in cell A1 the arguement would be: sheets("Data").cells(1,1).currentregion "Math" wrote: I'm trying to write a macro to create a standard pivot from a report and need some help with defining the range. I've recorded the actions that I want using an example of the data. It all appears to be fine and reasonably easy (if sometimes feeling long winded), except the range selection at the start. The start of the recorded macro looks like this... ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ * * * * "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ * * * * TableDestination:="", TableName:="PivotTable1", DefaultVersion:= _ * * * * xlPivotTableVersion10 * * ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) * * ActiveSheet.Cells(3, 1).Select * * With ActiveSheet.PivotTables("PivotTable1") * * * * .ColumnGrand = False * * * * .RowGrand = False * * End With I'm presuming that VBA doesn't recognise the normal "select current range based on current cell" action of the Pivot Table wizard. *In the above, it's explicitly defined the range even though I didn't actually have to think about that when recording. How do I replicate the "select current range..." action for a generic macro (because the number of records will vary each time)? I presume the best way would be to define a variable, use some method to push the current range into the variable, and then point the pivot at it. *But I don't know how. Or maybe, since the columns will stay the same, I should look at counting the rows and defining the range that way (Offset comes to mind, but I've also read mentions of the Resize function - but, if it is applicable, I can't find a clear explanation of how.) Any help will be greatly appreciated. ta Matt- Hide quoted text - - Show quoted text - |
Selecting current range for pivot macro
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _
sheets("Base Data Report").cells(1,1).currentregion).CreatePivotTabl e _ TableDestination:="", TableName:="PivotTable1 Try this but if you still have trouble..... I use the PivotTableWizard method of the sheet object which is a little less complicated. dim sht as worksheet dim pvt as PivotTable dim rngData as range set rng = sheets("Base Data Report").cells(1,1).currentregion set sht = activeworkbook.sheets.add set pvt = sht.PivotTableWizard(xlDatabase, rngData, , "PivotTable1") "Math" wrote: Thanks. How would I fit that into ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ TableDestination:="", TableName:="PivotTable1", ? Thanks again. On 4 Feb, 14:14, DomThePom wrote: You need to supply a range for your data argument The easiest way to do this is to use the currentregion method of the range object. Assuming your data is in a sheet called 'Data'. starting in cell A1 the arguement would be: sheets("Data").cells(1,1).currentregion "Math" wrote: I'm trying to write a macro to create a standard pivot from a report and need some help with defining the range. I've recorded the actions that I want using an example of the data. It all appears to be fine and reasonably easy (if sometimes feeling long winded), except the range selection at the start. The start of the recorded macro looks like this... ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ TableDestination:="", TableName:="PivotTable1", DefaultVersion:= _ xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select With ActiveSheet.PivotTables("PivotTable1") .ColumnGrand = False .RowGrand = False End With I'm presuming that VBA doesn't recognise the normal "select current range based on current cell" action of the Pivot Table wizard. In the above, it's explicitly defined the range even though I didn't actually have to think about that when recording. How do I replicate the "select current range..." action for a generic macro (because the number of records will vary each time)? I presume the best way would be to define a variable, use some method to push the current range into the variable, and then point the pivot at it. But I don't know how. Or maybe, since the columns will stay the same, I should look at counting the rows and defining the range that way (Offset comes to mind, but I've also read mentions of the Resize function - but, if it is applicable, I can't find a clear explanation of how.) Any help will be greatly appreciated. ta Matt- Hide quoted text - - Show quoted text - |
Selecting current range for pivot macro
Ah, wonderful. Thankyou :)
On 4 Feb, 17:01, DomThePom wrote: ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ * * * * sheets("Base Data Report").cells(1,1).currentregion).CreatePivotTabl e _ * * * * TableDestination:="", TableName:="PivotTable1 Try this but if you still have trouble..... I use the PivotTableWizard method of the sheet object which is a little less complicated. dim sht as worksheet dim pvt as PivotTable dim rngData as range set rng = sheets("Base Data Report").cells(1,1).currentregion set sht = activeworkbook.sheets.add set pvt *= sht.PivotTableWizard(xlDatabase, rngData, , "PivotTable1") "Math" wrote: Thanks. How would I fit that into ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ * * * * "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ * * * * TableDestination:="", TableName:="PivotTable1", ? Thanks again. On 4 Feb, 14:14, DomThePom wrote: You need to supply a range for your data argument The easiest way to do this is to use the currentregion method of the range object. Assuming your data is in a sheet called 'Data'. starting in cell A1 the arguement would be: sheets("Data").cells(1,1).currentregion "Math" wrote: I'm trying to write a macro to create a standard pivot from a report and need some help with defining the range. I've recorded the actions that I want using an example of the data. It all appears to be fine and reasonably easy (if sometimes feeling long winded), except the range selection at the start. The start of the recorded macro looks like this... ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase, SourceData:= _ * * * * "'Base Data Report'!R1C1:R244C44").CreatePivotTable _ * * * * TableDestination:="", TableName:="PivotTable1", DefaultVersion:= _ * * * * xlPivotTableVersion10 * * ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) * * ActiveSheet.Cells(3, 1).Select * * With ActiveSheet.PivotTables("PivotTable1") * * * * .ColumnGrand = False * * * * .RowGrand = False * * End With I'm presuming that VBA doesn't recognise the normal "select current range based on current cell" action of the Pivot Table wizard. *In the above, it's explicitly defined the range even though I didn't actually have to think about that when recording. How do I replicate the "select current range..." action for a generic macro (because the number of records will vary each time)? I presume the best way would be to define a variable, use some method to push the current range into the variable, and then point the pivot at it. *But I don't know how. Or maybe, since the columns will stay the same, I should look at counting the rows and defining the range that way (Offset comes to mind, but I've also read mentions of the Resize function - but, if it is applicable, I can't find a clear explanation of how.) Any help will be greatly appreciated. ta Matt- Hide quoted text - - Show quoted text -- Hide quoted text - - Show quoted text - |
All times are GMT +1. The time now is 12:10 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com