在另一个工作表上使用vlookup查找并替换值

时间:2014-04-03 11:08:09

标签: excel vba excel-vba

我有多个包含文本数据的单元格,并希望用其他工作表中的数据替换某些数据。

示例

第1页

  

A1 =" aa * a"

     

B1 =" b] bb"

     

C1 =" cae *"

第2页

  

A1 =" Q" B1 ="报价"

     

A2 =" E" B2 ="实施例"

结果

A1保持不变,B1保持不变,C1更改为caExample*

我想我需要一个VBA代码来查找并替换为VLookUp

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

Sub PolyChange()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Dim I As Long, J As Long, v1 As String
    Dim N1 As Long, N2 As Long, v2 As String, v3 As String
    N1 = s1.Cells(Rows.Count, "A").End(xlUp).Row
    N2 = s2.Cells(Rows.Count, "A").End(xlUp).Row
    For I = 1 To N1
        v1 = s1.Cells(I, "A")
        For J = 1 To N2
            v2 = s2.Cells(J, "A")
            v3 = s2.Cells(J, "B")
            v1 = Replace(v1, v2, v3)
        Next J
        s1.Cells(I, "A").Value = v1
    Next I
End Sub