title: VBA Exporting Chart Arrays on Button Click
body: <pre><code> Sub onButtonClick()
  
  Dim source As Worksheet, target As Worksheet
  Set source = Workbooks("End Market Monitor.xlsm").Worksheets("Aero Graphs")
  Set target = Sheet1
  
  Dim ws As Worksheet
  Dim title_name As String, search As String
  
  
  
  search = ActiveCell.Offset(0, -5).Value
  ReDim chartArray(1 To source.ChartObjects.Count) As Chart
  For i = 1 To source.ChartObjects.Count
      title_name = source.ChartObjects(i).Chart.ChartTitle.Text
      counter = 1
      If InStr(title_name, search) &gt; 0 Then
          Set chartArray(counter) = source.ChartObjects(i).Chart
          counter = counter + 1
      End If
  
  Next
  Set wsTemp = Sheets.Add
  
      tp = 10
  
      With wsTemp
          For n = 1 To UBound(chartArray)
              chartArray(n).CopyPicture
              wsTemp.Range("A1").PasteSpecial
              Selection.Top = tp
              Selection.Left = 5
              tp = tp + Selection.Height + 50
          Next
      End With
  
      wsTemp.ExportAsFixedFormat Type:=xlTypePDF, Filename:=NewFileName, Quality:=xlQualityStandard, _
             IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
  End Sub
  </code></pre>
  
  <p>I'm having trouble with the search variable. I keep getting object defined error and I have no idea why... Should I be using Cells.Offset?</p>
  
  <p>Also, when I loop through the array of charts, I am unable to use the for each loop so I had to loop through from 1 to the upper bound of the array. Is this viable?</p>