Home » excel » excel vba – Seemingly simple task of looping for a string in VBA

excel vba – Seemingly simple task of looping for a string in VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am having some trouble getting a loop to work in Excel VBA which searches cell E in each row for a string and then puts that string in cell N of that row. Code is below:

Dim i As Long
For i = 1 To Rows.Count
Next i
If Cells(i, 5).InStr(1, ActiveSheet.Range("ActiveCell").Value, "Smith") > 0 Then
Cells(i, 14).ActiveCell = "Smith"
End If

Can anyone please give insight as to where I am going wrong?

How to&Answers:

Try:

Dim i As Long
For i = 1 To Rows.Count
    If Cells(i, 5).InStr(1, ActiveSheet.Range("ActiveCell").Value, "Smith") > 0 Then
        Cells(i, 14).ActiveCell = "Smith"
    End If
Next i

You were ending your loop before it even did anything.

I think you also need to replace

Cells(i, 14).ActiveCell = "Smith"

With

Cells(i, 14).Value = "Smith"