在excel中替换文本

时间:2013-06-04 20:37:27

标签: excel replace substitution

我有一个excel工作表,一些单元格包含一个缩写,如SC,FEW,......后跟一个数字。例如SC058或FEW01,并且一些单元包含这些中的两个或三个的组合,即FEW004 SC044。 我想用一个值替换每个缩写而不管它们附加的数字。例如,我希望将所有“FEW004,FEW056,......”替换为数字“5”等。

由于数据库很大,有关如何做到这一点的任何建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我做了一个快速的样本,可以进行一种替换(你的例子是用“FEW”改为“5”),希望它有一些用处:

Sub substitute_abb()

theData = ActiveCell.Value
theNewData = ""

theDataArray = Split(theData, " ")

For Each x In theDataArray
    If InStr(x, "FEW") Then
        newSegment = "5"
        Else
        newSegment = x
    End If
    If theNewData = "" Then
        theNewData = newSegment
        Else
        theNewData = theNewData & " " & newSegment
    End If
Next

ActiveCell.Value = theNewData

End Sub

我意识到对于拆分分隔符你可能想要“,”而不仅仅是“”然后再次在theNewData组合上,也许你想要“,”。当然,这需要扩展到其他替代品。这是一个好的开始吗?