title: VBA Variable not defined when calling Method with Object
body: <p>Hi I develop the first time using VBA (Was using vb.net etc. before).
  I always get the error </p>
  
  <blockquote>
    <p>Variable is not defined</p>
  </blockquote>
  
  <p>when running the code.
  On calling the method <code>writeHeaders</code> the program always quits.</p>
  
  <p>Some help would be highly appreciated, this thing drives me nuts atm.</p>
  
  <pre><code>Option Explicit
  
  Sub exportToAPplus()
  
      Dim applusImport As Workbook
      Set applusImport = createFile
      writeHeaders applusImport
  End Sub
  
  Function createFile() As Workbook
  
      Dim dateiName As String
      dateiName = Application.GetSaveAsFilename()
      If dateiName &lt;&gt; "Falsch" And dateiName &lt;&gt; "False" Then
          Dim applusImport As Workbook
          dateiName = dateiName + "xlsx"
          Set applusImport = Workbooks.Add
          Application.DisplayAlerts = False
          applusImport.SaveAs dateiName, FileFormat:=xlOpenXMLWorkbook
          Application.DisplayAlerts = True
          Set createFile = applusImport
      Else
          MsgBox ("Sie haben keinen Dateinamen ausgewählt, es wurde nichts 
          gespeichert.")
      End If
  
  End Function
  
  Sub writeHeaders(applusImport As Workbook)
      Dim headers() As Variant
      headers() = Array("DATUM", "AUFTRAG", "PERSONAL", "KSTR", "INNENAUFTRAG", "POSITION", "AG", "KAPAST", "MASCHINENGRUPPE", "START", "ENDE", "DAUER", "BESCHREIBUNG")
      Application.ScreenUpdating = False
      For i = LBound(headers()) To UBound(headers()) Step 1
          .Cells(1, 1 + i).Value = headers(i)
      Next i
      applusImport.Save
      Application.ScreenUpdating = True
  End Sub
  </code></pre>