VBA Convert String To Date

VBA Convert String to Date

VBA convert string to date in Excel, we use VBA CDate data type conversion function. In the following article let us see an example and also the step by step instructions to run vba code in the visual basic editor(VBE) window. You can find link at the end of the session to know more about CDate VBA function.

Example macro code to convert string to date

Let us see the example VBA macro procedure code to convert string to date. Declared two variables. The first one is sInput to assign input string type value and the second one is iResult. In the below example we have used CDate function. Ensure to enter specified input in string format.

'VBA Convert String type to Date type value
Sub VBAF1_Convert_String_To_Date()

    'Variable Declaration
    Dim sInput As String, iResult As Date
    'Specify Input of type string
    sInput = "01/01/2019"
    'Convert String to Date
    sResult = CDate(sInput)
    MsgBox "Successfully string has converted to date.", vbInformation, "VBAF1"
End Sub

Output: Here is the output screenshot message of above example macro code for your reference.
VBA Convert String to Date

Multiple Example Macro Codes for String to Date Conversion

Here are the different example macro codes for string to date conversion. When we writing programming, we write code based on requirement. Date has different formats. Depending on the scenario we use required format. Let us see some examples here for your reference.

Note: Specified input (Sample Input Date) should be in string format.

Example 1: String To Date format dd-mmm-yyyy conversion in VBA

Format(CDate(Sample Input Date), "dd-mmm-yyyy")

Example 2: VBA String To Date format dd/mm/yyyy conversion

Format(CDate(Sample Input Date), "dd/mm/yyyy")

Example 3: String To Date format yyyymmdd conversion in VBA

Format(CDate(Sample Input Date), "yyyymmdd")

Example 4: VBA String To Date Time conversion

Format(CDate(Sample Input Date), "dd-mmm-yyyy hh:mm:ss")

Example 5: String To Date number format conversion in VBA


Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays in Excel VBA Tables and ListObjects

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment