Creating a macro that can open multiple files and add a pivot table to each. The files are all formatted the same with the worksheet "details" containing the data needed for the pivot table. However each file has a different amount of rows so I'm trying to use a dynamic range to capture all data on each file.
The coding above this opens the file and formats it as needed to work with the pivot table.
'Set Dynamic Range
Dim startCell As String
Dim lastRow As Long
Dim lastCol As Long
Dim WS As String
WS = "Details"
Worksheets(WS).Activate
'Find Last row and column
lastRow = Cells(Rows.Count, Cells(1, 7).Column).End(xlUp).Row
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 7), Cells(lastRow, lastCol)).Select
Range(Range("G1"), Range("G1").End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Name = "DynamicRange"
Sheets.Add After:=ActiveSheet
' Create Pivot Table
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"DynamicRange", Version:=6).CreatePivotTable TableDestination:= _
"Sheet1!R1C1", TableName:="PivotTable1", DefaultVersion:=6
Sheets("Sheet1").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1")
.ColumnGrand = True
.HasAutoFormat = True
When I get to the "create pivot table" section the first line gives me a run-time error code 1004.