Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 从单元格中的列表中删除多个单词_Vba_Excel - Fatal编程技术网

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

Vba 从单元格中的列表中删除多个单词,vba,excel,Vba,Excel,我有一个实体名称列表,我想从中提取关键字。所以我想从所有的名字中删除一个类似“company”、“ltd”、“university”、“of”、“and”等词的列表。“删除词”的列表大约有20项,所以使用替代词是行不通的。有没有办法做到这一点?公式和VBA都可以。如果可能,该方法应使用户能够在excel中添加、减少或编辑“删除单词”列表。我想要的是这样的: Sheet "Names" Input Cell A1-A4 = "Apple Co. Ltd.", "Orange University"

我有一个实体名称列表,我想从中提取关键字。所以我想从所有的名字中删除一个类似“company”、“ltd”、“university”、“of”、“and”等词的列表。“删除词”的列表大约有20项,所以使用替代词是行不通的。有没有办法做到这一点?公式和VBA都可以。如果可能,该方法应使用户能够在excel中添加、减少或编辑“删除单词”列表。我想要的是这样的:

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"

您需要循环浏览“删除单词”列表和
。用空白表达式替换每个单词:

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

您需要循环浏览“删除单词”列表和
。用空白表达式替换每个单词:

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

按照@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

按照@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也给出了这个代码。只需使用函数ClearWords(A1,Range)即可在任何单元格中获得结果。我认为这也是一个很好的解决方案,因为它将“删除单词”作为一个范围变量

 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

Peter_SSs也给出了这个代码。只需使用函数ClearWords(A1,Range)即可在任何单元格中获得结果。我认为这也是一个很好的解决方案,因为它将“删除单词”作为一个范围变量

 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

我把这个标记为答案。非常感谢你。是否可以将结果放在不同的列中,以便原始结果仍在那里进行比较?更改
Set rngChange=.Range(“A1:a”和.Range(“a”和.Rows.Count”).End(xlUp.Row)
to
Set rngChange=.Range(“B1:B”和.Range(“a”和.Rows.Count”).End(xlUp.Row)
对于列BI,将此标记为答案。非常感谢你。是否可以将结果放在不同的列中,以便原始结果仍在那里进行比较?更改
设置rngChange=.Range(“A1:a”和.Range(“a”和.Rows.Count”).End(xlUp.Row)
设置rngChange=.Range(“B1:B”和.Range(“a”和.Rows.Count”).End(xlUp.Row)
对于列,BAntiDrondert的代码有一个清理部分,AntiDrondert的代码有一个清理部分,这使它非常好。