A_Time | B_Time | C_Time | Condition Time | Result |
1/15/2022 6:21:26 AM | 1/15/2022 6:21:44 AM | 1/15/2022 6:21:13 AM | 1/15/2022 6:32:39 AM | B_Time |
1/14/2022 7:35:09 PM | 3/13/2022 12:08:45 PM | 1/15/2022 8:54:37 AM | 1/15/2022 9:02:15 AM | C_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
End Function