title: Calculating values using loops and formulas with VBA
body: <p><a href="https://i.stack.imgur.com/qGIwU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qGIwU.png" alt="enter image description here"></a></p>
  
  <p>I am new with VBA for Excel as I work on a project that involves lots of calculations, which I want to automate with VBA. My data ranges from 15 months. To do this, I use loops and formulas but the code results into diferent results. Spefically:</p>
  
  <p>1 - I would like to calculate the sum of values for next month and show the result in the last day of previous month. This is done in column <code>H11</code>.</p>
  
  <p>2- I would like to use this formula <code>=H11-G11-F11</code> in the last day of the month, after having determined the value of <code>H11</code>.</p>
  
  <p>3- I want to calculate the values of next month following the same logic expect that, in this case my formula depends on the results from previous months. For instance: <code>G29=L11+L112*K31;</code> I know the value in <code>K31</code>.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>Dim i As Long
  Dim lrow As Long
  Dim start_dif As Double
  
  lrow = Cells(Rows.Count, "A").End(xlUp).Row
  For i = 26 To lrow
      data = Cells(i, "A")
      next_date = Cells(i + 1, "A")
      If Month(next_date) = Month(data) + 1 Or _
          Year(next_date) = Year(data) + 1 Then
          'first diference
          start_dif = Cells(i, "I").Value
          'Calculating value in G for each end of month
          Cells(i, "G").Value = start_dif + start_dif * Cells(i, "K").Value
          'setting up next start_dif
          start_dif = Cells(i + 1, "I").Value
      End If
  Next i
  </code></pre>
  
  <p>Please note that I could not figured out the code for calculating the sum. Let me say how grateful I am for your help. </p>