title: How to Create a Pivot Table in VBA
body: <p>I'm trying to create a Pivot table, but getting <code>Invalid Procedure Call or Argument</code>.</p>
  
  <pre><code>ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rng", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="rngB", TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
  </code></pre>
  
  <ul>
  <li><code>rng</code> (The source) is a range consisting of about 20 columns and a few thousand rows.</li>
  <li><code>rngB</code> (The destination) is a single cell in a different worksheet</li>
  </ul>
  
  <p>Can anyone advise where I am going wrong?</p>
  
  <p>EDIT:</p>
  
  <p>My fault, I should have been using <code>rngData</code> and not <code>rng</code> as the Source.</p>
  
  <pre><code>    Set rng = wsA.Range("C14")
      Set rngData = Range(rng, rng.End(xlToRight))
      Set rngData = Range(rng, rng.End(xlDown))
      Set rngB = wsB.Range("C8")
  
      ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
  </code></pre>
  
  <p>This brings up the PivotTable frame just fine.</p>