title: VBA Excel - Summing a range of cells based on result in a different range of cells
body: <p>I am attempting create a forecasting function in excel using based on a set of preregistered historical data. I am new to VBA and I am unable to make this function.</p>
  <p>I have three Ranges:</p>
  <ul>
  <li>Range 1 lists every day in a year. All cells are formatted in the form of &quot;date&quot;.</li>
  <li>Range 2 on a different sheet named &quot;C&quot; also lists every day in a year. All cells are formatted in the form of &quot;date&quot;.</li>
  <li>Range 3 on sheet &quot;C&quot; contains the dollar value actualized in the specific date of the year mentioned in Range 2.</li>
  </ul>
  <h3>Requirement:</h3>
  <p>The function should take 3 different variables which are the three different ranges. The Function should first &quot;<code>LOOKUP</code>&quot; the month and year of the selected cell and match it with the month of and (year -1) of the cells in Range two.</p>
  <p>Accordingly, The cells in Range 3 on the same row in which the &quot;<code>LOOKUP</code>&quot; matches with Range 2 should sum up and then divide by the count of cells counted.</p>
  <p>So far I have been able to create a function named <code>MNAME</code>.</p>
  <pre class="lang-vb prettyprint-override"><code>Function MNAME(x As Variant) As String
    Dim CurrentMonth As Date
    CurrentMonth = x
    MNAME = MonthName(Month(CurrentMonth), True)
  End Function
  </code></pre>
  <p>But I am failing to nest the lookups and sum up the values.</p>