如果列A包含来自外部txt文件的文本,如何删除Excel中的整个行

你好,请帮助,如果你可以。我有一个文本文件名为del.txt包含

STRING1 STRING5 

我也有一个有近1 000 000行的excel表。
我正在寻找一种有效的方法如何删除整个行,如果在列A中的文本string与del.txt文件中的文本string之一完全匹配?

在列中的Excel文件中的文本stringA:

 STRING1 entire row will be deleted STRING1 entire row will be deleted STRING3 STRING3 STRING5 entire row will be deleted 

所以在这种情况下,STRING1和STRING5的行将被删除。感谢您的任何build议。

到目前为止,我有下面的代码,但我不知道如何从外部文本文件中取值…

 Sub Del_Strings() Dim i As Long, strArr strArr = Array("STRING1", "STRING2") For i = LBound(strArr) To UBound(strArr) With [A1:A1000000] .Replace strArr(i), "", xlWhole End With Next i ActiveSheet.Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(4).EntireRow.Delete End Sub 

这应该为你工作:

 Option Explicit Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0)) End Function Sub Del_Strings() Dim MyData As String, strData() As String Dim i As Long, lastRow As Long Open "C:\test\test\del.txt" For Binary As #1 '--->change file path as required MyData = Space$(LOF(1)) Get #1, , MyData Close #1 strData() = Split(MyData, vbCrLf) lastRow = Cells(Rows.Count, "A").End(xlUp).Row For i = lastRow To 1 Step -1 If IsInArray(Cells(i, 1).Value, strData) Then Rows(i).EntireRow.Delete End If Next i End Sub 

问我是否有任何疑问。