title: MS Excel: Finding the earliest date on a row of mixed content
body: <p>Does anyone know how to get the earliest date on a row in Microsoft Excel. There is not a predictable number of columns on each row, and there are values other than dates which need to be ignored. It could be done with Excel formulas or VBA.</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p>Right now I am using this quick and dirty VBA function, but when I loaded new input data (approx 200 rows by 100 columns) a message box came up saying that Excel does not have enough resources to process my changes.</p>
  
  <pre><code>' returns smallest date on row
  Public Function getSmallestDateFromRow(r As Integer, sheetName As String) As Date
      Dim toReturn As Date
      Dim rng As Range
      Dim scanToColumn As Integer
      Dim c As Integer
      Dim tmp As Variant
  
      Set rng = Sheets(sheetName).Cells.Find("*", [a1], , , xlByColumns, xlPrevious) 'is this inefficient?
      scanToColumn = rng.Column
      toReturn = #12/12/2100#
  
      For c = 1 To scanToColumn
          tmp = Sheets(sheetName).Cells(r, c).Value
          If (IsDate(tmp)) Then
              If (toReturn = Null) Then
                  toReturn = tmp
              ElseIf (toReturn &gt; tmp) Then
                  toReturn = tmp
              End If
          End If
      Next c
  
      If (toReturn = #12/12/2100#) Then
         toReturn = 0
      End If
  
      getSmallestDateFromRow = toReturn
  End Function
  </code></pre>