Return closest date

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
721
Office Version
  1. 2016
Platform
  1. Windows
I have a lengthy spreadsheet, I need to find which of two dates is the closest to a third date.

As an example, in "T" I would like to return the date from either "R" or "S" that is closest to "M" (positive or negative difference).

MNOPQRST
1910/17/2022Y  210/14/202210/18/2022Closest date goes here
KPI Data (Pure Retail)
Cell Formulas
RangeFormula
O19O19=IF(LEFT(N19,1)="Y","",(M19-TODAY())*-1)
P19P19=IF(N19<>"","",IF(M19+10<TODAY(),"Expired",M19+10))
Cells with Conditional Formatting
CellConditionCell FormatStop If True
O2:O9463Cell Value=10textNO
O2:O9463Expression=AND(ISNUMBER(O2),O2>10)textNO
P2:P9463Cell Valuecontains "Expired"textNO
P2:P9463Expression=AF2=TODAY()textNO
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I wrote a macro that works, but would still like to know if there is a formula that could do the same trick.

Thank you.

VBA Code:
Sub test4()

    lastrow = Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To lastrow

        If Range("Q" & x) > 1 Then

            one = Range("R" & x).Value - Range("M" & x).Value
            two = Range("S" & x).Value - Range("M" & x).Value

            If one < 0 Then one = one * -1

            If one < two Then
                Range("T" & x).Value = Range("R" & x).Value
            Else: Range("T" & x).Value = Range("S" & x).Value
            End If
    
        End If

    Next x

End Sub
 
Upvote 0
Book1
MNOPQRST
1910/17/2022Y  210/14/202210/18/202210/18/2022
Sheet2
Cell Formulas
RangeFormula
O19O19=IF(LEFT(N19,1)="Y","",(M19-TODAY())*-1)
P19P19=IF(N19<>"","",IF(M19+10<TODAY(),"Expired",M19+10))
T19T19=INDEX(R19:S19, MATCH(MIN(ABS(R19:S19-M19)), ABS(R19:S19-M19), 0))
 
Upvote 0
Solution
Book1
MNOPQRST
1910/17/2022Y  210/14/202210/18/202210/18/2022
Sheet2
Cell Formulas
RangeFormula
O19O19=IF(LEFT(N19,1)="Y","",(M19-TODAY())*-1)
P19P19=IF(N19<>"","",IF(M19+10<TODAY(),"Expired",M19+10))
T19T19=INDEX(R19:S19, MATCH(MIN(ABS(R19:S19-M19)), ABS(R19:S19-M19), 0))
Thank you, I had found something similar but was organizing the formula incorrectly.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,871
Members
449,097
Latest member
dbomb1414

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