The reason why you are getting that error is because your are supposed to use .Create
instead of .Add
Set MyPivotCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, MyPivotRangeName)
EDIT
Further to my comments under your question, you code could be optimized (UNTESTED) like this
Sub Sample()
Dim wb As Workbook
Dim ws As Worksheet, newWs As Worksheet
Dim sFile As String, MyPivotRangeName As String
Dim MyPivotRange As Range
Dim pt As PivotTable
Dim MyPivotCache As PivotCache
sFile = "C:UserssrujanDesktopTIME REPORTfresh25_Report Time Booking_25.xls"
Set wb = Workbooks.Open(sFile)
Set ws = wb.Sheets("25_Report Time Booking_25")
Set newWs = wb.Worksheets.Add
With ws
Set MyPivotRange = .Range("A1:G78")
MyPivotRangeName = "'" & ws.Name & "'!" & MyPivotRange.Address(ReferenceStyle = xlR1C1)
Set MyPivotCache = wb.PivotCaches.Create( _
SourceType = xlDatabase, _
SourceData = MyPivotRangeName)
Set pt = MyPivotCache.CreatePivotTable( _
tabledestination:=(newWs.Name & "!R3C1"), _
tablename:="PivotTable1")
'
'~~> Rest of the code
'
End With
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…