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.

Dates
15-Jun-18
15-Mar-18
15-Feb-18
5-Oct-17

<colgroup><col></colgroup><tbody>
</tbody>


Range for date 1Range for date 2Range for date 3Range for date 4
20-May-1831-May-1820-May-1816-Apr-18
18-Apr-1828-Feb-1818-Feb-1816-Jan-18
19-Mar-1830-Nov-1720-Nov-1716-Oct-17
18-Feb-1831-Aug-1720-Aug-1717-Jul-17
21-Jan-1831-May-1718-May-1718-Apr-17
17-Dec-1728-Feb-1716-Feb-1717-Jan-17
30-Nov-16 17-Oct-16
31-Aug-16 15-Jul-16
31-May-16

<colgroup><col span="4"></colgroup><tbody>
</tbody>

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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,215,487
Messages
6,125,075
Members
449,205
Latest member
Healthydogs

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