简体   繁体   English

从单元格中的列表中删除多个单词

[英]Remove multiple words from a list in a cell

I have a list of entities names that I want to extract keywords out of those.我有一个实体名称列表,我想从中提取关键字。 So I want to remove a list of words like "company", "ltd", "university", "of", "and", etc. from all the names.所以我想从所有名称中删除诸如“company”、“ltd”、“university”、“of”、“and”等词的列表。 The list of "removal words" is about 20 items so using SUBSTITUTE won't work. “删除词”列表大约有 20 个项目,因此使用 SUBSTITUTE 将不起作用。 Is there a way to achieve that?有没有办法实现这一目标? Formula and VBA are ok.公式和 VBA 没问题。 If possible, the method should give the user the ability to add, reduce or edit the list of "removal words" in excel.如果可能,该方法应使用户能够在 excel 中添加、减少或编辑“删除词”列表。 What I want look something like this:我想要的是这样的:

Sheet "Names" Input
Cell A1-A4 = "Apple Co. Ltd.", "Orange University", "Excel company", "Mountain trading and renting company Ltd."
Sheet "Removal"
Cell A1-A4 = "company", "co.", "Co.", "Ltd."
Sheet "Names" Result
Cell B1-B4 = "Apple", "Orange University", "Excel", "Mountain trading and renting"

You need to loop through the list of "removal words" and .Replace each of the words with blank expression:你通过“去除词”的名单需要循环.Replace每个与无表情的话:

Sub RemoveWords()
    Dim vArr(), i As Long
    Dim rngChange As Range
    'Store Removal values in array
    With ThisWorkbook.Worksheets("Removal")
        vArr = Application.Transpose(.Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value)
    End With
    With ThisWorkbook.Worksheets("Names")
        'Define range where replacements will be made
        Set rngChange = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row)

        'To use another column, uncomment the following lines:
        'Set rngChange = .Range("B1:B" & .Range("A" & .Rows.Count).End(xlUp).Row)
        'rngChange.Value = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Value

        'Loop through array of words to be removed
        For i = LBound(vArr) To UBound(vArr)
            'Replace "removal word" with blank expression
            rngChange.Replace vArr(i), "", xlPart
        Next i
        'Trim cells in range
        rngChange.Value = Evaluate("IF(ROW(),TRIM(" & rngChange.Address & "))")
    End With
End Sub

along the lines of @AntiDrondert 's code, but with some variations and putting results in sheet "Names" column B:沿着@AntiDrondert 的代码行,但有一些变化并将结果放在表“名称”列 B 中:

Sub RemoveWords()
    Dim wordsToRemove As Variant, word As Variant

    With Worksheets("Removal") 'reference "Removal" worksheet
        wordsToRemove = Application.Transpose(.Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Value) ' store
    End With
    With Worksheets("Names") 'reference "names" worksheet
        With .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)) ' reference referenced worksheet column A cells from row 1 down to last not empty row
            .Offset(, 1).Value = .Value ' copy values one column to the left
            For Each word In wordsToRemove 'loop through words to be removed array
                .Offset(, 1).Replace word, "", xlPart 'replace current word in referenced range one column to the left
            Next
        End With
    End With
End Sub

Peter_SSs also gave this code. Peter_SSs 也给出了这个代码。 Just use function ClearWords(A1,Range) to get the result in any cell.只需使用函数 ClearWords(A1,Range) 即可在任何单元格中获得结果。 I think this is also very good solution as it includes the "removal words" as a range variable.我认为这也是一个很好的解决方案,因为它包含“删除词”作为范围变量。

 Function ClearWords(s As String, rWords As Range) As String
      'By Peter_SSs, MrExcel MVP
      Static RX As Object
      
      If RX Is Nothing Then
        Set RX = CreateObject("VBScript.RegExp")
        RX.Global = True
        RX.IgnoreCase = True
      End If
      RX.Pattern = "\b" & Replace(Join(Application.Transpose(rWords), "|"), ".", "\.") & "\b"
      ClearWords = Application.Trim(RX.Replace(s, ""))
    End Function

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

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