1

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
3
  • 1
    Well, what's the value of RawExtract when it throws an error? Also, Format returns a string, and your function returns a date. Either change your function to return a string, or remove the format to 'yyyy/mm/dd'. Commented Oct 16, 2018 at 13:08
  • it means the date in the cell can not be converted using CDate. Commented Oct 16, 2018 at 13:10
  • as @JoshEller implies, most likely, the ten left-most characters of whatever is in the cell is not something that can be considered a date. Add a breakpoint and see what is there. Commented Oct 16, 2018 at 13:32

2 Answers 2

3

Your function looks fine, just need to make some minor changes:

Function FnDateExtract(fnFile, fnTab, fnRow, fnColumn) As Date
    Dim RawExtract As String
    With Workbooks(fnFile).Worksheets(fnTab)
        RawExtract = Left(CStr(.Cells(fnRow, fnColumn).Text), 10)
        FnDateExtract = CDate(Format(RawExtract, "yyyy/mm/dd"))
    End With
End Function

Moved the CDate() to outside the Format() function in order to set return type to Date

Also please note that you can use InStr(*string*, " ") - 1 instead of 10 with the Left() function to grab the date string. It may be more accurate in certain situations.

Sign up to request clarification or add additional context in comments.

7 Comments

Actually, you don't need to test for Len > 10 since the LEFT function will not return more characters than are present.
This is true, thank you for the input will adjust answer accordingly @RonRosenfeld
IMO this will not solve the issue in case the first ten characters cannot be interpreted as a date. You have to use IsDate beforehand in order to check if CDate does not run into an error.
@Storax this is based off of the information provided in the question. You can post a more robust solution if you feel it is needed
@Kubie thanks! Taking away CDate actually fixes it. Although I am a bit confused. Why is it possible to convert it to the yyyy/mm/dd format simply with Format? How does it recognize which numbers are dates/years/months? Does it just cycle through conventional date formats?
|
1

It seems that you have data which cannot converted into dates. That means you have to make your code more robust. A first version could look like that

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)

        If IsDate(RawExtract) Then
            FnDateExtract = CDate(Format(RawExtract, "yyyy/mm/dd"))
        Else
            FnDateExtract = vbEmpty
        End If

    End With

End Function

In a more improved version one would properly define the parameters maybe like that

Function FnDateExtract(ByVal fnFile As String, ByVal fnTab As String, _
                    ByVal fnRow As Long, ByVal fnColumn As Long) As Date

The function can still easily be disrupted as you assume you have a workbook named fnFile with a sheet fnTab. The easiest (maybe also quick and dirty) way to handle this is to add an error handler.

So the second version could look like that

Function FnDateExtract(ByVal fnFile As String, ByVal fnTab As String, _
                    ByVal fnRow As Long, ByVal fnColumn As Long) As Date
Dim RawExtract As String

On Error GoTo EH

    With Workbooks(fnFile).Worksheets(fnTab)
        RawExtract = .Cells(fnRow, fnColumn).Text
        If Len(RawExtract) > 10 Then RawExtract = Left(RawExtract, 10)
        If IsDate(RawExtract) Then
            FnDateExtract = CDate(Format(RawExtract, "yyyy/mm/dd"))
        Else
            FnDateExtract = vbEmpty
        End If
    End With

    Exit Function

EH:
    FnDateExtract = vbEmpty

End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.