Hi..
You need to add some code that will detect the bottom of your range..
Lets say your data is from Column A:J.. starting in row 2.. but the number of rows can change..
In this case.. you need to find the last used row in your data range..
First Dimension your variable..
then.. to find the Last Row (assuming you have data in Column A all the way till the bottom)..
'Find Last Row of the Data Range
LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Then... Your Pivot Table code can be..
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("Sheet1").Range("A2:J" & LastRow), Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:=Sheets("Sheet2").Cells(2, 1), TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
Be careful using..
DefaultVersion _
:=xlPivotTableVersion10
As that will limit the Excel versions your Pivot table will work in.. but if its only for your local machine.. thats fine..
Just noticed you might need to find the last used column...
This is the most reliable thing for that..
Dim rLastCell As Range
Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
Bookmarks