title: Comparing Time values in VBA throws Run-time error 13 'type mismatch'
body: <p>Am trying to compare system time with the time stored in 2 adjacent cells. I tried the below comparison of times and it just works fine...</p>
  
  <p>*<strong><em>this code worked fine</em>*</strong>
  Sub btn_Click()</p>
  
  <pre><code> MsgBox Range("C4"), vbOKOnly, "Test"
   Dim time1 As Date
   Dim time2 As Date
   time1 = Time
   time2 = TimeValue(Cells(3, 1).Text)
   If time1 &lt; time2 Then
        MsgBox str, vbOKOnly, "MsgIf"
   Else
        MsgBox str1, vbOKOnly, "MsgElse"
   End If
  </code></pre>
  
  <p>End Sub</p>
  
  <hr>
  
  <p>but this doesn't work!!! the If condition throws error saying 'type mismatch'.
  Please help!!!</p>
  
  <pre><code>Dim i As Integer
  Dim w As Integer
  Dim tm1 As Date
  Dim tm2 As Date
  Dim tm3 As Date
  tm1 = Time
  
  For i = 2 To 31
      tm2 = TimeValue(Cells(i, 1).Text)
      tm3 = TimeValue(Cells(i, 2).Text)
  
      If tm1 &gt; tm2 &amp; tm1 &lt; tm3 Then ' this is the line that throws 'type mismatch' error
          Exit For
      End If
      Next i
  
  
  MsgBox Cells(i, w), vbOKOnly, "Test"
  </code></pre>