vba what =“name”replacement =“somethingNew”没有按预期工作

时间:2016-03-31 17:25:35

标签: excel vba excel-vba

最近有一个问题已经解决并且似乎完美无缺vba replace all in column within sections broken by "/"

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

如果我有一些测试数据,例如

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

我一直试图在整个上午解决这个问题而且完全陷入困境。如果有人能够以我的方式解释错误,那将非常感激。

替代方法

我有一个解决方法可以解决上面尝试解析数据的问题。我使用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

1 个答案:

答案 0 :(得分:1)

我想我明白了。在您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

...我确信有人可以帮助收紧(或使用RegEx),但我相信这应该有效。当我用

运行时,这就是它对我的作用
replaceText "maul", "dmaul", 1
replaceText "Streett", "rstreett", 1

enter image description here

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

(感谢@leowyn提出的Split/Join想法!还有@EngineerToast for the StrReverse idea