The code below works - if string abcd
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
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
Same code with if-statement does not work as shown below - nothing prints in column X, why? What is wrong with the if-statement?
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