Welcome toExcel.Tips.Net
Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site
Filtering Columns for Unique Values
Printing Multiple Worksheets on a Single Page
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 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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3384) applies to Microsoft Excel versions: 97 2000 2002 2003 2007
Got the Time? Understanding the ins and outs of working with times and dates can be confusing. Remove the confusion--ExcelTips: Times and Dates is an invaluable resource for learning how best to work with times and dates.