首页 > 解决方案 > 如何在录制的宏中将单元格引用到替换代码中

问题描述

我正在尝试编写将职位标题列表转换为预定义标准职位的代码,这个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

我希望使用参考单元来更轻松地更新/维护文件

标签: excelvbaif-statementreplace

解决方案


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

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

推荐阅读