使用VBA根据日期删除行。

huangapple go评论70阅读模式
英文:

Delete rows based on date using VBA

问题

Each month new data (from the previous month) is pasted over old data. The number of days each month changes due to nonwork days.

I tried clearing the cells and pasting data in blank cells, but this messes up the associated charts and graphs.

I want to delete entire rows with data before the last month, day 1.

英文:

Each month new data (from the previous month) is pasted over old data. The number of days each month changes due to nonwork days.

I tried clearing the cells and pasting data in blank cells, but this messes up the associated charts and graphs.

I want to delete entire rows with data before the last month, day 1.

使用VBA根据日期删除行。

Sub ltest()

Dim d As Date
d = DateAdd("y", -1, Date)
LastMonth = Month(DateAdd("m", -1, Date))

Dim w As Long
For w = Sheet1.[a4].SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
    Debug.Print Cells(w, "A").Value
    If CDate(Cells(w, "A")) < CDate(LastMonth) Then
        Cells(w, "A").EntireRow.Delete
    End If
Next w

End Sub

Another attempt

Sub DeleteRowBasedOnDateRange()
Dim N As Long, I As Long
Dim lmon As Long

lmon = Month(DateAdd("m", -1, 1))
N = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row

For I = N To 1 Step -4
    If Sheet1.Cells(I, "A").Value < lmon Then
        Sheet1.Rows(I).Delete
    End If
Next I

End Sub

答案1

得分: 0

Sub DeleteOldDates()
Dim ws As Worksheet
Dim lastRow As Long
Dim previousM As Date
Dim i, rowDate

Application.ScreenUpdating = False

Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change "Sheet1" to your sheet's name
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Get last row in column A

'Get current month and subtract 1 to get previous month
previousM = DateSerial(Year(Date), Month(Date) - 1, 1)

For i = lastRow To 1 Step -1
    rowDate = Cells(i, "A").Value
    If IsDate(rowDate) Then 'Check if cell contains a valid date
        If Year(rowDate) < Year(previousM) Then
            Cells(i, "A").EntireRow.Delete
        ElseIf Month(rowDate) < Month(previousM) And Year(rowDate) = Year(previousM) Then
            Cells(i, "A").EntireRow.Delete
        End If
    End If
Next i

Application.ScreenUpdating = True

End Sub

英文:

example:

Sub DeleteOldDates()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim previousM As Date
    Dim i, rowDate
    
Application.ScreenUpdating = False

    Set ws = ThisWorkbook.Worksheets(&quot;Sheet1&quot;) &#39;Change &quot;Sheet1&quot; to your sheet&#39;s name
    lastRow = ws.Cells(ws.Rows.Count, &quot;A&quot;).End(xlUp).Row &#39;Get last row in column A
    
    &#39;Get current month and subtract 1 to get previous month
    previousM = DateSerial(Year(Date), Month(Date) - 1, 1)
        
    For i = lastRow To 1 Step -1
    rowDate = Cells(i, &quot;A&quot;).Value
        If IsDate(rowDate) Then &#39;Check if cell contains a valid date
            If Year(rowDate) &lt; Year(previousM) Then
                Cells(i, &quot;A&quot;).EntireRow.Delete
            ElseIf Month(rowDate) &lt; Month(previousM) And Year(rowDate) = Year(previousM) Then
                Cells(i, &quot;A&quot;).EntireRow.Delete
            End If
        End If
    Next i
    
Application.ScreenUpdating = True
End Sub

huangapple
  • 本文由 发表于 2023年5月10日 20:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定