title: How to write a for loop and if statement in this excel sheet
body: <p>I have been given an excel sheet with thousands of records. Instead of doing it manually, I am thinking of writing a <em>For Loop</em> and <em>If Statement</em> in VBA but I don't know how.</p>
  
  <p>First, take the <code>Date_Key</code> and if it is within the <code>Start Date</code> and <code>End Date</code>, print the <code>Current_Month</code> in <code>Period</code>.</p>
  
  <p>Then move to the next <code>Date_Key</code> and repeat. But if this <code>Date_Key</code> is the same as the previous one and the previous <code>Current_Month</code> has been used, go to next set of <code>Start Date</code> and <code>End Date</code> and if it is within the <code>Start Date</code> and <code>End Date</code>, use this <code>Current_Month</code> and insert it into the <code>Period</code>.</p>
  
  <p>Can someone please help me with this? Much appreciated! Thank you very much.</p>
  
  <pre><code>                    |------ THIS IS A REFERENCE TABLE ------|
  -------------------------------------------------------------
  | Period | Date_Key | Current_Month | Start Date | End Date |
  -------------------------------------------------------------
  |        | 20130914 | 2014 P02      | 20121001   | 20130929 |
  -------------------------------------------------------------
  |        | 20130914 | 2014 P03      | 20121029   | 20131027 |
  -------------------------------------------------------------
  |        | 20130914 | 2014 P04      | 20121203   | 20131201 |
  -------------------------------------------------------------
  |        | 20130926 | 2014 P05      | 20121231   | 20131229 |
  -------------------------------------------------------------
  |        | 20130926 | 2014 P06      | 20130128   | 20140126 |
  -------------------------------------------------------------
  </code></pre>
  
  <p>Expected Result:</p>
  
  <pre><code>                    |------ THIS IS A REFERENCE TABLE ------|
  -------------------------------------------------------------
  | Period | Date_Key | Current_Month | Start Date | End Date |
  -------------------------------------------------------------
  |2014 P02| 20130914 | 2014 P02      | 20121001   | 20130929 |
  -------------------------------------------------------------
  |2014 P03| 20130914 | 2014 P03      | 20121029   | 20131027 |
  -------------------------------------------------------------
  |2014 P04| 20130914 | 2014 P04      | 20121203   | 20131201 |
  -------------------------------------------------------------
  |2014 P02| 20130926 | 2014 P05      | 20121231   | 20131229 |
  -------------------------------------------------------------
  |2014 P03| 20130926 | 2014 P06      | 20130128   | 20140126 |
  -------------------------------------------------------------
  </code></pre>