title: Find closest Date to specific Date
body: <p>I have two Tables. </p>
  
  <p>Table1 goes from A1:F10 and shows the machine assignment.<br>
  Table2 goes from G1:K10 and shows the storage for the machines.</p>
  
  <p>With a button I want to simulate which storage should be used for which machine.</p>
  
  <p>In column C stands the date when the machine has to be built. In Column I stands the date when the storage is ready to use. </p>
  
  <p>For example: The first machine has to start on 08/15/2018. How can I check which date in Column I is the closest to 08/15/2018?</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>Private Sub CommandButton1_Click()
      Dim lastrow as Long
  
      lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
  
      for a = 1 to lastrow
          If Cells(a, 1) = "Machine Name" And _    ' Find the specific machine
          Cells(a, 4) = "" Then                    ' In this cell the serial number of the storage should be added
              ' Now check if Storage for this machine is ready to use.
              For b = 1 to lastrow
                  If Cells(b, 8) = "123" And _    ' Serial Number of the Storage
                  Cells(b, 10) = "" Then          ' In this Cell serial number of the machine should be added
                      ' Here it should check which Date in Column I is the closest to the date in Column C
                      Cells(a, 4).Value = Cells(b, 8)
                      Cells(b, 10).Value = Cells(a, 2)
                 End If
             Next b
         End If
      Next a
  End Sub
  </code></pre>
  
  <p>I tried to change the code from <a href="https://stackoverflow.com/questions/30764899/find-closest-date-to-current-date-in-vba">Find closest date to current date in VBA</a>.</p>
  
  <p>In the picture you can see an example how the table looks:</p>
  
  <p><a href="https://i.stack.imgur.com/dE5In.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dE5In.jpg" alt="enter image description here"></a></p>