Function behaving differently
I have a strange problem with a with two functions behaving differently even though they are essentially the same.
First of all, I have this sub:
Code:
Private Sub FindDate1()
Const dDate As String = "1/12/2023"
Dim TargetCell As Range
Set TargetCell = Worksheets("DP").Rows("1").Find(What:=CDate(dDate), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If (Not TargetCell Is Nothing) Then
Debug.Print TargetCell.Column
End If
End Sub
which works as it should. That is to say that it returns the column of the cell which contains 1/12/23
Now, when I 'enhance' this a little to this:
Code:
Private Function FindDate2(dDate As String) As Range
Dim TargetCell As Range
Set TargetCell = Worksheets("DP").Rows("1").Find(What:=CDate(dDate), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
If (Not TargetCell Is Nothing) Then
Set FindDate2 = TargetCell
End If
End Function
it stops working entirely. The Find function returns nothing and therefore the function does not return anything.
Why this is strange is that the Find function is exactly the same in both the sub and function. So, how can it find the field in one but not the other???
Any ideas?
Thanks