Loop through range of cells, string search, take action

  • Thread starter Thread starter phil.emmott
  • Start date Start date
P

phil.emmott

Hi there

I'm pretty new to this vba stuff, so hopefully this is not a difficult
request!

I'm after a code snippet that will loop through a range of cells - say
E2:H40 looking for strings in cells. In this case I'm looking for bits
of addresses. if any of the cells in that range on any particular row
contain "Preston" or "PR2 4JK" i'd like the macro to then put "1772" in
the 'I' cell on that row.

the cells may contain more characters than the "Preston", so as long as
the cell contains that string, the if/case statement should be true.

Thanks
 
Sub markit()
Dim r As Range
For Each r In ActiveSheet.UsedRange
If InStr(r.Value, "Preston") Or InStr(r.Value, "PR2 4JK") Then
Cells(r.Row, "I").Value = 1772
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top