Avatar of Utredning
Utredning

asked on 

Delete rows based on date criteria

Hi,

I am looking for a macro that delete rows based on date criterias. 

My data table starts in cell A10. And this first column contains a date. And would like to have all rows containg today's date deleted. 

Microsoft OfficeMicrosoft Excel* MS Excel Macro

Avatar of undefined
Last Comment
Utredning
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

The fastest way with a large set of data would be  to use AutoFilter. Try this

Sub deleteFilteredData()
    Dim rDelete    As Range
    Dim lCalc  As Long
    Dim sDelete As String
    
    With Application
        lCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        On Error GoTo exit_proc
        
        ''///Sheet with the data, change the name
        With ActiveSheet
            .AutoFilterMode = False
            ''///Apply the filter, this range of data starts in A1
            .Cells(1, 1).CurrentRegion.AutoFilter Field:=1, Criteria1:="=" & Date
            With .AutoFilter.Range
                On Error Resume Next
                Set rDelete = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
                .SpecialCells(xlCellTypeVisible)
                On Error GoTo 0
                If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
            End With
            ''///Remove the AutoFilter
            .AutoFilterMode = False
        End With
        
exit_proc:
        .ScreenUpdating = True
        .Calculation = lCalc
    End With
    
End Sub

Open in new window

Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I’m glad I was able to help.

If you expand the “Full Biography" section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Distinguished Expert in Excel 2018
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2020
              Experts Exchange Top Expert VBA 2018 to 2020
Avatar of Utredning
Utredning

ASKER

Thanks for your help :)
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo