title: Runtime error '1004' while creating a pivot table
body: <p>I am trying to create a pivot table after copying the data from one workbook to another workbook.  Here is the code.  </p>
  
  <pre><code>Dim ws As Worksheet
  Set ws = Sheets.Add
      ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
          "PIVOT_RANGE", Version:=xlPivotTableVersion14). _
          CreatePivotTable TableDestination:=ws.Name &amp; "!R1C1", TableName:= _
          "PivotTable2", DefaultVersion:=xlPivotTableVersion14
  </code></pre>
  
  <p>I am seeing different pivot table name every time.  Sometimes it shows <strong>PivotTable1</strong> and Sometimes it shows <strong>PivotTable2</strong>.</p>
  
  <p>I am also getting the following error message</p>
  
  <p><strong>'Run-time error 1004':</strong></p>
  
  <p><strong><em>The PivotTable field name is not valid. To create a PivotTable report, you must use data that is organized as a list with labeled columns.  If you are changing the name of a PivotTable field, you must type a new name for the field.</em></strong></p>
  
  <p>I actually have the data from A1:X and all the columns have column headings, but still I am getting this error message.  </p>
  
  <p>Just wanted to know is there a way to get pivot table name through VBA instead of giving the table name in code (i.e. <strong>TableName:= "PivotTable2"</strong>) which is not the same most of the times.</p>
  
  <p>Can someone please help me on this?</p>