如果单元格值在列表中,则替换为第三个值

时间:2019-05-10 11:09:13

标签: excel vba

我正在尝试用替换值替换列表中的所有值。

EG

我似乎不知道要使用什么功能

Sub findandreplace()
Dim WIP As Range
Dim SrchRng As Range, cel As Range
Set SrchRng = Selection
Set WIP = Workbooks("Long life check").Sheets("WIP").Columns("C:D")

For Each cel In SrchRng
    'if cel.value is contained within WIP then replace with value from column D
Next cel
End Sub

一旦运行A11,将会是I001080。

1 个答案:

答案 0 :(得分:1)

尝试使用类似以下的内容

Sub findandreplace()
    Dim WIP As Range
    Dim SrchRng As Range, cel As Range
    Set SrchRng = Selection
    Set WIP = Workbooks("Long life check").Sheets("WIP").Columns("C:D")

    With ActiveSheet.Columns("A")
        For Each cel In SrchRng
            .Cells.Replace cel, cel.Offset(0, 1)
        Next cel
    End With
End Sub

选择“查找”范围并运行