macros – 根据date删除行

我对Excel中的VBA和macros很新。 我有一个非常大的Excel电子表格,其中A列包含date。 我试图删除有一个小于某个date的值的行,这是我到现在为止..

Sub DELETEDATE() Dim x As Long For x = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row Debug.Print Cells(x, "A").Value If CDate(Cells(x, "A")) < CDate("01/01/2013") Then Cells(i, "A").EntireRow.Delete End If Next x Next i End Sub 

我正在收到一个没有错误的下一个…有人可以帮忙吗?

这非常适合使用Range.AutoFilter属性。 以下脚本包含对每个步骤所做的评论:

 Option Explicit Sub DeleteDateWithAutoFilter() Dim MySheet As Worksheet, MyRange As Range Dim LastRow As Long, LastCol As Long 'turn off alerts Application.DisplayAlerts = False 'set references up-front Set MySheet = ThisWorkbook.Worksheets("Sheet1") 'identify the last row in column A and the last col in row 1 'then assign a range to contain the full data "block" With MySheet LastRow = .Range("A" & .Rows.Count).End(xlUp).Row LastCol = .Range("A" & .Columns.Count).End(xlToLeft).Column Set MyRange = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) End With 'apply autofilter to the range showing only dates 'older than january 1st, 2013, then deleting 'all the visible rows except the header With MyRange .AutoFilter Field:=1, Criteria1:="<1/1/2013" .SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(.Rows.Count).Rows.Delete End With 'turn off autofilter safely With MySheet .AutoFilterMode = False If .FilterMode = True Then .ShowAllData End If End With 'turn alerts back on Application.DisplayAlerts = True End Sub 

在一个简单的例子(在这个图片的“Sheet1”上)运行这个代码,看起来像这样:

开始

将删除所有date早于1/1/2013的行,并为您提供此结果:

结束

回答你的问题

我正在收到一个下一个没有错误

问题是你试图循环i但你还没有打开一个For i循环。 当你将代码缩进任何调用Loop或条件的代码(即If )时,它就变得明显了

 Sub DELETEDATE() Dim x As Long For x = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row Debug.Print Cells(x, "A").Value If CDate(Cells(x, "A")) < CDate("01/01/2013") Then Cells(i, "A").EntireRow.Delete 'i has no value so Cells(0, "A") is ?? End If Next x Next i 'where is the For i = ... in this code? End Sub 

在编写代码时,我尝试:

  • 如果需要,立即input结束命令。 因此,inputIf...Then [ENTER]键入End If ,按[HOME] ,按[ENTER] ,然后按[TAB] [UP ARROW]然后[TAB]在正确的位置写入条件码,以便任何人能够轻松阅读和理解。
  • 始终在每个模块的顶部使用Option Explicit来强制variables声明。

关于根据条件删除行的提示如果您从顶部开始并逐渐减less,每次删除一行时,计数器将有效地移动到您删除的行下面两行的单元格,因为删除的行的正下方的行向上移动(即根本没有testing)。

最有效的方法是从底部或行进行循环:

 Sub DELETEDATE() Dim x As Long For x = [a1].SpecialCells(xlCellTypeLastCell).Row To 1 Step -1 Debug.Print Cells(x, "A").Value If CDate(Cells(x, "A")) < CDate("01/01/2013") Then Cells(x, "A").EntireRow.Delete 'changed i to x End If Next x End Sub 

这样,你想要testing的下一行就被保留下来了 – 你只把下面的行向上移动1,而且你已经在之前testing过了。

请试试这个

 Sub DELETEDATE() Dim x As Long last = Range("A65536").End(xlUp).Row For x = 1 To last Debug.Print Cells(x, "A").Value check: If x <= last Then If Trim(CDate(Cells(x, "A"))) <= Trim(CDate("7/29/2013")) Then last = last - 1 Cells(x, "A").EntireRow.Delete GoTo check End If End If Next x End Sub 

由于debugging器的突出显示,您在代码中有一些额外的Next i 。 尝试下面的内容:

 Sub DELETEDATE() Dim x As Long For x = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row Debug.Print Cells(x, "A").Value If CDate(Cells(x, "A")) < CDate("01/01/2013") Then Cells(i, "A").EntireRow.Delete End If Next x End Sub