Date Data Type in Excel VBA explained with syntax and examples. The Date VBA date type stores floating point numbers that represents date and time. We can store date, time or date and time. It has date range values from Jan 1, 0100 to December 31, 9999 and time values from 0:00:00 to 23:59:59. Date VBA data type doesn’t have literal type character and identifier type character, but it is enclosed with ‘#’. It occupies 8 bytes(64 bits) in memory.
Let us see Data Type Date Syntax in Excel VBA.
Dim VariableName as Date
Where VariableName represents the name of the variable.
and Date represents the type of data.
Here is an example on the Date VBA Data Type in Excel.
Sub VBAF1_Date_DataType() 'Integer Variable Declaration Dim dDate As Date Dim tTime As Date Dim dtDateTime As Date 'Assign value to variable dDate = #1/1/2019# tTime = #1:01:01 AM# dtDateTime = #1/1/2019 1:01:01 AM# MsgBox "Date: " & dDate & vbCrLf & "Time: " & tTime & vbCrLf & "DateTime: " & dtDateTime, vbInformation, "VBAF1" End Sub
Here is the output screenshot of above macro.
You can convert an expression to Date VBA data type using VBA CDate function. Click on the following link to learn about CDate function and complete tutorial and examples.
VBA Data Types
Also read about all other data types.