[英]Excel Macro - Delete Row based upon variable date range
我需要删除给定日期范围之外的所有行。 我能够记录一个宏,但是该宏仅适用于给定的数据集,并且在添加更多数据时将无法删除所有适当的行。 我修改主题问题中发现的其他宏并没有成功,...抱歉。
日期交易日期位于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``
先感谢您!
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
我认为这会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.