如何使用For循环替换偏移单元格

时间:2014-10-31 12:57:32

标签: excel vba excel-vba

所以我编写了一个For循环代码,试图搜索包含以“GE90”开头的描述的单元格的特定列(列M),并用“GE90 Hold”替换adjecent偏移单元格(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

2 个答案:

答案 0 :(得分:5)

您的问题实际上是假设"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

答案 1 :(得分:4)

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
相关问题