我需要删除给定日期范围之外的所有行。我能够记录一个宏,但这只适用于给定的数据集,并且在添加更多数据时将无法删除所有适当的行。我没有成功修改我在主题中发现的其他宏询问完全相同的事情....抱歉。
日期交易日期在C列中,最早在单元格J1中(通常是公式,如果重要,但我可以更改),日期范围的结尾在单元格L1中。
我尝试使用Dan Wagner之前发布的代码:
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``
提前谢谢!
答案 0 :(得分:0)
dim row as long
dim lastrow as long
dim mindate as date
dim maxdate as date
mindate = cdate(Cells(1,10))
maxdate = cdate(Cells(1,12))
lastrow = Cells(Rows.Count,"C").End(xlUp).Row
row = 1 'Set here the starting row for checking transaction dates
do while row <= lastrow
transdate = cdate(cells(row,3))
if transdate < mindate or transdate > maxdate then
rows(row).entirerow.Delete
else
row = row + 1
end if
loop
我认为这会起作用