For some reason the text has appeared on my first post....
I have recorded a macro while creating a pivot table but the range that the original table was created from will change from week to week. I need to write the macro to select the current range on the sheet the table is being created from!
Can anyone tell me how to do this????
Code:
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"'Prior To Pivot Table'!R2C1:R1194C3", TableDestination:="", TableName:= _
"PivotTable2"
ActiveSheet.PivotTables("PivotTable2").AddFields RowFields:=Array("Proj_only" _
, "Data")
With ActiveSheet.PivotTables("PivotTable2").PivotFields("GL WIP")
.Orientation = xlDataField
.Name = "Sum of GL WIP"
.Position = 1
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Con Reg WIP")
.Orientation = xlDataField
.Name = "Sum of Con Reg WIP"
.Function = xlSum
End With
End Sub
'Dynamic range can be specified as below
1. Name the Range
Choose Insert>Name>Define
Type a name for the range, e.g. Database
In the Refers To box, enter an Offset formula that defines the range size, based on the number of items in a column that doesn't contain any blank cells. , e.g.:
=OFFSET(Data!$A$1,0,0,COUNTA(Data!$A:$A),7)
In this example, the list is on a sheet named 'Data', starting in cell A1. The arguments used in this Offset function are:
Reference cell: Data!$A$1
Rows to offset: 0
Columns to offset: 0
Number of Rows: COUNTA(Data!$A:$A)
Number of Columns: 7
Note: for a dynamic number of columns,
replace the 7 with: COUNTA(Data!$1:$1)
Click OK
2. Base the Pivot Table on the Named Range
Select a cell in the database
Choose Data>PivotTable and PivotChart Report
Select 'Microsoft Excel List or Database', click Next.
For the range, type your range name, e.g. Database
Click Next
Click the Layout button
Drag field buttons to the row, column and data areas
Click OK, click Finish