如何使用For循环replace偏移单元

于是我写了一个For循环代码,它试图search包含以“GE90”开始的描述的单元格的特定列(列M)的数据,并用“GE90 Hold”代替双眼单元(列C)。

我以为我正确使用代码,但由于某种原因,似乎并没有工作。

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 

Cell.Offset(, -10).Value如果你想写给Col C是正确的,但为什么不是一个简单的方法?

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

要么

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

问题是你的If条件。 将其更改为

 If Cell.Value Like "GE90*" Then 

你的问题实际上是假定"GE90*"的星号是通配符,但实际上,你的代码正在寻找字面值"GE90*" 。 更改您的代码,如下所示:

 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