如果可以,请大家帮忙。我有一个名为del.txt的文本文件,其中包含
STRING1
STRING5
此外,我还有一张excel表,里面有几乎1 000 000
行
我正在寻找一种有效的方法,如果A列中的文本字符串与del.txt
文件中的一个文本字符串完全匹配,如何删除整行?
列A中的excel文件中的文本字符串:
STRING1 entire row will be deleted
STRING1 entire row will be deleted
STRING3
STRING3
STRING5 entire row will be deleted
因此,在这种情况下,将删除包含STRING1和STRING5的行。感谢您提出任何建议。
到目前为止,我有以下代码,但我不知道如何从外部文本文件中获取值...
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
答案 0 :(得分:1)
这应该适合你:
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
问我是否有任何疑问。