title: VBA/excel Date comparison from text box
body: <p>I need to be able to enter a date range in two textboxes (‘txtDateFrom’ and ‘txtDateTo’) on a VBA form (I’ve already created the VBA form in Excel).  I then need to select individual rows from Sheet1 (see screenshot of Sheet1) based on column B and the date criteria from my two textboxes.  </p>
  
  <p>I intend using  the ‘click’ event of a button on my VBA form (cmdExtractData) to run the code.  I would then like to put this extracted data into Sheet2 of my spreadsheet so I can do further analysis on it.  Sheet2 will therefore look exactly like Sheet1, but with only those rows of data that match the date criteria selected.</p>
  
  <p>I’m comfortable doing all the necessary error checking (making sure the dates are valid etc).</p>
  
  <p>Spreadsheet Data:</p>
  
  <pre><code>1,19/07/2015,1,F,P,White Goods,One Off,£250.00
  2,24/08/2015,2,D,A,Handyman Services,Ongoing,£500.00
  3,21/07/2015,3,W,L,Home Assistance,One Off,£750.00
  4,01/09/2015,4,F,C,Convalescent/Respite,One Off,£250.00
  5,17/06/2015,5,D,H,Living Expenses,Ongoing,£500.00
  6,29/11/2015,1,F,O,Specialist Equipment,One Off,£250.00
  7,12/12/2015,4,D,O,Convalescent/Respite,One Off,£250.00
  8,23/01/2016,2,D,L,Transport Costs,One Off,£500.00
  9,27/02/2016,4,W,L,Living Expenses,One Off,£500.00
  10,03/11/2015,4,F,C,Convalescent/Respite,One Off,£750.00
  </code></pre>
  
  <hr>
  
  <p>OK, after much fiddling I have the basis of something that works...</p>
  
  <pre><code>' Clear Sheet2 ready for new data
  Sheet2.Cells.ClearContents
  
  ' First find the last row in the spreadsheet that has data in it.
  LastRowFrom = Range("B" &amp; Rows.Count).End(xlUp).Row
  
  'Loop for each entry in column B
  For i = 2 To LastRowFrom
    'get the next date from column B
     TempDate = Range("B" &amp; i).Value
  
     If TempDate &gt;= txtDateFrom.Text And TempDate &lt;= txtDateTo.Text Then
     ' Write code here if the date is in the selected range
      Range("A" &amp; i).EntireRow.Copy
      Sheet2.Range("A" &amp; i).End(xlUp).Offset(1).PasteSpecial
      Sheet1.Select
    End If
  Next i
  </code></pre>
  
  <p>This works but I can't help feeling that this is too simple.  Should I be checking for anything whilst this code is executing.  Are there any loose ends I need to tie up etc?</p>