title: SUM until last row using VBA to add formula in Excel
body: <p>I got problem with SUM. The idea is i want to use my own varible to SUM certain range.</p>
  
  <p>This is my code</p>
  
  <pre><code>Range("B299").Activate
  ActiveCell.FormulaR1C1 = "=SUM(R[-298]C:R[-1]C)"
  </code></pre>
  
  <p>i want to use lastrow variable to change the value of SUM</p>
  
  <p>Here is my lastrow declaration, i create this variable on the other sub and i call it before i want to use SUM</p>
  
  <p><code>lastrow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row</code></p>
  
  <p>When i tried to run the macro, it showed :</p>
  
  <blockquote>
    <p>run-time error '1004': .... </p>
  </blockquote>
  
  <p>edited :
  i encountered another problem. It's about find lastrow function.</p>
  
  <p>Let's say when i tried to run macro first time, it recorded lastrow as 300. But when i tried to run it second time, which is for example i have 200 row of data, the findlastrow function still recorded it as 300 instead of 200.</p>
  
  <p>this is my findlastrow function</p>
  
  <pre><code>Sub FindLastRow()
  
  lastrow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
  
  MsgBox "Last Row this sheet is " &amp; lastrow
  </code></pre>
  
  <p>the lastrow variable, I declared it as global variable in Module1</p>
  
  <pre><code>Public lastrow As Integer
  </code></pre>
  
  <p>I wonder what's wrong with the code</p>