VBA Convert String to Date

vba Convert String to Date

VBA convert string to date, 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 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 use

You can follow step by step instructions to run above macro example VBA macro code.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro code or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • Now you can see the output on the system screen.

Other Related Articles Link

Click on the following links for related reference articles. It helps for your reference.

VBA CDate Function VBA Format Function VBA Functions List

Leave a Comment