Vba cdate function in Excel

The Cdate function Is used to convert a string representation of date into date format.

The date expession must be in any of the excel supported date format, which can be seen from the "Format Cells" dialog box.

The Cdate function has two arguments, and the syntax is as shown below.

Syntax

Format(Cdate(expression),)

The expression is the string containing the date expression

The specifies how to display the date.There are various date formats, some of which are discussed below.

"Long Date", "Short Date" and custome expressions such as "YYYY-DD-MM","MM-DD-YY" are some of the most commonly used.

The Cdate function is very important as most of the date information obtained extrenally from the users are of plain text and

for the excel to identify this as date, it has to be converted.

The following example illustrates this

Sub cdate_demo()

MsgBox "Long Date " & vbNewLine & Format(CDate("2017/4/23"), "Long Date")

MsgBox "Short Date " & vbNewLine & Format(CDate("2017/4/23"), "Short Date")

MsgBox "Custom Date Format " & vbNewLine & Format(CDate("2017/4/23"), "yyyy-mm-dd")

End Sub

The screenshot of the editor is as shown below

excel vba cdate function

Output

excel vba cdate function

excel vba cdate function

excel vba cdate function

 

You can find similar Excel Questions and Answer hereunder

1) What is a module in VBA. How to create a module in VBA

2) Various important math functions in VBA (trigonometry, algebra, exponential, �)

3) How to format variable with VBA with the very useful format function. Define the number of decimal, the way a date is formated

4) How to use logical operators in VBA

5) How to modify strings in VBA, what are the functions you can manipulate strings with

6) How can worksheet functions be accessed in VBA?

7) Here we show you advanced VBA array functions, like splitting arrays, Lbound, Ubound

8) What are the various functions of the worksheet in Excel VBA

9) What are the date and time function in Excel VBA

10) What is variable scope in VBA, how do variable can be reach from other function or routines in VBA

 

Here the previous and next chapter