如何将单元格引用到录制的宏中的替换代码中

时间:2019-06-11 18:49:22

标签: excel vba if-statement replace

我正在尝试编写将职位名称列表转换为预定义标准职位的代码,IF函数可能太大。 我想到的唯一方法是通过将“ this”替换为“ that”并在所有位置执行此操作来记录宏,但是如果可以在代码中引用单元格而不是文本字符串,则将容易得多:

What:="Field Installation Supervisor" (This part should be a cell reference from another sheet)
Replacement:= _ "Installation Supervisor" (This part is also a referenced cell from the same sheet as the above)

不知道是否可以在此替换代码中使用单元格引用

Sub replacing()  

    Sheets("Active Employees June 01 2019").Select
    Cells.Replace What:="Field Installation Supervisor", Replacement:= _
        "Installation Supervisor", LookAt:=xlPart, SearchOrder:=xlByRows, _
        MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End Sub

我希望使用参考单元使文件的更新/维护更加容易

1 个答案:

答案 0 :(得分:0)

只需以常规方式引用单元格即可(无需选择工作表)。

Sheets("Active Employees June 01 2019").Cells.Replace _
    What:=Sheets("this").Range("A1").Value, _
    Replacement:=Sheets("this").Range("A2").Value, _
    LookAt:=xlPart, SearchOrder:=xlByRows, _
    MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False