Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Convert String to Date

Tags:

excel

vba

vb6

I have data in an excel sheet in the following format:

ItemCode                            DeliveryDate
5456987                              24.01.2009
5456988                                          
5456989                              12.24.2009
5456990                              12/24/2009

I have stored the values of DeliveryDate in an array. I need to make decision on basics of date and then print the result in a new sheet. So I have to convert the values into array:

Dim current as Date, highest as Date, result() as Date
For Each itemDate in DeliveryDateArray
    current = CDate(itemDate)
    if current > highest then
         highest = current
    end if
    ' some more operations an put dates into result array
Next itemDate
'After activating final sheet...
Range("A1").Resize(UBound(result), 1).Value = Application.Transpose(result)

Unfortunately, CDate() function throws the error:

Run-time error '13':

Type mismatch

Is there a function in VBA which can:

  • parse string with any date format and return a date object to work with.
  • return an empty date object, if the string is empty or malformed (for comparison in the loop).

Edit:

To reproduce the error, simply run myDate = CDate("24.01.2009")

like image 208
vulcan raven Avatar asked Jul 21 '12 02:07

vulcan raven


People also ask

How do I convert a string to a date in VBA?

Excel VBA String to Date. In VBA, there is a method through which we can convert a given string to a date. The method is known as the CDATE function in VBA. It is an inbuilt function in VBA, and the parts required for this function are first to convert the string to a number, then convert the given number to date.

What does Cdate do in VBA?

VBA CDATE is a data type conversion function that converts a data type, either text or string, to a date data type. Once we convert the value to date data type, we can play around with date stuff.

Can you cast a string as a date?

Convert string to date using CAST() function In this syntax, the string can be any DATE value that is convertible to a date. The CAST() function returns a DATE value if it successfully converts the string to date.


1 Answers

Try using Replace to see if it will work for you. The problem as I see it which has been mentioned a few times above is the CDate function is choking on the periods. You can use replace to change them to slashes. To answer your question about a Function in vba that can parse any date format, there is not any you have very limited options.

Dim current as Date, highest as Date, result() as Date 
For Each itemDate in DeliveryDateArray
    Dim tempDate As String
    itemDate = IIf(Trim(itemDate) = "", "0", itemDate) 'Added per OP's request.
    tempDate = Replace(itemDate, ".", "/")
    current = Format(CDate(tempDate),"dd/mm/yyyy")
    if current > highest then 
        highest = current 
    end if 
    ' some more operations an put dates into result array 
Next itemDate 
'After activating final sheet... 
Range("A1").Resize(UBound(result), 1).Value = Application.Transpose(result) 
like image 57
Mark Hall Avatar answered Nov 13 '22 00:11

Mark Hall