VBA Function: DateValue

The VBA function DateValue converts a string to a date, accepting different date formats.

Usage:

DateValue(text)


Example of Usage

Using the DateValue function to convert different strings to dates:

Sub example()

    MsgBox DateValue("10/30/2024") 'Returns: 10/30/2024
    MsgBox DateValue("10/30/24") 'Returns: 10/30/2024
    MsgBox DateValue("2024-10-30") 'Returns: 10/30/2024
    MsgBox DateValue("10 30 2024") 'Returns: 10/30/2024
    MsgBox DateValue("30 October 2024") 'Returns: 10/30/2024
    MsgBox DateValue("October 30, 2024") 'Returns: 10/30/2024
    MsgBox DateValue("Oct-30-2024") 'Returns: 10/30/2024
    MsgBox DateValue("10/30/2024 21:30:00") 'Returns: 10/30/2024

End Sub

In this example, all the strings return the same date.