title: fill formula down row with variable reference
body: <p>Good morning,</p>
  
  <p>Thanks in advance for any help you can give. I have written a code that uses a reference number in b6 on sheet "Y" to count the number of columns on sheet x based on that number and starts the formula 6 rows down. It then pastes a formula in in the proper cell in row six with the variable column.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>Sub testingme4()
  Dim myvalue As Double, lastrow As Long
  myvalue = Worksheets("x").Cells(6, 2).Value
  Worksheets("y").Cells(6, (myvalue) + 21).Cells.FormulaR1C1 = "formula"
  End Sub
  </code></pre>
  
  <p>This works perfectly for me.</p>
  
  <p>However, when I try to get it to paste down the entire row, it gets wonky.</p>
  
  <p>I added this: </p>
  
  <pre><code>Dim lastrow as long
  lastrow = Worksheets("y").Cells(Rows.Count, 1).End(xlUp).Row
  </code></pre>
  
  <p>Which also work and counts the correct number of rows (used 'MsgBox "Last row of data anywhere: " &amp; lastrow &amp; vbCrLf to verify)</p>
  
  <p>The last part is that I need to loop the formula until it hits the last row of the column picked in my first statement.</p>
  
  <p>I tried:</p>
  
  <pre><code>Dim paster as long,
  For paster = Worksheets("x").Cells(6, (myvalue) + 21) To lastrow
  Cells.FormulaR1C1 = "formula"
  Next paster
  </code></pre>
  
  <p>This keeps giving me a running out of memory error.</p>
  
  <p>Thanks again for any help.</p>
  
  <p>EP</p>