Vba excel 根据 table 值替换字符串

Vba excel replacing string based on table values

我有代码用 sheet "Filter".[=15= 的 B 列中写入的新值替换 sheet "Data" 中 A 列中的部分字符串]

sheet"Filter"的A列有旧值,B列有替换后的值,见图:

我知道如何将 "Griffith RV8" 替换为 "Griffith RV-8",但是我如何进行替换并删除被替换字符串前面的所有字符?

格里菲斯 RV8 = RV-8

Sub Substitutions()

Dim rngData     As Range
Dim rngLookup   As Range
Dim Lookup      As Range

With Sheets("Data")
    Set rngData = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With

With Sheets("Filter")
    Set rngLookup = .Range("A1", .Range("A" & Rows.Count).End(xlUp))
End With

For Each Lookup In rngLookup
    If Lookup.Value <> "" Then
        rngData.Replace What:=Lookup.Value, _
                        Replacement:=Lookup.Offset(0, 1).Value, _
                        LookAt:=xlPart, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=False
    End If
Next Lookup

End Sub

您可以使用通配符来替换字符串的一部分。

Sub Example()

    Range("A1") = "Where in the World is Carmen Sandiego?"

    Range("A1").Replace "*Carmen", "Who is Micheal"

    Range("A3") = "Hello World! How are You?"

    Range("A3").Replace "!*", "? Can you hear me!?"

    Range("A5") = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    Range("A5").Replace "??H??", "fgHij"

    ' You can escape the wildcards using "\"
    Range("A7") = "? not escaped"

    Range("A8") = "WHO? WHO!"

    Range("A8").Replace "WHO?", "WHO ARE YOU?"

    Range("A10") = "? was escaped using a backslash \?"

    Range("A11") = "WHO? WHO!"

    Range("A11").Replace "WHO\?", "WHO ARE YOU?"

End Sub
rngData.Replace What:= "* " & Lookup.Value, _
                Replacement:=Lookup.Offset(0, 1).Value, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    MatchCase:=False

[some text here][space]Lookup.Value 替换为 Lookup.Offset(0, 1).Value