title: Making a dynamic formula in Excel using two the FirstRow statement
body: <p>I'm using a line of code to place a formula inside a cell in Excel. I have the formula that works, but I want to make it dynamic, i'm using the FirstRow function to determine the array. The formula is as following:</p>
  
  <pre><code>Range("Q" &amp; FirstRow).Offset(1).Formula = "=IF(P43&lt;&gt;P44,1,0)"
  </code></pre>
  
  <p>I've tried placing the FirstRow statement inside the formula:</p>
  
  <pre><code>FirstRowOffset1 = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(1).Row
  FirstRowOffset2 = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(2).Row
  
  Range("Q" &amp; FirstRow).Offset(1).Formula = "=IF(P" &amp; FirstRowOffset1 &amp; ") &lt;&gt; (P" &amp; FirstRowOffset2 &amp; "),1,0"
  </code></pre>
  
  <p>And i've tried using a Set function to place in the formula:</p>
  
  <pre><code>FirstRowOffset1 = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(1).Row
  FirstRowOffset2 = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(2).Row
  
  Set fro1 = Worksheets("Compliance").Range("P" &amp; FirstRowOffset1)
  Set fro2 = Worksheets("Compliance").Range("P" &amp; FirstRowOffset2)
  
  Range("Q" &amp; FirstRow).Offset(1).Formula = "=IF( &amp; fro1 &amp;  " &lt;&gt; " &amp;  fro2 &amp; ),1,0"
  </code></pre>
  
  <p>But this only gives a true of false statement inside the cell, not the formula i'm looking for. How can I make the formula dynamic?</p>
  
  <pre><code>    LastRowInput = Worksheets("Input").Cells(Rows.Count, 1).End(xlUp).Offset().Row
      LastRowMatchC = Worksheets("Compliance").Cells(Rows.Count, 1).End(xlUp).Row
      LastRowSumC = Worksheets("Compliance").Cells(Rows.Count, 1).End(xlUp).Offset(0).Row 'Offset(-1,0)
      FirstRow = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Row
      FirstRowFill = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(1).Row
      FirstRowOffset1 = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(1).Row
      FirstRowOffset2 = Worksheets("WIP extract").Cells(Rows.Count, 1).End(xlUp).End(xlUp).Offset(2).Row
  
      '~~&gt; Autofill formules
      Range("P" &amp; FirstRow) = "Check"
      Range("Q" &amp; FirstRow) = "ID"
  
      Set frCP = Worksheets("Compliance").Range("P" &amp; FirstRowFill &amp; ":P" &amp; LastRowMatchC)
  
      Range("P" &amp; FirstRow).Offset(1).FormulaArray = "=IFERROR(INDEX(Input!$A$2:A$" &amp; LastRowInput &amp; ",MATCH(1,SEARCH(TRANSPOSE(Input!$A$2:A$" &amp; LastRowInput &amp; "),O43),0),0),""ZZ"")"
          Range("P" &amp; FirstRow).Offset(1).Select
              Selection.AutoFill Destination:=frCP
  
      Set frCQ = Worksheets("Compliance").Range("Q" &amp; FirstRowFill &amp; ":Q" &amp; LastRowMatchC)
      Range("Q" &amp; FirstRow).Offset(1).Formula = "=IF(P" &amp; FirstRowOffset1 &amp; " &lt;&gt; (P" &amp; FirstRowOffset2 &amp; ",1,0)"
  
          Range("Q" &amp; FirstRow).Offset(1).Select
              Selection.AutoFill Destination:=frCQ
  </code></pre>