2017-11-01 13 views
0

私はいくつかの除外ワードを持っています:すなわち、Limited、Corporation、Incorporation、Inc、Ltd、CoはそれぞれA2からA6のセルにあります。Excel VBA除外ワード

B欄には、それぞれABC Limited、XYZ Holdings Corporation Limited、Tesco Bee Co Ltdの入力値がB2~B4のセルに入力されます。

C列には、A列の単語の一部または全部を除いたBの結果が表示されます。ABC、XYZホールディングスおよびTesco Beeの結果がそれぞれ表示されます。

この問題を解決するための数式やマクロはありますか?

私が説明しようとしているもののサンプルへのリンクを添付しました。

Screenshot here

私はこのコードを試してみました:

Sub test() 
    Dim OriginalText As String 
    Dim CorrectedText As String 
    OriginalText = Range("C4").Value 
    CorrectedText = Replace(OriginalText, "Limited", "") 
    Range("C4").Offset(, 1).Value = CorrectedText 
End Sub 

しかし、私は現在、私は唯一の「リミテッド」を除外するために管理し、その中のすべての除外ワードを組み込む方法がわかりません。

+0

あなたは 'SUBSTITUTE'機能を利用したExcelの数式を使用することができ、またはあなたはVBA' Replace'機能を利用してVBAを使用することができます。 (あなたが望むならば、VBAではループで行うことができるが、それぞれを5回使用しなければならないだろう) – YowE3K

答えて

0

あなたのコードを撮影し、ループに入れる:

Sub test() 
    Dim Exclusions As Variant 
    Dim CorrectedText As String 
    Dim r As Long 
    Dim i As Long 
    'Get all the "exclusions" from column A 
    Exclusions = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value 
    'Loop through all the cells in column C 
    For r = 2 To Cells(Rows.Count, "C").End(xlUp).Row 
     'Start with the current value 
     CorrectedText = Cells(r, "C").Value 
     'Remove, one word at a time, any words that are excluded 
     For i = LBound(Exclusions, 1) To UBound(Exclusions, 1) 
      CorrectedText = Replace(CorrectedText, Exclusions(i, 1), "") 
     Next 
     'Put whatever is left in column D 
     Cells(r, "D").Value = CorrectedText 
    Next 
End Sub 
+0

ありがとう!それは魅力のように機能します! –