title: Creating pivot table in VBA
body: <p>I am using the code below to try and create a pivot table in Excel, but it doesn't appear to be working</p>
  
  <pre><code>    Range("A1").Select
      Range(Selection, Selection.End(xlToRight)).Select
      Range(Selection, Selection.End(xlDown)).Select
  
  Dim AllData As Range
  Set AllData = selection.CurrentRegion
  
      ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=AllData).CreatePivotTable TableDestination:= _
          "'[Process Data v2.xls]Pivot'!R4C1", TableName:="PivotTable3", _
          DefaultVersion:=xlPivotTableVersion10
  </code></pre>
  
  <p>The error is with the pivot table section, and its a runtime error 5. Invalid procedure call or argument.</p>
  
  <p>I have tried recording a macro and having a precise range in the script and it works, but replacing this with the 'Alldata' variable breaks it. I tried a lot of things but can't figure out why its not working</p>