title: Updating Excel Header with Visual Basic
body: <p>I have a macro that creates PDF files from a data sheet in Excel.  Another sheet in Excel is an invoice template.  The macro fills in different cells on the template sheet and saves the results as a PDF file.  The template sheet has a header that needs to be updated each time based on the vendor.  I have the code below that updates the header.  It works when I step through the code but not when I run the macro.  </p>
  
  <p>I'm not sure why.  Any suggestions to get this to work would be greatly appreciated.  Thanks....</p>
  
  <pre><code>Application.PrintCommunication = False
  
  Worksheets("PrintInvoice").PageSetup.LeftHeader = ""
  Worksheets("PrintInvoice").PageSetup.CenterHeader = ""
  Worksheets("PrintInvoice").PageSetup.CenterHeader = Header1 &amp; Chr(10) &amp; Header2 &amp; Chr(10) &amp; Header3
  Worksheets("PrintInvoice").PageSetup.RightHeader = "Page &amp;P of &amp;N"
  Worksheets("PrintInvoice").PageSetup.LeftFooter = _
          " ©  2013 Proprietary and Confidential   Reproduction/Distribution Prohibited "
  Worksheets("PrintInvoice").PageSetup.CenterFooter = ""
  Worksheets("PrintInvoice").PageSetup.RightFooter = ""
  Worksheets("PrintInvoice").PageSetup.LeftMargin = Application.InchesToPoints(0.75)
  Worksheets("PrintInvoice").PageSetup.RightMargin = Application.InchesToPoints(0.75)
  Worksheets("PrintInvoice").PageSetup.TopMargin = Application.InchesToPoints(1)
  Worksheets("PrintInvoice").PageSetup.BottomMargin = Application.InchesToPoints(1)
  Worksheets("PrintInvoice").PageSetup.HeaderMargin = Application.InchesToPoints(0.5)
  Worksheets("PrintInvoice").PageSetup.FooterMargin = Application.InchesToPoints(0.5)
  Worksheets("PrintInvoice").PageSetup.Zoom = 91
  Worksheets("PrintInvoice").PageSetup.PrintErrors = xlPrintErrorsDisplayed
  Worksheets("PrintInvoice").PageSetup.OddAndEvenPagesHeaderFooter = False
  Worksheets("PrintInvoice").PageSetup.DifferentFirstPageHeaderFooter = False
  Worksheets("PrintInvoice").PageSetup.ScaleWithDocHeaderFooter = True
  Worksheets("PrintInvoice").PageSetup.AlignMarginsHeaderFooter = False
  Worksheets("PrintInvoice").PageSetup.EvenPage.LeftHeader.Text = ""
  Worksheets("PrintInvoice").PageSetup.EvenPage.CenterHeader.Text = ""
  Worksheets("PrintInvoice").PageSetup.EvenPage.RightHeader.Text = ""
  Worksheets("PrintInvoice").PageSetup.EvenPage.LeftFooter.Text = ""
  Worksheets("PrintInvoice").PageSetup.EvenPage.CenterFooter.Text = ""
  Worksheets("PrintInvoice").PageSetup.EvenPage.RightFooter.Text = ""
  Worksheets("PrintInvoice").PageSetup.FirstPage.LeftHeader.Text = ""
  Worksheets("PrintInvoice").PageSetup.FirstPage.CenterHeader.Text = ""
  Worksheets("PrintInvoice").PageSetup.FirstPage.RightHeader.Text = ""
  Worksheets("PrintInvoice").PageSetup.FirstPage.LeftFooter.Text = ""
  Worksheets("PrintInvoice").PageSetup.FirstPage.CenterFooter.Text = ""
  Worksheets("PrintInvoice").PageSetup.FirstPage.RightFooter.Text = ""
  
  Application.PrintCommunication = True
  
      Sleep 2000
  </code></pre>