【问题标题】:replacing many words every one with alternative word用替代词替换每个单词
【发布时间】:2017-10-02 16:06:34
【问题描述】:

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

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

我有三列:

  1. 第一列有句我要替换的单词
  2. 第二列有我要替换的所有单词
  3. 在第三列的旁边是替换词或新标签。


+---+-------------------------+----------------------------+---------------------------+
|   |            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 |
+---+-------------------------+----------------------------+---------------------------+

【问题讨论】:

  • 你可以使用SUBSTITUTE()
  • 在您的示例中,我在您的句子 word1 中看不到 word3。这只是示例中的错误,还是我误解了您的问题?

标签: excel excel-formula vba


【解决方案1】:

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

【讨论】:

  • 如果您想为旧文本和新文本选择整列,我建议在 FindReplace = Replace(FindReplace, Old_T(i, 1), New_T(i, 1), 1) 行之前添加 If Old_T(i, 1) = vbNullString Then Exit For 注意:如果您要在旧文本中的值之间添加空格列不要这样做。 – 约书亚芬纳 1 小时前
【解决方案2】:

您可以使用以下 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

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

here得到这个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-15
    • 2012-09-21
    • 1970-01-01
    相关资源
    最近更新 更多