VBA代碼的下面是查找文本和刪除行。但它是基於整張表格進行搜索。如何搜索包含「文本」的特定列並刪除不包含搜索的所有行
我如何使它只搜索「特定的列」與列出的文字陣列和刪除包含文本行。
根據下面的代碼,它是搜索我不想要的整個工作表。
Sub DeleteSystemMessage()
Dim varList As Variant
Dim varQP As Variant
Dim lngarrCounter As Long
Dim rngFound As Range, rngToDelete As Range
Dim strFirstAddress As String
Application.ScreenUpdating = False
'delete system message
varList = VBA.Array("XXXXXX", vbTextCompare)
For lngarrCounter = LBound(varList) To UBound(varList)
With Sheet1.UsedRange
Set rngFound = .Find(_
What:=varList(lngarrCounter), _
Lookat:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
If rngToDelete Is Nothing Then
Set rngToDelete = rngFound
Else
If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
Set rngToDelete = Application.Union(rngToDelete, rngFound)
End If
End If
Set rngFound = .FindNext(After:=rngFound)
Do Until rngFound.Address = strFirstAddress
If Application.Intersect(rngToDelete, rngFound.EntireRow) Is Nothing Then
Set rngToDelete = Application.Union(rngToDelete, rngFound)
End If
Set rngFound = .FindNext(After:=rngFound)
Loop
End If
End With
Next lngarrCounter
If Not rngToDelete Is Nothing Then rngToDelete.EntireRow.Delete
Application.ScreenUpdating = True
End Sub
_如何讓它只搜索文本數組的特定列並刪除不包含文本的列。請澄清以下內容:您需要搜索一列(如列D) ,並刪除工作表中所有其他列,或者只是輸入錯誤,實際上您需要刪除**行**? –
我編輯了我的問題。正在用數組文本搜索列。 如果列包含列中的文本,它將刪除整行 – user1935007
將'With Sheet1.UsedRange'更改爲'With Sheet1.Usedrange.Columns(1)' - 將搜索分離到所用範圍中的第一列。 – MattCrum