I am having trouble searching a range containing workday dates for the closest one to the target.
Ie. Jan 26, 2014 is on a Sunday, if this is the start date of a project I want the function to output Jan 24,2014. (This function is triggered by another sub, so I'm not really using it to output to the spreadsheet right away.)
Anyway, I've developed the following code.
To test the function I've inputted the following in the immediate window.
The range looks like the following except that the cells content are rotated 90 degrees and is three rows tall for the reports visual output:
[TABLE="class: grid, width: 540"]
<tbody>[TR]
[TD]1/17/2014[/TD]
[TD]1/20/2014[/TD]
[TD]1/21/2014[/TD]
[TD]1/22/2014[/TD]
[TD]1/23/2014[/TD]
[TD]1/24/2014[/TD]
[TD]1/27/2014[/TD]
[TD]1/28/2014[/TD]
[TD]1/29/2014[/TD]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
</tbody>[/TABLE]
The result thus far is that the hlookup is not finding the 24th and continues to rollback the dtlookup. If I change the 24th to a text field type and the dtlookup to a string type(currently it is a short date type), the function locates the cell. However this creates a lot of problems for the sub that will use this function. So I'd like to maintain everything as a date format.
Any thoughts and comments are greatly appreciated.
Ie. Jan 26, 2014 is on a Sunday, if this is the start date of a project I want the function to output Jan 24,2014. (This function is triggered by another sub, so I'm not really using it to output to the spreadsheet right away.)
Anyway, I've developed the following code.
Code:
Function tmep(dtlookup As Date, rngHdrDt As Range, startDt As Boolean) As Date
On Error GoTo errDtNtFnd
tmep = Application.WorksheetFunction.HLookup(dtlookup, rngHdrDt, 1, False)
On Error GoTo 0
Exit Function
errDtNtFnd:
If startDt Then
dtlookup = DateAdd("d", -1, dtlookup)
Else: dtlookup = DateAdd("d", 1, dtlookup)
End If
Resume
End Function
To test the function I've inputted the following in the immediate window.
Code:
sheet4.tmep(#1/26/2014#,sheets("Report").Range("D6:ACF8"),true)
The range looks like the following except that the cells content are rotated 90 degrees and is three rows tall for the reports visual output:
[TABLE="class: grid, width: 540"]
<tbody>[TR]
[TD]1/17/2014[/TD]
[TD]1/20/2014[/TD]
[TD]1/21/2014[/TD]
[TD]1/22/2014[/TD]
[TD]1/23/2014[/TD]
[TD]1/24/2014[/TD]
[TD]1/27/2014[/TD]
[TD]1/28/2014[/TD]
[TD]1/29/2014[/TD]
[/TR]
[TR]
[/TR]
[TR]
[/TR]
</tbody>[/TABLE]
The result thus far is that the hlookup is not finding the 24th and continues to rollback the dtlookup. If I change the 24th to a text field type and the dtlookup to a string type(currently it is a short date type), the function locates the cell. However this creates a lot of problems for the sub that will use this function. So I'd like to maintain everything as a date format.
Any thoughts and comments are greatly appreciated.