查找并替换多个不同的单词

时间:2014-06-22 11:24:21

标签: excel excel-vba vba

我想找到&用一个命令的同义词替换列表中的100个单词,例如word" go"用"移动","爱"与#34;浪漫","遥远"使用" remote"," subject"用"问题"等

2 个答案:

答案 0 :(得分:0)

这是一个很小的样本,可以帮助您入门:

Sub xLator()
    from = Array("go", "love", "distant")
    too = Array("move", "romance", "remote")
    For i = LBound(from) To UBound(from)
        Cells.Replace What:=from(i), Replacement:=too(i)
    Next i
End Sub

当然,您会扩展翻译阵列以满足您的需求,如果需要,可能会限制范围。

修改#1:

Sheet1 包含数据, Sheet2 包含 A B 列中的转换表。将输入表并应用它。:

Sub xLator2()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim N As Long, i As Long
    Dim from(), too()
    Set s1 = Sheets("Sheet1") '   contains the data
    Set s2 = Sheets("Sheet2") '   contains the translation table

s2.Activate

    N = Cells(Rows.Count, 1).End(xlUp).Row
    ReDim from(1 To N)
    ReDim too(1 To N)
    For i = 1 To N
        from(i) = Cells(i, 1).Value
        too(i) = Cells(i, 2).Value
    Next i

s1.Activate

    For i = LBound(from) To UBound(from)
        Cells.Replace What:=from(i), Replacement:=too(i)
    Next i
End Sub

答案 1 :(得分:-1)

您必须使用Excel VBA才能执行此操作。或者,如果您了解Perl,则可以使用Perl读取/写入XLS文件。由于我不了解Excel VBA,因此我使用Perl来执行此操作。