用替代词替换每一个词

时间:2017-10-02 16:06:34

标签: excel excel-vba excel-formula vba

我想用替代标签(或单词)替换很多单词。每个单词都有一个特定的标签,但是查找和替换它需要花费很多时间。

任何公式可以帮助解决这种情况吗?

我有三列:

  1. 第一列的句子带有我要替换的单词
  2. 第二列包含我要替换的所有单词
  3. 在第三栏旁边的替换字或新标签。

  4. +---+-------------------------+----------------------------+---------------------------+
    |   |            A            |             B              |             C             |
    +---+-------------------------+----------------------------+---------------------------+
    | 1 | sentence word1 sentence |                 word3      |                      tag3 |
    | 2 | sentence word2 sentence |                 word6      |                      tag6 |
    | 3 | sentence word3 sentence |                 word8      |                      tag8 |
    | 4 | sentence word4 sentence |                 word9      |                      tag9 |
    | 5 | sentence word5 sentence |                 word10     |                     tag10 |
    +---+-------------------------+----------------------------+---------------------------+
    

2 个答案:

答案 0 :(得分:0)

UDF可以解决这个问题

在调查数组形式的=substitute()之后,我选择了UDF。将以下代码粘贴到您需要完成的工作簿中的新模块中。

在excel中,您可以将该功能用作任何其他功能。 =FindReplace(:点击ctrl-shift-a,它会询问你的标准。

示例:=FindReplace(A1,$B$1:$B$2,$C$1:$C$2)

    Function FindReplace(Cell As Range, Old_Text As Range, New_Text As Range) As String

    Dim i As Long, text As String, Old_T() As Variant, New_T() As Variant

    FindReplace = Cell.Value
    Old_T = Old_Text.Value
    New_T = New_Text.Value

    For i = LBound(Old_T) To UBound(Old_T)
        FindReplace = Replace(FindReplace, Old_T(i, 1), New_T(i, 1), 1)
    Next i

    End Function

答案 1 :(得分:0)

您可以使用以下UDF

Function SubstituteMultiple(str As String, old_txt_rng As Range, new_txt_rng As Range)
    Dim i As Single
    For i = 1 To old_txt_rng.Cells.Count
        Result = Replace(LCase(str), LCase(old_txt_rng.Cells(i)), LCase(new_txt_rng.Cells(i)))
        str = Result
    Next i
    SubstituteMultiple = Result
End Function

根据下面的图片,在=SubstituteMultiple(A2,$B$2:$B$6,$C$2:$C$6)中输入Cell D2,然后按需要拖动/复制。

enter image description here

来自here

相关问题