Return the value at position of MaxDate which satisfies a condition

sarathc

New Member
Joined
Mar 30, 2022
Messages
14
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
A_TimeB_TimeC_TimeCondition TimeResult
1/15/2022 6:21:26 AM1/15/2022 6:21:44 AM1/15/2022 6:21:13 AM1/15/2022 6:32:39 AMB_Time
1/14/2022 7:35:09 PM3/13/2022 12:08:45 PM1/15/2022 8:54:37 AM1/15/2022 9:02:15 AMC_Time

1) I have 3 columns (A,B and C) which contains some date and time as shown in the table above
2) I want the max of Columns A,B and C, but the condition is that it should be less than or equal to the date and time in Column D
3) And once we get that, it should return the header of the column (like B_Time, C_Time, etc.)

I tried to write a Function for this. Here is my function:

Function ClosestBef(LookupValue As String, TableArray As Range, ReturnArray As Range)
Dim Cell As Range
For Each Cell In TableArray
If Cell.Value > LookupValue Then
Cell.Value = ""
End If
Next Cell
x = Application.Max(TableArray)
y = Application.Match(x, TableArray, 0)
ClosestBef = ReturnArray(y)
End Function
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You don't need to write UDF's for such simple tasks, native functions will be more efficient.

Array confirmed with Ctrl Shift Enter.
Excel Formula:
=IFERROR(INDEX($A$1:$C$1,IF($A2:$C2=MAX(IF($A2:$C2<=$D2,$A2:$C2)),COLUMN($A$1:$C$1))),"")
 
Upvote 0
I believe Index formula has some limitation when the data is not sorted. That's why I went with custom Function.

The Index formula you gave will give wrong results when anyone of A,B,C is blank and if its not sorted.
 
Upvote 0
The formula will work fine if the data is not sorted, I had not allowed for blanks as all fields in your example contained dates.
Excel Formula:
=IFERROR(INDEX($A$1:$C$1,IF($A2:$C2=MAX(IF($A2:$C2<=$D2,IF($A2:$C2<>"",$A2:$C2))),COLUMN($A$1:$C$1))),"")
 
Upvote 0
Sorry to say, its giving wrong results for me. Don't know why. Maybe because of older excel version (2010).
 
Upvote 0
That was my bad, I missed a bit out that would have caused it to return multiple results where it should only return one.

Both of these should work, if you only have a few rows of data then it will not make much difference, with a lot of data you may find that one calculates faster than the other.
Excel Formula:
=IFERROR(INDEX($A$1:$C$1,1,MAX(IF($A2:$C2=MAX(IF($A2:$C2<=$D2,IF($A2:$C2<>"",$A2:$C2))),COLUMN($A$1:$C$1)))),"")
Excel Formula:
=INDEX($A$1:$C$1,MATCH(MAX(IF($A2:$C2<=$D2,$A2:$C2)),$A2:$C2,))
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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