ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop through range of cells, string search, take action (https://www.excelbanter.com/excel-programming/376636-loop-through-range-cells-string-search-take-action.html)

[email protected]

Loop through range of cells, string search, take action
 
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


Gary''s Student

Loop through range of cells, string search, take action
 
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

--
Gary''s Student


" wrote:

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




All times are GMT +1. The time now is 10:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com