select行并删除不会删除行

我写了一些简单的代码,将一个工作表中的单元格与另一个单元格中的单元格相匹配,然后在单元格相同的情况下删除整行。

该代码正确select行,但由于某种原因拒绝实际删除我的工作表中的行。 编辑:一些行删除。 尽pipe它们与删除的值完全相同,但其他值不会。 如果任何人都可以帮助,将不胜感激。

Sub delFunds() Dim fCell As Range 'Fund cell Dim fRng As Range 'Fund range Dim wCell As Range 'Working sheet cell Dim wRng As Range 'Working sheet range Dim n As Long Set fRng = Worksheets("Funds").Range("C2:C117") Set wRng = Worksheets("Working sheet").Range("I3:I7483") For Each fCell In fRng.Cells 'Loop through all funds For Each wCell In wRng.Cells 'Loop through all working cells If StrComp(wCell.Value, fCell.Value, vbTextCompare) = 0 Then 'If equal then delete n = wCell.Row Rows(n & ":" & n).Select Selection.Delete Shift:=xlUp End If Next wCell Next fCell 'Go to next fund End Sub 

我会用这个代码没有嵌套循环:

 Sub delFunds() Dim rngToDel As Range Dim fRng As Range 'Fund range Dim wCell As Range 'Working sheet cell Dim wRng As Range 'Working sheet range Set fRng = Worksheets("Funds").Range("C2:C117") Set wRng = Worksheets("Working sheet").Range("I3:I7483") 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 End Sub 

我知道@ simoco的答案是有效的,已经被接受了,但是我喜欢一个很好的问题,所以我想用autofilterfilter一起解决一个解决scheme,立即杀死大量的工作表。 我想你的devise可能看起来像这样:

建立

从那里,你可以循环通过简明的基金名单,并过滤每个基金的工作表:

 Option Explicit Sub EliminateWorkingDuplicates() Dim WorkingSheet As Worksheet, FundSheet As Worksheet Dim FundRange As Range, WorkingRange As Range, _ Fund As Range Dim LastRow As Long, LastCol As Long, _ WorkingFundCol As Long 'assign sheets and ranges for easy reference Set WorkingSheet = ThisWorkbook.Worksheets("Working sheet") Set FundSheet = ThisWorkbook.Worksheets("Funds") Set FundRange = FundSheet.Range("C2:C117") WorkingFundCol = 9 'column I on working sheet 'determine the bounds of the data block on the working sheet LastRow = WorkingSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row LastCol = WorkingSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column Set WorkingRange = Range(WorkingSheet.Cells(2, 1), WorkingSheet.Cells(LastRow, LastCol)) 'start working through the funds and calling the autofilter function For Each Fund In FundRange Call FilterAndDeleteData(WorkingRange, WorkingFundCol, Fund.Value) Call ClearAllFilters(WorkingSheet) Next Fund End Sub '********** 'blow away rows Sub FilterAndDeleteData(DataBlock As Range, TargetColumn As Long, Criteria As String) 'make sure some joker didn't pass in an empty range If DataBlock Is Nothing Then Exit Sub 'execute the autofilter with the supplied column and criteria With DataBlock .AutoFilter Field:=TargetColumn, Criteria1:=Criteria .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With End Sub '********** 'safely clear filters Sub ClearAllFilters(TargetSheet As Worksheet) With TargetSheet .AutoFilterMode = False If .FilterMode = True Then .ShowAllData End If End With End Sub