用条件替换子串

时间:2017-07-13 16:58:47

标签: vba excel-vba excel

我正在尝试使用A列中的字符串搜索Excel文件,并在有几个条件通过时替换字符串的一部分。

逻辑上:if NAME and CODE(A) or CODE(B) exist in string then replace NAME

示例:NAME = child1in CODE(A) = csus CODE(B) = mfus

注意:有几个名称需要替换,但代码是相同的 TIA!

在: 的 child1in 下," DV""""的个CSU "" AVU&# 34;," 06212017"" 06212017"""" 1"""&# 34;"" CAU的"" $现金""""""&# 34;,""" 598.5""""""",& #34;""""""""""&# 34;,""""""""""&# 34;""""""""""&#34 ;,""" 7667079"""" 48"""&#34 ;"" N"" Y""""""" """"""""""" """""""""

在:
child1 下," DV""""个CSU"" AVU"" 06212017& #34;" 06212017"""" 1""""",& #34; CAU的"" $现金""""""""& #34;" 598.5""""""""&#34 ;, """""""""""& #34;"""""""""",& #34;""""""""""&# 34;," 7667079"""" 48"""""&# 34; N"" Y""""""""&#34 ;,""""""""""&#34 ;"""""""

这是我的代码:

`Sub replacement_TEST()

Dim rows As Integer
Dim acct1 As String
Dim eqcode As String

rows = ActiveSheet.UsedRange.rows.Count

For i = 1 To rows

acct1 = ActiveSheet.Cells(i, 1).value
eqcode = ActiveSheet.Cells(i, 4).value

If acct1 = "child1in" And (eqcode = "csus" Or eqcode = "mfus") Then
                             acct1 = Replace(acct1, "child1in", "child1")
ElseIf acct1 = "wstre2in" And (eqcode = "csus" Or eqcode = "mfus") Then
                             acct1 = Replace(acct1, "wstre2in", "wstre2lv")
ElseIf acct1 = "wstrebin" And (eqcode = "csus" Or eqcode = "mfus") Then
                             acct1 = Replace(acct1, "wstrebin", "wstrebal")
ElseIf acct1 = "lrcfbag" And (eqcode = "csus" Or eqcode = "mfus") Then
                             acct1 = Replace(acct1, "lrcfbag", "lrcfbal")
ElseIf acct1 = "wstpsbst" And (eqcode = "csus" Or eqcode = "mfus") Then
                             acct1 = Replace(acct1, "wstpsbst", "wstpsbal")

                End If

                    ActiveSheet.Cells(i, 1).value = acct1

        Next

End Sub

2 个答案:

答案 0 :(得分:0)

不太确定这是你正在寻找的东西,但它肯定是一个起点。根据需要进行修改以满足您的需求。

Sub replacement_TEST()

    Dim r As Range, c As Range, values As Variant
    Set r = Range(Cells(1, 1), Cells(ActiveSheet.UsedRange.Rows.Count, 1))

    For Each c In r
        'Split comma separated values
        values = Split(c.Value, ",")

        'According to the screenshot provided, the index of "eqcode" is 3
        'and the "acct1" is 0
        If values(3) = "csus" Or values(3) = "mfus" Then
            Select Case values(0)
                Case "child1in":
                    c.Value = Replace(c.Value, values(0), "child1")

                Case "wstre2in":
                    c.Value = Replace(c.Value, values(0), "wstre2lv")

                Case "wstrebin":
                    c.Value = Replace(c.Value, values(0), "wstrebal")

                Case "lrcfbag":
                    c.Value = Replace(c.Value, values(0), "lrcfbal")

                Case "wstpsbst":
                    c.Value = Replace(c.Value, values(0), "wstpsbal")

                Case Else:
                    'do nothing
            End Select
        End If
    Next c
End Sub

答案 1 :(得分:0)

这样的事情应该有效。如果将查找/替换对放在工作表上并在VLOOKUP中使用它,则可以更容易地对此进行管理。

defrecord
相关问题