如何保持选词forms的excel,并删除其余的

我需要处理Excel中的14K行数据。
我有一个单词列表需要保留,并需要删除其余的。 由于数据量大,很难一个一个地find和replace这个词。 我得到一些关于把单词列表放入另一个excel文件并加载它来检查数据文件的每一列(与excel相同)。 vba是我的正确方法,还是可以使用Excel来解决这个问题?

这应该让你开始!

Sub CutWords() 'Cuts specific strings out from a specified sheet. '---Variables--- Dim pos As Integer Dim val As String Dim word As String Dim source As Worksheet Dim target As Worksheet Dim list As Worksheet Dim startRow As Integer Dim columns As Integer Dim i As Long Dim j As Long Dim k As Long Dim l As Long '---Customize--- Set source = ThisWorkbook.Sheets(1) 'This sheet contains the data Set target = ThisWorkbook.Sheets(2) 'Trimmed data will end up here Set list = ThisWorkbook.Sheets(3) 'List of words to look for startRow = 2 'The first row to be trimmed in data columns = 2 'The number of columns to be trimmed in data '---Logic--- Application.ScreenUpdating = False 'Saves us a bit of time target.Cells.ClearContents 'Clearing the target sheet i = startRow 'i will act as the "row counter" for our source sheet l = 1 'l will act as the "row counter" for our target sheet Do While i <= source.Range("A" & source.Rows.Count).End(xlUp).Row 'Looping until 'we hit the last row with data in "A" column. j = 1 'j will act as the "column counter" for our source sheet Do While j <= columns 'Checking all columns k = 1 'k will act as the "row counter" for our word list sheet Do While k <= list.Range("A" & list.Rows.Count).End(xlUp).Row 'Looping 'until we hit the last row with data in "A" column - these are the words word = list.Range("A" & k).Value 'Get the word. val = source.Cells(i, j).Value 'Get the value to check. pos = InStr(val, word) 'Check for match, 0 = no match If pos > 0 Then 'Match found target.Cells(l, j).Value = val 'The values will be in the same 'position as they were in the source (minus the ignored rows). 'It should be quite simple to remove the empty space if needed. End If k = k + 1 'Next word Loop j = j + 1 'Next column Loop l = l + 1 'Next target row i = i + 1 'Next source row Loop Application.ScreenUpdating = True 'Make sure to restore the value End Sub 

将代码插入一个新的代码模块,它应该准备好,但我承认我没有做太多的testing。 当然不是最快或最花哨的方法 – 但一个简单的方法。

编辑:即使部分比赛将被视为比赛。 你在单词列表中的单词可以是“你好”,也可以是“Hello World!”这样的短语。 仍然被认为是一场比赛。 如果您只想要完全匹配,则需要直接比较string,而不是使用InStr 。

 If val = word Then 'Match found 

默认情况下,第一张表应该包含您的数据,从列“A”开始。 它可能有标题。 第二张纸将是您运行macros后切割清单的位置。 第三张将会有你想要剪切的单词列表。 这个词必须在“A”列中。

HTH