VBA CDATE: How to Convert String or Text into Dates
Visual Basic for Applications or VBA is a scripting language that enables automation of tasks in the Microsoft Office suite of products. It’s also an event driven programming language which contains a lot of useful functions. It is leveraged by programmers to create effective and efficient macros. Read more about what VBA can help you do, in this tutorial. Today, we are going to take it a step ahead in this intermediate tutorial and take a look at a vital date function namely CDate(). This function is used to convert the strings or numbers into date format.
We assume that you have working knowledge of MS Excel and VBA. If you are new to this concept, we suggest you to go through our introductory course to Excel VBA.
Date Systems in Microsoft Excel
Date and time functions are frequently used in MS Excel and VBA. Therefore, it’s important that you know the basic concepts of date and time functions if you want to manipulate and use them in your programs. Please note that MS Excel stores date as sequential numbers known as serial values. MS Excel considers time to be a part of a day and it is stored as decimal fractions. Dates and times can be added and subtracted. Therefore they can be included in many calculations and VBA programs. That is you can compare two dates or subtract one date from another. You can learn more about this in our Excel course.
There are two date systems in MS Excel- the 1900 and 1904. By default Microsoft Excel for Windows is 1900 and for Macintosh is 1904. You can change the date system if you want by clicking on Tools menu. Then click “Options” and choose “Calculation tab.” In Calculation tab, select or clear the 1904 date system checkbox.
You should also be aware that, the date system will be changed automatically when you open an Excel worksheet from another platform. That is when you open a worksheet created in Excel for Macintosh, while working in Excel for Windows, the system will automatically select the 1904 date system.
The following table shows the first and last date for each date system.
|1900||January 1, 1900
(serial value 1)
|December 31, 9999
(serial value 2958465)
|1904||January 2, 1904
(serial value 1)
|December 31, 9999
(serial value 2957003)
How Microsoft Excel Interprets Two-Digit Years
If you are using Microsoft Windows 2000 or later versions, the regional options in Control Panel controls how Excel interprets two-digit years. If you want MS Excel to assume the year values as you intended it to be, enter the year as a four digit value. The reason is, Excel will not interpret century.
For example instead of entering “00” for the year 2000, you should enter the all the four digits “2000”.
How MS Excel Interprets Year Portion of the Date Entered as a String or Text
- 00 through 29– When you enter the date as 3/24/14, Excel assumes the date is March 24, 2014.
- 30 through 99– Here MS Excel interpret the two-digit year values 30 through 99 as the years 1930 through 1999. That is if you enter the date in the cell as 3/24/94, Excel will interpret the date as March 24, 1994.
Microsoft Excel VBA has many date and time functions.A few of the important ones being Date(), Day(), Now(), DateAdd(), TimeSerial() and DateSerial(). It also has equal number of data type conversion functions. An important data type conversion function that is frequently used in VBA programming is CDate(). To learn more about Excel VBA, you can check out this course.
What is CDate() Function
CDate identifies date and time literals (along with some numbers that fall inside the range of acceptable dates) and converts them to the date date type. If there a fractional part of the number it is converted to a time of day starting from midnight.
The CDate() function identifies date formats depending on the locale setting of your system. The correct order of day, month and year will not be realized by this function if it is entered in a format other than one of the recognized date settings. If the year is not specified in the date, then the current year is used. Also note that a long date format is not recognized if it also contains the day-of-the-week string.
VBA CDate()converts a value of any data type, string or integer, into a date type. The syntax of this date function looks like this
Here the argument expression is mandatory. When you enter a valid date and time expression this function converts it into type Date. Let’s make this concept clear using a simple example.
Function FnCDate () Dim strDate_1 Dim intDate_1 Dim strTime_1 strDate_1 = "January 24, 2014" intDate_1 = #12/03/2014# strTime_1 = "15:30:15 PM" MsgBoxCDate(strDate_1) MsgBoxCDate(intDate_1) MsgBoxCDate(strTime_1) End Function
In the program, three variables strDate_1, IntDate_1 and strTime_1 are declared. These variables are assigned values “January 24, 2014”, #12/03/2014# and “15:30:15 PM” respectively. The variables are passed through the CDate() function. The output is as follows.
For the MsgBoxCDate(strDate_1) the output displayed is “24-01-2014.”
For the MsgBoxCDate(intDate_1) the output displayed is “03-12-2014.”
For the MsgBoxCDate(strTime_1) the output displayed is “15:30:15.”
How to Convert Text String to Date in MS Excel VBA Using CDate() Function
When you are working with data that contains dates, you need to have a clear idea about how to use the date data type. From MS Excel versions 2000 onwards, you have VBA to automate tasks that you would otherwise perform manually. This is normally done with the help of macros (you can learn more about VBA macros in this course). VBA CDate()function is one of the most commonly used date functions to convert a date stored in a “String” variable into a date data type. The example given below will help make the concept clear.
Open your MS Excel and enter the date in cell A1 in the following format:
March 24, 2014
To open the “Visual Basic Editor”, click the “Developer” tab and then “Visual Basic”. In the editor click “Insert” menu and click “Module.” This will insert a new code module. Here is where you write the code.
Sub converTextToDate() Dim Current_DateAs Date Dim Date_StringAs String Range("A1").Select Date_String = Range("A1").Value Current_Date = CDate(date_String) Range("C1").Select Range("C1").Value = Current_Date End Sub
Let’s take a closer look at the program.
In this program, two variables, “Current_Date” and “Date_String” have been declared as type Date and String respectively. Range(“A1”).Select will get the date entered in the cell A1.Date_String = Range(“A1”).Valuestores in the “string” variable Date_String.Current_Date=CDate(date_string) converts the string into Date data type and stores it in the variable “Current_Date”.The last two steps will move the cursor to cell C1 and display the date in column C1 when the procedure is executed.
In the next program, we take a look at how to compare two dates in VBA using CDate() function.
Note that to compare dates in VBA, you need to have dates stored in variables of type “Date”. However, if the dates that you are comparing are entered as numbers or string, you need to convert them using CDate() function. “If statement” is used to compare the dates. The following example helps make the concept clear.
Public Sub CompareDates() Dim d1, d2 As Date d1 = CDate("24/1/2013") d2 = CDate("24/1/2014") If (d1 < d2) Then Debug.Print "Date 1 occurs earlier than date 2." If (d1 > d2) Then Debug.Print "Date 1 occurs later than date 2." If (d1 = d2) Then Debug.Print "Date 1 is the same as date 2." End Sub
If you happen to work regularly on data which contains date columns, it is always wise to create a macro. Call that Macro whenever you want to delete rows in your table. Here is an example which deletes the dates prior to “12/30/2011.”
Sub DeleteRowbyDate() Dim x As Long For x = 1 To Cells.SpecialCells(xlCellTypeLastCell).Row Debug.Print Cells(x, "B").Value If CDate(Cells(x, "B")) <CDate("12/29/2011") Then Cells(i, "B").EntireRow.delete End If Next i End Sub
In this example all rows with dates less than “12/29/2011” will be deleted.For example, If you want to delete all the rows prior to December 29 2012 from your table, then you will have to change the line to:
If CDate(Cells(x, "B")) <CDate("12/29/12")
We hope this gives you a good understanding of the wide range of options available to you while working with dates in Excel and with VBA. As usual, the best way to get familiar with these formula, is to try them out for yourself. If you’d like, Mr Excel has a good course with plenty of examples to help you on your way!
Top courses in Excel VBA
Excel VBA students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.