2017-11-01 3 views
0

나는 몇 가지 제외 단어가 있습니다 : 제한, 법인, 법인, Inc, 주식 회사 및 공동 각각 A2에 A6 셀.엑셀 VBA 제외 단어

B 열은 ABC Limited, XYZ Holdings Corporation Limited 및 Tesco Bee Co Ltd의 셀 B2에서 B4에 각각 입력 값이됩니다.

C 열에는 A 열의 단어 또는 일부를 제외하고 B 열의 결과가 표시됩니다. ABC, XYZ Holdings 및 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' 기능을 활용 엑셀 수식을 사용할 수 있습니다, 또는 당신은 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

대단히 감사합니다! 그것은 매력처럼 작동합니다! –