vba什么=“名称”replace=“somethingNew”不按预期工作

最近有一个问题已经解决,并出现了完美的工作, VBA全部replace“/”

我一直在testing各种数据,遇到了一个我无法弄清楚的问题。

如果我有一些testing数据如

/ test user / john Streett / 

并运行下面的代码

 Worksheets(1).Columns(Poc).Replace _ What:="/ *street* /", Replacement:="/rstreett/", _ SearchOrder:=xlByColumns, MatchCase:=False 

我的数据出来

 /rstreett/ 

这不是我期待的事情,也不是我想要发生的事情。 我预计:

 / test user /rstreett/ 

其他数据如

  / Test User / maul / Random User / Third User / 

随着确切的代码(名称更改)

 Worksheets(1).Columns(Poc).Replace _ What:="/*maul*/", Replacement:="/dmaul/", _ SearchOrder:=xlByColumns, MatchCase:=False 

数据出来…

  /dmaul/ Random User / Third User / 

它删除“第一个用户”! 不是我一直打算的。 我期望的是:

 / Test User /dmaul/ Random User / Third User / 

我一直试图弄清楚这一切,完全卡住了。 如果有人能以我的方式解释错误,将不胜感激。

替代方法

我有一个解决方法,通过上面的问题来parsing数据。 我使用split为了将名称拆分到不同的列,然后在这些单元格中单独修改数据,以允许我使用更新的名称。 代码在下面(希望没有错别字,但你可以得到它的要点。)

 #Set active sheets For i = 1 To 8 ActiveWorkbook.Sheets(i).Activate #This portion creates the "/" as a seperator For Q = 2 To 1000 If Cells(Q, "B").Value <> "" Then Cells(Q, "B").Value = "/ " & Cells(Q, "B").Value & " /" end if #This portion changes "," to "/" which were used sometimes in my case to split names Worksheets(i).Columns("B").Replace _ What:=",", Replacement:="/", _ SearchOrder:=xlByColumns, MatchCase:=False #Split the names (first into column 1, second into column 2) #I only need 2 names from all the names that may be in field which is why I only push to 2 columns (24 and 25) dim Poc = 24 dim addPoc = 25 For Q = 2 To 1000 Dim tokens() As String Name = Cells(Q, "B").Value tokens = Split(Name, "/") If UBound(tokens) > 0 Then Cells(Q, Poc).Value = tokens(1) If UBound(tokens) > 1 Then Cells(Q, addPoc).Value = tokens(2) End If Next Q #Now I proceed through the 2 new columns and modify the names Dim colPoc For Each colPoc In Array(Poc, addPoc) Worksheets(i).Columns(colPoc).Replace _ What:="*street*", Replacement:="rstreet", _ SearchOrder:=xlByColumns, MatchCase:=False Worksheets(i).Columns(colPoc).Replace _ What:=" *maul* ", Replacement:="dmaul", _ SearchOrder:=xlByColumns, MatchCase:=False Worksheets(i).Columns(colPoc).Replace _ What:="*test*", Replacement:="tuser", _ SearchOrder:=xlByColumns, MatchCase:=False #these statements continue to modify all possible names that may be encountered Next I #Now I proceed through the 2 new columns and modify the names 

我想我明白了。 在你的For Each colPoc In Array(Poc, addPoc) ,从你当前的子For Each colPoc In Array(Poc, addPoc)调用它,移除你的Replaces并放置这些行(我认为colPoc是一个范围,所以我们需要它的列):

 replaceText "maul", "dmaul", colPoc.column replaceText "Streett", "rstreett", colPoc.column replaceText "test", "tuser", colPoc.column 

然后,将其添加到当前代码所在的模块/表单中:

 Sub replaceText(findString As String, replaceString As String, colPoc As Long) Dim replaceStr$, searchStr$, str$, editedStr$ Dim lastRow& Dim cel As Range, rng As Range lastRow = Cells(Rows.Count, colPoc).End(xlUp).Row Set rng = Range(Cells(1, colPoc), Cells(lastRow, colPoc)) For Each cel In rng cel.Select str = cel.Value editedStr = StrReverse(Replace(StrReverse(str), StrReverse(findString), StrReverse(replaceString), , 1)) Dim strArray As Variant, finalArray As Variant Dim i&, startPos& If InStr(1, editedStr, replaceString, vbBinaryCompare) Then strArray = Split(editedStr, "/") For i = LBound(strArray) To UBound(strArray) startPos = InStr(1, strArray(i), replaceString, vbBinaryCompare) If startPos Then strArray(i) = Mid(strArray(i), startPos, startPos + Len(replaceString)) strArray(i) = Trim(strArray(i)) Exit For End If Next i finalArray = Join(strArray, "/") cel.Offset(0, 1).Value = finalArray End If Next cel End Sub 

…我确定有人可以帮助收紧(或使用正则expression式),但我相信应该工作。 当我运行它时,这就是它为我做的

 replaceText "maul", "dmaul", 1 replaceText "Streett", "rstreett", 1 

在这里输入图像说明

我认为这是区分大小写的,但是我们以后可以担心

(感谢@leowyn的Split/Join思想!还有@EngineerToast的StrReverse思想 )