为每个工作表删除超过一个月的数据

嘿家伙,所以基本上我有一个excel与大量的工作表,我希望通过按下button,我可以在月底过滤旧数据,并删除它。 (其原因并不总是我可以清楚的第一个数据,有时用户会来工作的工作簿,才可以设置为当月)

所以这是已经build立的代码:

Private Sub CommandButton4_Click() Dim ws As Worksheet Dim Rng As Range Dim LastRow As Long, LastCol As Long response = MsgBox("Tem a Certeza que quer Limpar todo os Dados?", vbYesNo) If response = vbNo Then Exit Sub For Each ws In ThisWorkbook.Worksheets If ws.Name <> "Formularios" Then If ws.Name <> "Coordenador" Then If ws.Name <> "LookupList" Then With ws .Unprotect Password:=pass LastRow = .Range("A" & .Rows.Count).End(xlUp).Row LastCol = .Range("A" & .Columns.Count).End(xlToLeft).Column Set Rng = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) End With With Rng .AutoFilter Field:=1, Criteria1:=xlFilterLastMonth .SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count).Rows.Delete End With With ws .AutoFilterMode = False If .FilterMode = True Then .ShowAllData End If .Protect Password:=pass End With End If End If End If Next End Sub 

当我运行代码时,我得到错误1004对象的行

  .SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count).Rows.Delete 

任何人看到我的代码上的任何错误?

谢谢您的帮助

这是我通常使用的forms。

  With ws .Unprotect Password:=pass LastRow = .Range("A" & .Rows.Count).End(xlUp).Row LastCol = .cells(1, .Columns.Count).End(xlToLeft).Column Set Rng = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) End With With Rng .AutoFilter Field:=1, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic with .resize(.rows.count-1, .columns.count).offset(1, 0) if cbool(application.subtotal(103, .cells)) then .SpecialCells(xlCellTypeVisible).EntireRow.Delete end if end with End With 

我改变了初始范围(rng)只是第二行,并取消了偏移量和resize。 由于范围只用于这个子程序,为什么它被resize? 我在本月testing了5个或6个date,最后甚至将它们排除在外,并按预期工作。

 sub stest() With Worksheets(1) LastRow = .Range("A" & .Rows.Count).End(xlUp).Row Set Rng = .Range(.Cells(2, 1), .Cells(LastRow, 1)) End With With Rng .AutoFilter Field:=1, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic .offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With End Sub