2016-09-06 521 views
1

我在Excel中有兩個文本單元格,如下所示,其中包含字符串(A1和A2),我試圖從A1單元格字符串中刪除A2單元格字符並將結果存儲在A3中。如何從Excel中減去另一個文本單元格?

enter image description here

我一直在使用像搜索一些功能(),TEXT()試過了,RIGHT()..但不可能達到我需要的結果。

我可以很容易地使用任何編程語言(Python,C等)來做到這一點 - 只需要一個循環和幾個字符串函數。但無法弄清楚如何使用Excel Formula獲得相同的結果。

任何想法我們怎麼能得到這個?

+0

這是區分大小寫? (你拿出'我',但離開'我')。您可能需要VBA,是否需要*使用公式?你可以做一個超級錯綜複雜的替代品,你可以用一個替代品包裹替代品...... – BruceWayne

+0

這可能不是真的,但從我所知道的你不能通過一個簡單的內置函數來完成。您需要使用vba宏 – Luke

+0

可能會編寫一個Excel VBA函數。 – RBarryYoung

回答

5

這樣做通過UDF將是非常直的前鋒。基本上只需循環遍歷ins字符,並使用replace()將字符替換爲空;

Function textSubtract(startString As String, subtractString As String) As String 
    'Function to subtract characters in one string from another string 

    'Loop through every character in subtractString 
    Dim charCounter As Integer 
    For charCounter = 1 To Len(subtractString) 

     'Replace out the character in startString 
     startString = Replace(startString, Mid(subtractString, charCounter, 1), "") 

    Next charCounter 

    'Return 
    textSubtract = startString 

End Function 

然後,你可以這樣調用它:

enter image description here

+0

爲什麼不添加一個可選的布爾參數來打開和關閉大小寫敏感?(默認爲按照OP的示例) – Jeeped

+1

這是VBA ...天空是極限,我只是寫給規範。 – JNevill

4

B2輸入:

=MID($A$2,COLUMN()-1,1) 

和跨複製(這隔離字符)。在B3輸入:

=SUBSTITUTE($A$1,B2,"") 

C3輸入:

=SUBSTITUTE(B3,C2,"") 

和整個複製(這消除每個字符)

enter image description here

+0

我認爲沒有辦法,通過公式,在一個單元格中做所有的事情?既然你不知何故必須遍歷'A2',是嗎? – BruceWayne

+0

除非你想將它嵌入到10個地方,如果你知道a2中字符的限制是10 – happymacarts

+0

@BruceWayne這可能可以用一個*** Array Formula ***來完成,但這超出了我的能力。 –

相關問題