使用 VBA 替换多个值 Excel

Substitute Multi Values Excel using VBA

我在 VBA 的帮助下创建了一个工作函数(由其他人编写)来替换多个字符串,我的问题是它替换了第一个实例而不是字符串的完整值,我将解释举个例子。

旧值

  1. 汽车
  2. 汽车圆胎

新值

  1. 自行车
  2. 公交车

现在这个函数可以用自行车替换汽车,但是当涉及到 "Car Tyre" 时,它用自行车替换汽车并忽略 "Tyre" 给我最终输出为 "Bike Round Tyre" 但答案应该是巴士

Function SubstituteMultiple(text As String, old_text As Range, new_text As Range)
Dim i As Single
For i = 1 To old_text.Cells.Count
    Result = Replace(text, old_text.Cells(i), new_text.Cells(i))
    text = Result
Next i
SubstituteMultiple = Result
End Function

此功能非常有用,但需要完善。

此致

如果考虑到完整的单元格值是可以的,那么:

Function SubstituteMultiple(text As String, old_text As Range, new_text As Range)

    Dim i As Single, Result as String
    Result = text
    For i = 1 To old_text.Cells.Count
        If text = old_text.Cells(i).Value Then
            Result = new_text.Cells(i).Value
            Exit For
        End If
    Next i
    SubstituteMultiple = Result

End Function