I wish to write a macro to search a column of mixed data, find dates, and compare to "today".

Allan Williams 1 Reputation point
2021-03-28T04:51:24.677+00:00

I wish to write a macro to search a column of mixed data, find dates, and compare to "today". If less, find next, if more, short scroll up (3) and select rows 1 to "current" and delete.

{count} votes

1 answer

Sort by: Most helpful
  1. Jahanzaib Shahzad 0 Reputation points
    2023-01-17T07:52:13.3733333+00:00

    Here is an example of a VBA macro that could accomplish this task:

    
    Sub DeleteRows()
        Dim currentRow As Long
        Dim lastRow As Long
        Dim currentDate As Date
        Dim cellValue As Variant
        currentDate = Date
        lastRow = ActiveSheet.UsedRange.Rows.Count
        For currentRow = 1 To lastRow
            cellValue = ActiveSheet.Cells(currentRow, 1).Value
            If IsDate(cellValue) And cellValue < currentDate Then
                Rows(currentRow + 1 & ":" & currentRow + 3).Select
                Selection.Delete Shift:=xlUp
            End If
        Next currentRow
    End Sub
    
    

    This macro assumes that the column you want to search is the first column (column A) of the active sheet. The macro starts at the first row and checks each cell in that column to see if it contains a date. If the cell contains a date that is less than today's date, the macro selects the next three rows and deletes them. The macro will continue to iterate through the remaining rows until it reaches the last row of the used range.

    Note that this is just an example, and you may need to modify it to suit your specific needs.

    0 comments No comments