2017-03-17 48 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

相關問題