我正在尝试删除一个工作表中与多个其他工作表中的行匹配条件的行。我需要遍历工作簿中的所有其他工作表,每次找到匹配的东西时,我都会删除第一个工作表中的整行。我正在联盟上获得错误1004。我认为这可能是因为您不能在工作表之间使用Union,所以我在每张工作表后将其设置为Nothing。我仍然遇到同样的错误。
以下是代码:
Sub findRemaining()
Dim rngToDel As Range
Dim fRng As Range 'Fund range
Dim wCell As Range 'Working sheet cell
Dim wRng As Range 'Working sheet range
Dim WS_Count As Integer
Dim I As Integer
Set fRng = Worksheets("All").Range("B2:B1495")
WS_Count = ActiveWorkbook.Worksheets.Count
For I = 2 To WS_Count
Set rngToDel = Nothing
Set wRng = Worksheets(I).Range("B2:B200")
For Each wCell In wRng 'Loop through all working cells
' if wCell found in Fund range then delete row
If Not IsError(Application.Match(Trim(wCell.Value), fRng, 0)) Then
If rngToDel Is Nothing Then
Set rngToDel = wCell
Else
Set rngToDel = Union(rngToDel, wCell)
End If
End If
Next wCell
If Not rngToDel Is Nothing Then rngToDel.EntireRow.Delete
Next I
End Sub
答案 0 :(得分:0)
问题是我在wCell
中将范围设置为wRng
,但实际上我要删除的是fRng
中的单元格。通过修复其余代码完美运行。 (谢谢克里斯)