Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Deleting Old Data from a Worksheet.

Deleting Old Data from a Worksheet

Written by Allen Wyatt (last updated January 8, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


3

Gene is looking for a way to quickly delete data from a worksheet based on the date in a particular column. If the date is older than today (the date is passed) then the row should be deleted.

This can be rather easily done with a macro. All you need to do is have the macro step through the data and compare the date in each row to today's date. If the date is less than today, then the Delete method is used on the EntireRow object.

Sub DeleteRows1()
    Dim x As Long
    Dim iCol As Integer

    iCol = 7 'Filter all on Col G

    For x = Cells(Cells.Rows.Count, iCol).End(xlUp).Row To 2 Step -1
        If Cells(x, iCol).Value < Date Then
            Cells(x, iCol).EntireRow.Delete
        End If
    Next
End Sub

In this example, the macro checks column G (in the iCol variable) for the date. If your date is in a different column, then you should make the change to the variable. Depending on the number of rows of data in your worksheet, the macro may also take quite a while to run.

If you notice a lag in performance, then you may want to use a different approach. The following example uses the AutoFilter capabilities of Excel to first filter the data to show only the old data, and then deletes those rows.

Sub DeleteRows2()
    Dim Dates As Range
    Dim nRows As Double
    Dim currDate As Variant

    'Format dates as text
    Range("Dates").NumberFormat = "@"
    'Today's date in number format
    currDate = CDbl(Date)
    Range("Dates").AutoFilter Field:=1, _
      Criteria1:="<" & currDate
    nRows = Range("Dates").Rows.Count
    Rows("2:" & nRows).Select
    Selection.Delete Shift:=xlUp
    Range("Dates").AutoFilter
    Range("Dates").NumberFormat = "m/d/yyyy"
    Range("C2").Select
End Sub

This macro presumes that you have taken the step of assigning a name to your data range. Select all the cells in your data table—including any heading row—and give it the name "Dates." When you run the macro, it uses this range as the target for the AutoFilter.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10783) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Deleting Old Data from a Worksheet.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Putting Cell Contents in Footers

Referencing information between cells in a worksheet is a piece of cake using some elemental formulas. You cannot, ...

Discover More

Saving a Workbook Using Passwords

One of the security features built-in to Excel is the ability to save a workbook using a password that limits access to ...

Discover More

Returning to Your Document after Adding an Endnote

Endnotes are a required element in some types of writing. When you add an endnote, Word moves you to the end of your ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Deriving an Absolute Value in a Macro

Need to figure out an absolute value within your macro code? It's easy to do using the Abs function, described in this tip.

Discover More

Converting Numbers Into Words

Write out a check and you need to include the digits for the amount of the check and the value of the check written out ...

Discover More

Adjusting a Path Based on System and User

It is not uncommon to set variables in a macro based on other values, such as time or date. You could also set variables ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four minus 0?

2022-02-15 12:05:08

J. Woolley

For a similar Tip, see
https://excelribbon.tips.net/T001566_Deleting_Rows_before_a_Cutoff_Date.html


2022-02-03 04:14:44

Peter

Thanks Allen, I like your second idea. I regularly delete rows using if they meet some criteria and it is a bit slow.


2022-02-02 09:20:18

Duncan McNeill

hello, I'm new to working with the more scripted side of excel, but I was wondering if there is a way that would delete/overwrite data on a certain day of the week, may also need to add a time component so it would not consistently delete data. I have a group excel page that we are using to track availability over two weeks, and what I want to do is to move the data from the second week in place of the first week, through either overwriting or deleting and moving the data onto week one. For the most part I am more concerned about the deleting aspect as this is the first fundamental step to lead to the other and I'm not finding exactly what i want. Any help will be appreciated


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.