title: How to apply for loop to create charts using VBA
body: <p>I have 14 pivot tables.I want to create charts for all 14 tables.My code is given below which looks so wired.I want to apply for loop here.My code is given below:</p>
  
  <pre><code>Sub Macro1()
  '
  ' Macro1 Macro
  '
  
  '
      Range("B5:E5").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$3:$E$5")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht1 As Shape
      Set cht1 = ActiveSheet.Shapes(1)
      cht1.Name = "chart001"
      ActiveSheet.ChartObjects("chart001").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart001").Width = 288
      ActiveSheet.Shapes("chart001").LockAspectRatio = msoTrue
  
  
  
  
      Range("B12:D12").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$10:$D$12")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht2 As Shape
      Set cht2 = ActiveSheet.Shapes(1)
      cht2.Name = "chart002"
      ActiveSheet.ChartObjects("chart002").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
  
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart002").Width = 288
      ActiveSheet.Shapes("chart002").LockAspectRatio = msoTrue
  
  
      Range("B19:E19").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$17:$E$19")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht3 As Shape
      Set cht3 = ActiveSheet.Shapes(1)
      cht3.Name = "chart003"
      ActiveSheet.ChartObjects("chart003").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart003").Width = 288
      ActiveSheet.Shapes("chart003").LockAspectRatio = msoTrue
  
  
      Range("B26:E26").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$24:$E$26")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht4 As Shape
      Set cht4 = ActiveSheet.Shapes(1)
      cht4.Name = "chart004"
      ActiveSheet.ChartObjects("chart004").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart004").Width = 288
      ActiveSheet.Shapes("chart004").LockAspectRatio = msoTrue
  
  
      Range("B33:E33").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$31:$E$33")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht5 As Shape
      Set cht5 = ActiveSheet.Shapes(1)
      cht5.Name = "chart005"
      ActiveSheet.ChartObjects("chart005").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart005").Width = 288
      ActiveSheet.Shapes("chart005").LockAspectRatio = msoTrue
  
  
      Range("B40:E40").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$38:$E$40")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht6 As Shape
      Set cht6 = ActiveSheet.Shapes(1)
      cht6.Name = "chart006"
      ActiveSheet.ChartObjects("chart006").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart006").Width = 288
      ActiveSheet.Shapes("chart006").LockAspectRatio = msoTrue
  
  
      Range("B47:E47").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$45:$E$47")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht7 As Shape
      Set cht7 = ActiveSheet.Shapes(1)
      cht7.Name = "chart007"
      ActiveSheet.ChartObjects("chart007").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart007").Width = 288
      ActiveSheet.Shapes("chart007").LockAspectRatio = msoTrue
  
  
      Range("B54:E54").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$52:$E$54")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht8 As Shape
      Set cht8 = ActiveSheet.Shapes(1)
      cht8.Name = "chart008"
      ActiveSheet.ChartObjects("chart008").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart008").Width = 288
      ActiveSheet.Shapes("chart008").LockAspectRatio = msoTrue
  
  
      Range("B59:E59").Select
      ActiveSheet.Shapes.AddChart.Select
      ActiveChart.ChartType = xlColumnClustered
      ActiveChart.SetSourceData Source:=Range("Pivot!$A$59:$E$61")
      ActiveChart.ShowValueFieldButtons = False
      Dim cht9 As Shape
      Set cht9 = ActiveSheet.Shapes(1)
      cht9.Name = "chart009"
      ActiveSheet.ChartObjects("chart009").Activate
      ActiveChart.SeriesCollection(1).Select
      ActiveChart.SeriesCollection(1).ApplyDataLabels
      ActiveChart.SeriesCollection(2).Select
      ActiveChart.SeriesCollection(2).ApplyDataLabels
      ActiveChart.SeriesCollection(3).Select
      ActiveChart.SeriesCollection(3).ApplyDataLabels
      ActiveChart.ChartArea.Select
      ActiveSheet.Shapes("chart009").Width = 288
      ActiveSheet.Shapes("chart009").LockAspectRatio = msoTrue
  End Sub
  </code></pre>
  
  <p>I have 14 different pivot tables. Now how can I apply a for loop or any other loop to minimize the length of the code. I am new here so not finding out any solution for it.</p>