Excel VBA String to Date

One of the common problems we all face with Excel is “Date & Time,” which are often stored as text values and go unnoticed initially. But, when they require the use of that time, we know that those values are stored as text and don’t know how to deal with them. That is because “Date & Time” are two combined things in one element. But, once those values as text values are stored, it is a pain to work with.

You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA String to Date (wallstreetmojo.com)

How to Convert String Values to Date?

Example #1

Once the VBA variable is declaredVBA Variable Is DeclaredVariable declaration is necessary in VBA to define a variable for a specific data type so that it can hold values; any variable that is not defined in VBA cannot hold values.read more and assigned as String, anything assigned to that variable will be treated as a string only. For example, look at the below code.

Code:

Sub String_To_Date()

Dim k As String k = “10-21” MsgBox k

End Sub

The above code defines the variable “k” as the “String” data type. We have assigned this variable the value of “10-21.”

Run the code and see what we get in the message box in VBAMessage Box In VBAVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided.read more.

We only got the value as 10-21. But usually, these values are dates, not string values. So, even though the data type assigned is “String,” we can still convert to date using the data type conversion function CDATE VBACDATE VBAThe VBA CDATE Function converts a variable (text or string) to the date data type. VBA displays the run-time 13 error if it is unable to convert a value to Date & Time.read more.

Sub String_To_Date()

Dim k As String k = “10-21” MsgBox CDate(k)

End Sub

In the above, before we show the variable “k” result in the message box. Then, we assigned the CDATE function. Then, we made a small adjustment. Finally, let us see how big an impact it makes.

Now, we would see the result as “Date” no longer as a “String” value.

Example #2

Look at the below code.

Sub String_To_Date()

Dim k As String k = 43599 MsgBox k

End Sub

The above code would show the result as “43599,” as we assigned above.

But once we use the CDATE function, it will convert to the date value.

Sub String_To_Date()

Dim k As String k = 43599 MsgBox CDate(k)

End Sub

The result after applying the CDATE function is as follows.

Since Excel stored the date as serial numbers, our assigned serial number 43599 equals the date 05/14/2019 when the date format is applied.

To read the date accurately, we can apply the format to the date as “DD-MMM-YYYY.”

Sub String_To_Date1()

Dim k As String Dim DateValue As Date

k = 43599 DateValue = CDate(k) MsgBox Format(DateValue, “DD-MMM-YYYY”)

End Sub

We have declared one extra variable to store the result in the above. For this variable, we have applied the CDATE conversion function.

Next, we used the FORMAT function to apply the “DD-MMM-YYYY” format. The result will be as shown below.

With this, we can read the daypart and month part. Of course, it also depends on your system date format in excelDate Format In ExcelThe date format in Excel can be changed either from the “number format” of the Home tab or the “format cells” option of the context menu.read more. Since my system date format was “MM-DD-YYYY,” it was showing like that, but that should not hinder the format.

Example #3

Now, we will see how dates format as text values in worksheet cells. Below is the image of the dates stored as text in a worksheet.

In column A from A2 to A12, we have date-looking values, but when we look at the format tab, it shows “Text” format. So, now we need to convert these values from text to date.

Below is the code we have written to convert the text-formatted date values to actual dates.

Sub String_To_Date2()

Dim k As Long

‘Data is in more than one cell, so need to loop through each cell ‘Open For Loop

For k = 2 To 12 ‘Data starts from 2nd row and ends at 12th row, so 2 to 12 Cells(k, 2).Value = CDate(Cells(k, 1).Value) Next k

End Sub

If you run the code, it will give us the below result.

Things to Remember

  • The CDATE is a data type conversion function that can convert VBA stringVBA StringString functions in VBA do not replace the string; instead, this function creates a new string. There are numerous string functions in VBA, all of which are classified as string or text functions.read more stored date to actual date values.The result of the CDATE function format depends on the system date format only.Dates are stored as serial numbers in Excel, so formatting is required to show them as dates.

This article has been a guide to VBA String to Date. Here, we discuss converting date stores in string values to date format in Excel VBA using the CDATE function and examples. You can learn more about VBA functions from the following articles: –

  • VBA Format DateSplit String into Array in VBASet Range in VBAGet Value from Cell in VBA