Deleting Lines of data when one of the columns contains a date that is less than 6 months from now

MarsBars

New Member
Joined
May 21, 2014
Messages
27
Hi Everybody. First time poster here. I'm new to prgramming in general and have only been doing VBA work for a couple of weeks. So please feel free to ask for clarification. I only ask that you use "little words" that I can understand.

I am trying to set up a few lines of code that will delete a line if the date in one column is less than 6 months from today's date. Here is what I have:

Dim i2 As Integer
i2 = 1
Dim x As Integer
Dim today As Integer
Dim SLED As Integer

Do While Cells(i + 1, 1) <> ""
x = DateValue(Cells(i + 1, 18))
today = DateValue(Date)
SLED = 183
If (x - Date) < SLED Then
Cells(i + 1, 18).EntireRow.Delete
Else
i = i + 1
End If
Loop

It is a slightly modified version of a code I used to remove a line if a different column was below a certain value. That one works like a charm, but this one doesn't. It doesn't throw up an error, but it just kind of doesn't do what I need it to. After it runs, I see dates from July still in the data set. I've tried to tweak a few different things, so I may have a few diffferent problems in there.

Thanks!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I rewrote your code.

Dim i As Integer
i = 1
Dim x As Date
Dim today As Date
Dim SLED as Integer

Do While Cells(i + 1, 1) <> ""
x = DateValue(Cells(i + 1, 4))
today = DateValue(Date)
SLED = 183
If (Date - x) < SLED Then
Cells(i + 1, 4).EntireRow.Delete
Else
i = i + 1
End If
Loop
 
Upvote 0
Another way, assuming your data table begins on row 2:

Code:
Sub DeleteRows()
  Dim k As Long, LR As Long
  LR = Cells(Rows.Count, 1).End(xlUp).Row
    For k = LR To 2 Step -1
      If Cells(k, 18) - Date < 183 Then
        Rows(k).Delete
      End If
    Next k
End Sub
 
Upvote 0
About 10 minutes after I posted this, I noticed that I hadn't defined the dates correctly. That did it. Thanks for the replies though!
I liked what you did with your code Osvaldo. However, I don't understand how this line works:

For k = LR To 2 Step -1

Can you explain what this does? I'm interested for possible future use.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,215,597
Messages
6,125,741
Members
449,256
Latest member
Gavlaar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top