我正在嘗試在列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
你需要解釋你的代碼不能工作。它是否錯誤 - 如果是的話 - 或只是沒有做你想做的事情? – SJR
嗨,歡迎來到Stack Overflow,請花點時間通過[歡迎導覽](https://stackoverflow.com/tour)瞭解你在這裏的方式(並獲得你的第一個徽章),閱讀如何創建一個[Minimal,Complete和Verifiable示例](https://stackoverflow.com/help/mcve)並檢查[如何提出好問題](https://stackoverflow.com/help/how-to-ask ),所以你增加了獲得反饋和有用答案的機會。 – DarkCygnus
很難理解你在這裏做什麼,因爲你正在編寫'eqcode'和'acct1':在下面的If塊中你不想做什麼,因爲只有第一次檢查通過......這將有助於展示「之前」和「之後」數據的例子。 –