VBA code to find the closest earlier date from a range

kirtesh250187

New Member
Joined
Jul 18, 2013
Messages
25
Hi,

Is there a way from which we can find the closest earlier date to a date from a range. My range for each date will change every time. I want to find the closest earlier date to the one in Dates column below.

[TABLE="width: 118"]
<colgroup><col></colgroup><tbody>[TR]
[TD]Dates[/TD]
[/TR]
[TR]
[TD]15-Jun-18[/TD]
[/TR]
[TR]
[TD]15-Mar-18[/TD]
[/TR]
[TR]
[TD]15-Feb-18[/TD]
[/TR]
[TR]
[TD]5-Oct-17[/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="width: 304"]
<colgroup><col span="4"></colgroup><tbody>[TR]
[TD]Range for date 1[/TD]
[TD]Range for date 2[/TD]
[TD]Range for date 3[/TD]
[TD]Range for date 4[/TD]
[/TR]
[TR]
[TD]20-May-18[/TD]
[TD]31-May-18[/TD]
[TD]20-May-18[/TD]
[TD]16-Apr-18[/TD]
[/TR]
[TR]
[TD]18-Apr-18[/TD]
[TD]28-Feb-18[/TD]
[TD]18-Feb-18[/TD]
[TD]16-Jan-18[/TD]
[/TR]
[TR]
[TD]19-Mar-18[/TD]
[TD]30-Nov-17[/TD]
[TD]20-Nov-17[/TD]
[TD]16-Oct-17[/TD]
[/TR]
[TR]
[TD]18-Feb-18[/TD]
[TD]31-Aug-17[/TD]
[TD]20-Aug-17[/TD]
[TD]17-Jul-17[/TD]
[/TR]
[TR]
[TD]21-Jan-18[/TD]
[TD]31-May-17[/TD]
[TD]18-May-17[/TD]
[TD]18-Apr-17[/TD]
[/TR]
[TR]
[TD]17-Dec-17[/TD]
[TD]28-Feb-17[/TD]
[TD]16-Feb-17[/TD]
[TD]17-Jan-17[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]30-Nov-16[/TD]
[TD] [/TD]
[TD]17-Oct-16[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]31-Aug-16[/TD]
[TD] [/TD]
[TD]15-Jul-16[/TD]
[/TR]
[TR]
[TD] [/TD]
[TD]31-May-16[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
</tbody>[/TABLE]

Thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Is this what you mean? Formula shown is copied down.

Excel Workbook
ABCD
1DatesEarlier Date
215-Jun-1820-May-18
315-Mar-1828-Feb-18
415-Feb-1820-Nov-17
55-Oct-1717-Jul-17
6
7Range for date 1Range for date 2Range for date 3Range for date 4
820-May-1831-May-1820-May-1816-Apr-18
918-Apr-1828-Feb-1818-Feb-1816-Jan-18
1019-Mar-1830-Nov-1720-Nov-1716-Oct-17
1118-Feb-1831-Aug-1720-Aug-1717-Jul-17
1221-Jan-1831-May-1718-May-1718-Apr-17
1317-Dec-1728-Feb-1716-Feb-1717-Jan-17
1430-Nov-1617-Oct-16
1531-Aug-1615-Jul-16
1631-May-16
17
Earlier Date



Edit: I forgot the thread title said vba. Do you really need the vba?
 
Last edited:
Upvote 0
Yes, this is what i am looking for but can we do it via macro because it wanted to make it a part of another larger macro. Thanks
 
Upvote 0
For that layout, try
Code:
Sub EarlierDate()
  Dim lr As Long, c As Long
  Dim r As Range
  
  Const fr As Long = 8
  
  For Each r In Range("A2", Range("A1").End(xlDown))
    c = c + 1
    lr = Cells(Rows.Count, c).End(xlUp).Row
    r.Offset(, 1).Value = Evaluate(Replace(Replace(Replace( _
                          "aggregate(14,6,index($a$#:$d$%,0,rows(a$2:a@))/(index($a$#:$d$%,0,rows(a$2:a@))<a@),1)" _
                          , "#", fr), "%", lr), "@", r.Row))
  Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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