Hi,
WS is a reference to a worksheet rather than the worksheet's name, so your TableDestination argument should be:
Code:
TableDestination:=WS.Name & "!R1C1"
As a general rule it is more robust to include ' in case there is a space in the name.
Code:
TableDestination:="'" & WS.Name & "'!R1C1"
Don't try to do too much in one line of code. It makes it more readable and managable if you split it out into simpler steps, for example:
Code:
Sub foo()
Dim WS As Worksheet
Dim PC As PivotCache
Dim PT As PivotTable
Set WS = ActiveWorkbook.Worksheets.Add
Set PC = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlDatabase, _
SourceData:="InputSheet!R1C1:R20C20")
Set PT = PC.CreatePivotTable( _
TableDestination:="'" & WS.Name & "'!R1C1", _
TableName:="MyPivotTable", _
DefaultVersion:=xlPivotTableVersion11)
End Sub
For example, now it's easier to debug or add defensive coding/error handling if there's a problem .
Hope that helps...