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

의 단점이나 오류로 돌아가 :

  1. 공식을 사용하여이. 2 개의 날짜 (아래 예시 스크린 샷)를 빼고 요구 사항에 따라 '차이'열을 필터링하고 원치 않는 데이터를 삭제하십시오. Date Difference using Excel Formula

    . 2.Abother는 훨씬 더 동적 인 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