VBA autocomplete date in userform

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
Office Version
  1. 2016
Platform
  1. Windows
I have an Excel userform in which are Textboxes where various dates have to be entered.

I am looking for VBA which will autocomplete dates that are partially entered:

- If I put 02/10 instead of 02/10/2022, I'd like the year to be added automatically
- Or if I put 2/10/2022 I'd like the first 0 to be put automatically.
- Or if I put 021022 then the result will be 02/10/2022.

Thanks for any help.
 
In that case, I can just change the variable 'mdy' to 'dmy' and specify the output format as "dd/mm/yyyy" instead of "mm/dd/yyyy".

VBA Code:
Public Function Format_Date_EUR(inputDate As String, outputDate As String) As Boolean

    Dim parts As Variant
    Dim dmy As String
    
    Format_Date_EUR = True 'assume valid input date
    parts = Split(inputDate, "/")
    Select Case UBound(parts)
        Case Is = 0
            If Len(inputDate) = 6 Then
                dmy = Mid(inputDate, 1, 2) & "/" & Mid(inputDate, 3, 2) & "/" & Mid(inputDate, 5, 2)
                If IsDate(dmy) Then
                    outputDate = Format(CDate(dmy), "dd/mm/yyyy")
                Else
                    outputDate = "Error: '" & inputDate & "' invalid date"
                    Format_Date_EUR = False
                End If
            ElseIf Len(inputDate) = 8 Then
                dmy = Mid(inputDate, 1, 2) & "/" & Mid(inputDate, 3, 2) & "/" & Mid(inputDate, 5, 4)
                If IsDate(dmy) Then
                    outputDate = Format(CDate(dmy), "dd/mm/yyyy")
                Else
                    outputDate = "Error: '" & inputDate & "' invalid date"
                    Format_Date_EUR = False
                End If
            Else
                outputDate = "Error: '" & inputDate & "' unknown date format"
                Format_Date_EUR = False
            End If
        Case Is = 1
            dmy = parts(0) & "/" & parts(1) & "/" & Year(Date)
            If IsDate(dmy) Then
                outputDate = Format(CDate(dmy), "dd/mm/yyyy")
            Else
                outputDate = "Error: '" & inputDate & "' invalid date"
                Format_Date_EUR = False
            End If
        Case Is = 2
            dmy = parts(0) & "/" & parts(1) & "/" & parts(2)
            If IsDate(dmy) Then
                outputDate = Format(CDate(dmy), "dd/mm/yyyy")
            Else
                outputDate = "Error: '" & inputDate & "' invalid date"
                Format_Date_EUR = False
            End If
        Case Else
            outputDate = "Error: '" & inputDate & "' unknown date format"
            Format_Date_EUR = False
    End Select
    
End Function
 
Upvote 0
Solution

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,214,863
Messages
6,121,978
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top