简体   繁体   English

从单元格中删除不在列表中的单词

[英]Remove words from a cell that aren't in a list

I want to remove some words that aren't in a separate list from an excel list.我想从 excel 列表中删除一些不在单独列表中的单词。 Someone gave me an example with Find/Replace, but i need the exact opposite, meaning that i want to keep the words in the list and remove the other.有人给了我一个查找/替换的例子,但我需要完全相反,这意味着我想保留列表中的单词并删除其他单词。 Also if a word is removed, I would have more than 1 space so i would need to remove multiple spaces.此外,如果删除了一个单词,我将有超过 1 个空格,所以我需要删除多个空格。

Can anyone give me an example?谁能给我一个例子?

Thanks, Sebastian谢谢,塞巴斯蒂安

EDIT编辑

Initial cell contents: word1 word2 word3 word4初始单元格内容: word1 word2 word3 word4

Cell contents after script: word2 word4脚本后的单元格内容: word2 word4

My list contains: word2, word4, word7, ...我的列表包含: word2, word4, word7, ...

This works:这有效:

Sub words()
    Dim whitelist() As Variant
    Dim listToScreen As Variant
    Dim screenedList As String
    Dim itsInTheWhitelist As Boolean
    Dim i As Long
    Dim j As Long

    ' Words to keep
    whitelist = Array("word2", "word4", "word7")

    ' Input old cell contents, split into array using space delimiter
    listToScreen = Split(Range("A1").Value, " ")

    screenedList = ""
    For i = LBound(listToScreen) To UBound(listToScreen)

        ' Is the current word in the whitelist?
        itsInTheWhitelist = False
        For j = LBound(whitelist) To UBound(whitelist)
            If listToScreen(i) = whitelist(j) Then
                itsInTheWhitelist = True
                Exit For
            End If
        Next j

        If itsInTheWhitelist = True Then
            ' Add it to the screened list, with space delimiter if required
            If Not screenedList = "" Then
                screenedList = screenedList & " "
            End If
            screenedList = screenedList & listToScreen(i)
        End If
    Next i

    'Output new cell contents
    Range("A2").Value = screenedList

End Sub

Using a Scripting.Dictionary and a RegExp will cost two references, but will avoid a N*N loop:使用 Scripting.Dictionary 和 RegExp 将花费两个引用,但会避免 N*N 循环:

' needs ref to Microsoft Scripting Runtime,
' Microsoft VBScript Regular Expressions 5.5

Option Explicit

Sub frsAttempt()
  Dim sInp As String: sInp = "word1 word2 word3 word4"
  Dim aInp As Variant: aInp = Split(sInp)
  Dim sExp As String: sExp = "word2 word4"
  Dim sLst As String: sLst = "word2, word4, word7"
  Dim aLst As Variant: aLst = Split(sLst, ", ")
  Dim dicGoodWords As New Dictionary
  Dim nIdx
  For nIdx = 0 To UBound(aLst)
    dicGoodWords(aLst(nIdx)) = 0
  Next
  For nIdx = 0 To UBound(aInp)
      If Not dicGoodWords.Exists(aInp(nIdx)) Then
         aInp(nIdx) = ""
      End If
  Next
  Dim sRes As String: sRes = Join(aInp)
  Dim reCleanWS As New RegExp
  reCleanWS.Global = True
  reCleanWS.Pattern = "\s+"
  sRes = Trim(reCleanWS.Replace(sRes, " "))
  Debug.Print sExp
  Debug.Print sRes
  Debug.Print sRes = sExp
End Sub

Output: Output:

word2 word4
word2 word4
True

The dictionary could be filled from a WorkSheet's column.字典可以从 WorkSheet 的列中填充。

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

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