I have the following code to create a pivot table from a data set. I get a Reference is Not Valid at the red line below. I know the fix must be simple, but I have hit a blank on the fix.
Thanks in advance.
Dim WSD As Worksheet
Dim WSData As Worksheet
Dim PTCache As PivotCache
Dim PT As PivotTable
Dim PRange As Range
Dim FinalRow As Long
Dim FinalCol As Long
Set WSData = Worksheets("Counter Measures")
Set WSD = Worksheets("Pivot")
' Delete any prior pivot tables
For Each PT In WSD.PivotTables
PT.TableRange2.Clear
Next PT
' Define input area and set up a Pivot Cache
FinalRow = WSData.Cells(Application.Rows.Count, 1).End(xlUp).Row
FinalCol = WSData.Cells(1, Application.Columns.Count).End(xlToLeft).Column
Set PRange = WSData.Cells(1, 1).Resize(FinalRow, FinalCol)
''''''''''''''''''''''''''''''''
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:="=PRange")
''''''''''''''''''''''''''''''''''''''''''''''''''''''
I get a Reference is not Valid error at the next line.
' Create the Pivot Table from the Pivot Cache
Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Cells(3, 1), TableName:="PivotTable4")
Bookmarks