title: How to add a Formula To Cell using VBA
body: <p>I am attempting to write some VBA which will add header text to 3 cells then fill a formula all the way down to the last row.  I have written the below, which writes the headers no problems, but when it get's to my first <code>.Formula</code> it throws a </p>
  
  <blockquote>
    <p>Application Defined or Object Defined error</p>
  </blockquote>
  
  <p>What needs to be altered so that this macro will execute successfully?  (The formulas were pulled directly from the formula in the cell, so I know they are valid formulas at least on the "front-end")</p>
  
  <pre><code>Function Gre()
      Range("E2").Select
      ActiveCell.FormulaR1C1 = "Under"
      Range("F2").Select
      ActiveCell.FormulaR1C1 = "Over"
      Range("G2").Select
      ActiveCell.FormulaR1C1 = "Result"
  
      With Range("E2:E" &amp; Cells(Rows.Count, "C").End(xlUp).Row)
          .Formula = "=IF(C2&lt;B2,B2-C2,"")"
      End With
      With Range("F2:F" &amp; Cells(Rows.Count, "C").End(xlUp).Row)
          .Formula = "=IF(C2&gt;B2,C2-B2,0)"
      End With
      With Range("G2:G" &amp; Cells(Rows.Count, "C").End(xlUp).Row)
          .Formula = "=IF(F2&gt;0,'Issue',"")"
      End With
  End Function
  </code></pre>