2017-07-13 55 views
-2

我正在嘗試在列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」, 「非洲虛擬大學」, 「06212017」, 「06212017」, 「」, 「1」, 「」, 「」,」 CAU的」, 「$現金」, 「」, 「」, 「」, 「」, 「598.5」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」 , 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「7667079」, 「」, 「48」, 「」 「」, 「N」, 「Y」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」

後:
child1, 「DV」, 「」, 「個CSU」, 「AVU」, 「06212017」, 「06212017」, 「」, 「1」, 「」, 「」,「諸CAU 」, 「$現金」, 「」, 「」, 「」, 「」, 「598.5」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「7667079」, 「」, 「48」, 「」, 「」, 「N」, 「Y」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」, 「」,」 「

這裏是我的代碼:


`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 
+0

你需要解釋你的代碼不能工作。它是否錯誤 - 如果是的話 - 或只是沒有做你想做的事情? – SJR

+1

嗨,歡迎來到Stack Overflow,請花點時間通過[歡迎導覽](https://stackoverflow.com/tour)瞭解你在這裏的方式(並獲得你的第一個徽章),閱讀如何創建一個[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)並檢查[如何提出好問題](https://stackoverflow.com/help/how-to-ask ),所以你增加了獲得反饋和有用答案的機會。 – DarkCygnus

+2

很難理解你在這裏做什麼,因爲你正在編寫'eqcode'和'acct1':在下面的If塊中你不想做什麼,因爲只有第一次檢查通過......這將有助於展示「之前」和「之後」數據的例子。 –

回答

0

像這樣的東西應該工作。如果將查找/替換對放在工作表上,並在VLOOKUP中使用它,則可以更輕鬆地進行管理。

Sub replacement_TEST() 

    Dim rows As Long, i As Long, n As Long 
    Dim acct1 As String 
    Dim eqcode As String 
    Dim sht As Worksheet, arrFind, arrRepl, m 

    Set sht = ActiveSheet 
    rows = sht.UsedRange.rows.Count 

    'arrays of matching find/replace values 
    arrFind = Array("child1in", "wstre2in", "wstrebin", "lrcfbag", "wstpsbst") 
    arrRepl = Array("child1", "wstre2lv", "wstrebal", "lrcfbal", "wstpsbal") 

    For i = 1 To rows 
     acct1 = sht.Cells(i, 1).Value 
     eqcode = sht.Cells(i, 4).Value 

     If eqcode = "csus" Or eqcode = "mfus" Then 
      m = Application.Match(acct1, arrFind, 0) '<< any match ? 
      If Not IsError(m) Then 
       sht.Cells(i, 1).Value = arrRepl(m - 1) 'm is 1-based... 
      End If 
     End If 
    Next 
End Sub 
+0

感謝! - 還有一個問題,如果所有的數據(包括eqcode)都在列A中(如.csv)而不是在第四列中 - 您如何修改上述內容以實現相同目標? - 我試圖簡單地將4改爲1但不起作用... – srgomez

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 
+0

謝謝@科斯塔斯的幫助,雖然它沒有工作..我已經更新我的代碼版本(也仍然不工作)任何建議? – srgomez

+0

這可能是由於引號'''在值的周圍?嘗試'Debug.Print values(3)'來查看引號是否包含在字符串中。 –

+0

我有一個編譯錯誤:sub或function not defined @kostas k – srgomez