title: Macro crashes while creating pivot out of large data sets
body: <p>I am quite new to VBA and excel macros but want to automate certain reports for work and I am really hoping for someone's help. I tried to fix it myself by watching YouTube videos and reading stackoverflow but the solution didn't match with my issue. For now I created few macros but i am stuck with the one which should actually create a pivot table.</p>
  
  <p>I first created a regular table out of the data I had (using TableP Macro beloew). It looks like it works fine (tested few other data sheets) :</p>
  
  <pre><code>Sub tableP()
  '
  ' tableP Macro (Creates a table of the existing data sheet, regardless the data input)
  '
  
      ActiveCell.Cells.Select
      ActiveCell.Activate
      ActiveSheet.ListObjects.Add(xlSrcRange, Range("$1:$1048576"), , xlYes).Name = _
          "Table2"
      ActiveCell.Cells.Select       `
  `End Sub   `
  </code></pre>
  
  <p>The second macro (below) would ideally take that table (an object) and create a pivot table, however it crashes <strong>(excel stops working while I run the macro)</strong> each time I use a bigger dataset. </p>
  
  <pre><code>   Sub Macro1test() '
  ' Macro1test creates dataset out of the existing table
  '      
      Dim dataname As String
      Dim Newsheet As String
      dataname = ActiveSheet.ListObjects(1).Name
          Sheets.Add
      Newsheet = ActiveSheet.Name
          ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
              dataname, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
              :=Newsheet &amp; "!R3C1", TableName:="PivotTable2", DefaultVersion:= _
              xlPivotTableVersion14
          Sheets.Select
          Cells(3, 1).Select
      End Sub
  </code></pre>
  
  <p>Can please someone let me know what am I doing wrong and what steps am missing to be able to apply those macros on any data set? </p>