下面的代码删除工作表中的所有内容,即使该值在命名范围内也是如此。它需要检查的列是sheet1上的列I,范围是A1:A45 on 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
答案 0 :(得分:0)
ws1.Cells(i, 1).EntireRow.Delete
可能只是ws1.Rows(i).Delete
。我将在Visual Basic中使用F8来查看它实际上在做什么。
答案 1 :(得分:-1)
如果该行包含来自所有200多行第1页的行中的文本,那么您的宏只能在该范围内生活。
你应该让你的外for
在表格2的行中循环,并且只删除它不包含表格1中的任何值。