简体   繁体   English

Excel VBA排除词

[英]Excel VBA Exclusion Words

I have some exclusion words namely: Limited, Corporation, Incorporation, Inc, Ltd and Co in cells A2 to A6 respectively. 我有一些排除词,分别是:Limited,Corporation,Incorporation,Inc,Ltd和Co,分别位于单元格A2至A6中。

In column B, it will be the input value of namely: ABC Limited, XYZ Holdings Corporation Limited and Tesco Bee Co Ltd in cells B2 to B4 respectively. 在B列中,分别是B2至B4单元格中的ABC Limited,XYZ Holdings Corporation Limited和Tesco Bee Co Ltd的输入值。

In column C, it will display the results of B with the exclusion of any or all words from column A. It should show results of ABC, XYZ Holdings and Tesco Bee respectively. 在C列中,它将显示B的结果,但不包括A列中的任何或所有单词。它应分别显示ABC,XYZ Holdings和Tesco Bee的结果。

Will there be any formula or macros that will be able to solve this issue? 是否会有任何公式或宏可以解决此问题?

I have attached a link to sample of what I am trying to illustrate. 我已附上我要说明的示例的链接。

屏幕截图在这里

I have tried this code: 我已经试过这段代码:

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

However, I do not know how to incorporate all exclusion words in it, currently I managed to exclude "Limited" only. 但是,我不知道如何在其中加入所有排除词,目前我仅设法排除了“受限”。

Taking your code and putting it into a loop: 将您的代码放入循环中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM