[英]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.