replacing many words every one with alternative word

生来就可爱ヽ(ⅴ<●) 提交于 2020-07-09 17:55:28

问题


I want to replace many words with alternative Tags(or words). Every word has a specific tag but it will take so much time to do it with find and replace.

Can any formula help with this situation?

I have three columns:

  1. the first column have sentence with the word I want to replace
  2. the second column have all words I want to replace
  3. next to it in the third column the replacement word or new tag.


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

回答1:


A UDF does the trick

After investigating =substitute() in the array form I settled on a UDF. Paste the following code into a new module in the workbook you need this done in.

In excel you can use the function as any other function. =FindReplace( : hit ctrl-shift-a and it will ask you for the criteria.

Example: =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



回答2:


You can use following 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

As per the image below enter =SubstituteMultiple(A2,$B$2:$B$6,$C$2:$C$6) in Cell D2 then drag/copy down as reuired.

Got this from here.



来源:https://stackoverflow.com/questions/46529366/replacing-many-words-every-one-with-alternative-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!