I'm trying to create a pivot table on a new sheet with dynamic sheet names/ranges and I keep getting error "Invalid procedure call or argument" error. My code is below:
Dim wb As Workbook
Dim FinalRow As Long
Dim FinalCol As Long
WSD = ActiveSheet.Name
Sheets.Add
PS = ActiveSheet.Name
FinalRow = Sheets(WSD).Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = Sheets(WSD).Cells(1, Application.Columns.Count). _
End(xlToLeft).Column
ColName = Split(Cells(, FinalCol).Address, "$")(1)
With ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
WSD & "!$A$1:$" & ColName & "$" & FinalRow, Version:=xlPivotTableVersion14)
'This next piece is setting off error: Invalid Procedure Call or Argument
.CreatePivotTable _
TableDestination:=PS & "!$G$3", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
End With
Just for comparison sake, code when copied straight from the from macro:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R1C1:R412C21", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet19!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet19").Select
Cells(3, 1).Select
All I've done is replace the static ranges with dynamic ones. Why isn't this working? Any help is appreciated!
Bookmarks