title: Creating pivot table with macro - subscript out of range
body: <p>I am trying to create pivot table macro. For this, I have input data and I have a recorded macro on it. However, I am getting an error since I want to use it in multiple areas of the code.</p>
  
  <p>Here is some sample data:</p>
  
  <p><img src="https://i.stack.imgur.com/sIDLu.png" alt="enter image description here"></p>
  
  <p>I need output like this:</p>
  
  <p><img src="https://i.stack.imgur.com/gAAae.png" alt="enter image description here"></p>
  
  <pre><code>Sub macro5()
  '
  ' macro5 Macro
  '
  
  '
      Range("A2:C16").Select
      Range("C16").Activate
      Sheets.Add
      ActiveWorkbook.Worksheets("sheet2").PivotTables("PivotTable3").PivotCache. _
          CreatePivotTable TableDestination:="Sheet6!R3C1", TableName:="PivotTable5" _
          , DefaultVersion:=xlPivotTableVersion12
      Sheets("Sheet6").Select
      Cells(3, 1).Select
      With ActiveSheet.PivotTables("PivotTable5").PivotFields("Item")
          .Orientation = xlRowField
          .Position = 1
      End With
      With ActiveSheet.PivotTables("PivotTable5").PivotFields("Category")
          .Orientation = xlColumnField
          .Position = 1
      End With
      ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
          "PivotTable5").PivotFields("Price"), "Sum of Price", xlSum
  End Sub
  </code></pre>
  
  <p>I am getting an error at the highlighted part.</p>
  
  <p>Can anyone help me figure out why?</p>