Delete Overlapping Date Ranges VBA

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I would like to produce a short script that has the ability to loop through a table of cells and delete rows that meet an overlap criteria.

I have three columns for each row. Column 1 is a random number. Column 2 is a starting date value. Column 3 is a ending date value. I would like to test each row against every other row to check for overlapping dates. If the dates overlap I would like to delete the entire row that has the larger random number between the two rows that have overlapping dates.

Any ideas or psuedo code that would accomplish this goal?

Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
To break the question down a bit...How is best to define a range given a startdate and an enddate? With a defined range of date values using a startdate and an enddate (as the contents of the cells) I would then like to use the intersect function with a conditional statement to determine any overlap.
 
Upvote 0
I would like to produce a short script that has the ability to loop through a table of cells and delete rows that meet an overlap criteria.

I have three columns for each row. Column 1 is a random number. Column 2 is a starting date value. Column 3 is a ending date value. I would like to test each row against every other row to check for overlapping dates. If the dates overlap I would like to delete the entire row that has the larger random number between the two rows that have overlapping dates.

Any ideas or psuedo code that would accomplish this goal?

Thanks!

I don't quite understand what you meant.
By 'overlap' you mean if a date falls between two dates?
Can you post some data sample?
 
Upvote 0
To break the question down a bit...How is best to define a range given a startdate and an enddate? With a defined range of date values using a startdate and an enddate (as the contents of the cells) I would then like to use the intersect function with a conditional statement to determine any overlap.

If what you mean is populate each date between 2 dates to a range, here's an example:
The codes generates dates between 2 dates starting from A5 down.

Code:
Sub toDate()

Dim stDate As Date
Dim enDate As Date
Dim d As Date

    Range("A2") = DateSerial(2018, 5, 10)
    Range("B2") = DateSerial(2018, 5, 15)
    
    stDate = CDate(Range("A2"))
    enDate = CDate(Range("B2"))
    
    i = 5
    For d = stDate To enDate
       Range("A" & i) = d
       i = i + 1
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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