In my Form, I have a text box where I enter a 'CompletionDate' Is there a way to ensure…
- User
In my Form, I have a text box where I enter a 'CompletionDate' Is there a way to ensure that whichever way I enter the date it will always appear in the following format dd/mm/yy
Option Explicit
Private Sub CommandButton1_Click() Application.EnableEvents = False ' Get data from form Dim TaskDescription As String: TaskDescription = Me.TaskDescription.value Dim TaskType As Long: TaskType = Me.TaskType.ListIndex + 1 ' Adjust for 0-based indexing Dim DateCreated As Date: DateCreated = Me.DateCreated.value Dim Collaborators As Long: Collaborators = Me.Collaborators.ListIndex + 1 ' Adjust for 0-based indexing Dim Resources As Long: Resources = Me.Resources.ListIndex + 1 ' Adjust for 0-based indexing Dim TaskPriority As Long: TaskPriority = Me.TaskPriority.ListIndex + 1 ' Adjust for 0-based indexing Dim CompletionDate As Date: CompletionDate = Me.CompletionDate.value Dim StagesRequired As Long: StagesRequired = Me.StagesRequired.ListIndex + 1 ' Adjust for 0-based indexing
' Calculate difference in days Dim diffDays As Long: diffDays = DateDiff("d", Date, CompletionDate)
' Determine target sheet based on difference in days Dim targetSheet As String Select Case diffDays Case Is <= 3: targetSheet = "Day" Case 4 To 9: targetSheet = "Week" Case 10 To 20: targetSheet = "Forthnight" Case 21 To 59: targetSheet = "Month" Case 60 To 89: targetSheet = "QtrYear" Case 90 To 209: targetSheet = "HalfYear" Case Else: targetSheet = "Year" End Select ' Find next available row Dim lastRow As Long: lastRow = Sheets(targetSheet).Cells(Rows.Count, 1).End(xlUp).Row + 1 'Application.Wait (Now + TimeValue("0:00:01")) ' Write data to sheet With Sheets(targetSheet) Sheets(targetSheet).Unprotect Password:="edit" ' Unlock the sheet .Cells(lastRow, 1).value = TaskDescription .Cells(lastRow, 2).value = Me.TaskType.value .Cells(lastRow, 3).value = DateCreated .Cells(lastRow, 4).value = Me.Collaborators.value .Cells(lastRow, 5).value = Me.Resources.value .Cells(lastRow, 6).value = Me.TaskPriority.value .Cells(lastRow, 7).value = CompletionDate .Cells(lastRow, 8).value = Me.StagesRequired.value Sheets(targetSheet).Protect Password:="edit" ' Unlock the sheet End With 'Application.Wait (Now + TimeValue("0:00:01")) ' Display confirmation message MsgBox "Your new task has been recorded in " & targetSheet & ".", vbInformation
' Clear form Me.TaskDescription.value = "" Me.TaskType.ListIndex = -1 ' Set to no selection Me.DateCreated.value = "" Me.Collaborators.ListIndex = -1 Me.Resources.ListIndex = -1 Me.TaskPriority.ListIndex = -1 Me.CompletionDate.value = "" Me.StagesRequired.ListIndex = -1 Unload Me ' Unload the form Application.EnableEvents = True End Sub
Private Sub CommandButton2_Click() CalendarForm.Show ' Open the CalendarForm End Sub
Private Sub UserForm_Initialize() ' Run when the form opens Me.DateCreated.value = Format(Date, "dd/mm/yy") ' Set to current date directly End Sub
- Assistant
To ensure that the 'CompletionDate' appears in the dd/mm/yy format regardless of how it is entered, you can add a validation check before storing the value.
You can add the following code snippet before assigning the 'CompletionDate' value:
' Validate and format CompletionDate Dim strCompletionDate As String: strCompletionDate = Me.CompletionDate.value If IsDate(strCompletionDate) Then CompletionDate = Format(CDate(strCompletionDate), "dd/mm/yy") Else MsgBox "Please enter a valid date in dd/mm/yy format.", vbExclamation Exit Sub End IfThis code snippet checks if the entered value in the 'CompletionDate' text box is a valid date. If it is a valid date, it formats it to the dd/mm/yy format using the
Formatfunction. If it is not a valid date, it displays a message asking the user to enter a valid date in the specified format.You can replace the existing line
Dim CompletionDate As Date: CompletionDate = Me.CompletionDate.valuewith the updated code snippet to ensure that the 'CompletionDate' is in the dd/mm/yy format.