VBA删除行删除工作表中的所有内容

下面的代码删除工作表中的所有内容,即使该值位于命名范围内。 它需要检查的列是sheet1上的第一列,范围是A1:A45在sheet2上:

Sub DeleteRows() 'Deletes rows where one cell does not meet criteria Dim ws1 As Worksheet: Set ws1 = ActiveWorkbook.Sheets("Sheet1") Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("Sheet2") Dim criteria As String Dim found As Range Dim i As Long Application.ScreenUpdating = False For i =212 to 2 step -1 criteria = ws1.Cells(i, 1).Value With ws2.Range("A1:A45") Set found = .Find(What:=criteria, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) End With If found Is Nothing Then ws1.Cells(i, 1).EntireRow.Delete End If Next i Application.ScreenUpdating = True End Sub 

ws1.Cells(i, 1).EntireRow.Delete可以是ws1.Rows(i).Delete 。 我会用Visual Basic中的F8来查看它实际上在做什么。

只有当该行包含来自工作表1的所有200行以上的文本时,您的macros才能在该范围内生存。

你应该有你的外部循环通过表2的行,并删除它只包含任何值从表1。