However there is a way to overcome this problem by using dynamic data range with the help of Excel Offset Function. Before creating the pivot by macro you need to create dynamic data range using offset function.
Then you create a Pivot with the named range.
See the macro code given below for better understanding
Sub Pivot_with_Dynamic_range() ' This creates Dynamic data range named "PvtData" ActiveWorkbook.Names.Add Name:="PvtData", RefersToR1C1:= _ "=OFFSET('Data'!R2C1,0,0,COUNTA('Data'!C1),COUNTA('Data'!R2))" ' This creates Pivot using Dynamic data range named "PvtData" ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _ "PvtData").CreatePivotTable TableDestination:="", TableName:="PivotTable1" ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select ActiveSheet.PivotTables("PivotTable1").SmallGrid = False End Sub
The above code handles the major issue of dynamic data range and will create blank pivot table for your data, you need to add the required fields to complete your report. In this data is available at Sheet named "Data" and starting point of the data is Row 2 Column 1 or Cell "A2". You will need to change these references while using the above code in your macro.
Hey I am still not able to make the macros run with dynamic range.
ReplyDeleteIs there a email address or somthing where I can mail you the code??
Hey if i want to paste the pviot table only at a particular sheet, say sheet2, how do i modify it??
ReplyDelete