2017-03-17 13 views

回答

0
sub deldat() 
dim ws as worksheet 
dim lrow as variant 
dim lcol as variant 
dim dt as variant 
dim n as variant 

n=7 'assuming 7days to delete records 

set ws=thisworkbook.sheets(1) 
lrow=ws.range("A1:A" & rows.count).end(xlup).row 'Getting Last Row 
lcol=ws.Cells(1, .Columns.Count).End(xlToLeft).Column 'Getting Last column 
set dt=date() 'getting current date 

for i=1 to lrow 
cell=ws.cells(i,1) 'Assuming date values in column 1 Or A 
if datediff("d",dt,cell.value) > n then 
ws.row(i).entirerow.delete 
end if 
next i 
end sub 

我不确定代码。我没有测试过它。尝试代码并取回与缺点或错误在此代码

1

有2种方法可以做到这一点:

  1. 公式。只需减去2日期(下面的示例屏幕截图),然后根据您的要求筛选“差异”列,并删除不需要的数据。 Date Difference using Excel Formula

    。 2.另一种是使用VBA,它会更加动态。

    Sub Delete_Rows_based_on_Date() 
    
    Dim iRow As Integer, iCol As Integer, iNum As Integer 
    Dim iLoopR As Integer, iLoopC As Integer 
    'Benchmark Days 
    iNum = 7 
    
    iRow = Range("A1:A" & Rows.Count).End(xlUp).Row 
    iCol = Cells(1, Columns.Count).End(xlToLeft).Column 
    
    
    For iLoopR = 1 To iRow 'Assuming data starts from Row #1 
        For iLoopC = 1 To iCol 'Assuming data starts from Col #1 (A) 
         If IsDate(Cells(iLoopR, iLoopC)) Then ' Check if the cells contains a Date Value 
          If DateDiff("d", Now(), Cells(iLoopR, iLoopC)) > iNum Then 
           Cells(iLoopR, iLoopC).EntireRow.Delete 
          End If 
         End If 
        Next iLoopC 
    Next iLoopR 
    End Sub 
    
+0

任何原因不接受答案?? – jainashish

相关问题