How to use For Loop to replace an offset cell

So I wrote a For Loop code that tries to search a specific column (column M) of data for cells that contain a description that starts with "GE90" and replaces the adjacent offset cell (column C) with "GE90 Hold".

I thought I was using the code correctly, but for some reason it doesn't work.

Dim Cell
For Each Cell In Range("M2:M" & LastRow)
    If Cell.Value = "GE90*" Then
        Cell.Offset(, -10).Value = "GE90 Hold"
    End If

Next Cell

      

+3


source to share


2 answers


Cell.Offset(, -10).Value

if you want to write in Col C

correctly, but why not simplify the approach?

Range("C" & cell.row).Value = "GE90 Hold"

      

or



Cells(cell.row, 3).value '<~~ As enderland suggested in the comment

      

The problem is with your If condition. Change it to

If Cell.Value Like "GE90*" Then

      

+4


source


Your problem is that you are assuming the asterisk in "GE90*"

is a wildcard, but your code is actually looking for a literal value "GE90*"

. Change your code like this:



Dim Cell
For Each Cell In Range("M2:M" & lastrow)
    If Left(Cell.Value, 4) = "GE90" Then
        Cell.Offset(, -10).Value = "GE90 Hold"
    End If

Next Cell

      

+5


source







All Articles