I wrote a simple function to extract date from cells. I have some cells containing strings "5-Oct-2018" and other cells containing "2018/10/10 Random Texts". I want to truncate the random texts and also convert the rest of the strings into dates with the same format.
Since the random texts only ever show up when the string is >10 characters or longer, I decided to truncate everything to the right.
I wrote the following, but it keeps getting stuck on this line "FnDateExtract = Format(CDate(RawExtract), "yyyy/mm/dd")" saying Type Mismatch. What am I doing wrong?
Function FnDateExtract(fnFile, fnTab, fnRow, fnColumn) As Date
Dim RawExtract As String
With Workbooks(fnFile).Worksheets(fnTab)
RawExtract = .Cells(fnRow, fnColumn).Text
If Len(RawExtract) > 10 Then RawExtract = Left(RawExtract, 10)
FnDateExtract = Format(CDate(RawExtract), "yyyy/mm/dd")
End With
End Function