title: Excel VBA Code Does Not Work With If Statement
body: <p>The code below works - if string <code>abcd</code> is found in column E then it's position is printed in the same row in column X, if not found then it prints 0</p>
  
  <pre><code>Sub SearchInColumn()
      Dim LastRow As Integer
      Dim SrchIn As String
      Dim SrchFor As String
  
      LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
  
      For i = 2 To LastRow
          SrchIn = Sheet1.Cells(i, 5).Value
          SrchFor = "abcd"
          'If SrchFor = "abcd" Then
          Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
          'End If
      Next i
  End Sub
  </code></pre>
  
  <p>Same code with if-statement does not work as shown below - nothing prints in column X, why?  What is wrong with the if-statement?</p>
  
  <pre><code>Sub SearchInColumn()
      Dim LastRow As Integer
      Dim SrchIn As String
      Dim SrchFor As String
  
      LastRow = Sheet1.Cells(Rows.Count, "E").End(xlUp).Row
  
      For i = 2 To LastRow
          SrchIn = Sheet1.Cells(i, 5).Value
          'SrchFor = "abcd"
          If SrchFor = "abcd" Then
              Sheet1.Cells(i, "X").Value = InStr(SrchIn, SrchFor)
          End If
      Next i
  End Sub
  </code></pre>