I have a vba form that has a field where I enter the Expected CompletionDate. In some…
- User
I have a vba form that has a field where I enter the Expected CompletionDate. In some forms I have used a tab that will reval a calender and then from the revealed calender, I can click the date I desire and the date value is entered into the required field. How is this done. I have also included the Form code below.
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 UserForm_Initialize() ' Run when the form opens Me.DateCreated.Value = Format(Date, "dd/mm/yy") ' Set to current date directly End Sub
- Assistant
To add a calendar to your VBA form for selecting the completion date, you can use the DTPicker control provided by Microsoft. Here's how you can add a DTPicker control to your form and use it to select the completion date:
- Open your UserForm in design mode.
- Go to the "Toolbox" in the VBA editor and find the "Microsoft Date and Time Picker Control" (mscomct2.ocx). If you don't see it, you may need to add it by right-clicking on the Toolbox and selecting "Additional Controls."
- Drag and drop the Date and Time Picker control onto your UserForm.
- Resize and position the DTPicker control as desired on your form.
- Double-click on the DTPicker control to open the code window.
- Use the following code to set the value of the CompletionDate field when a date is selected:
Private Sub DTPicker1_Change() Me.CompletionDate.Value = DTPicker1.Value End Sub- Now, when the user selects a date from the DTPicker control, it will automatically update the CompletionDate field on your form.
- User
I could not find "Microsoft Date and Time Picker Control" in the Additional Controls
- Assistant
If you are unable to find the "Microsoft Date and Time Picker Control" in the Additional Controls list, you can use an alternative approach to add a calendar control to your VBA form. One common alternative is to create a calendar form that pops up when needed and allows the user to select a date.
Here's a simplified example of how you can create a calendar form in VBA:
- Create a new UserForm in your VBA editor (Right-click on VBAProject (Your workbook name) > Insert > UserForm).
- Add a MonthView control to the UserForm. You can find it under the "Additional Controls" in the Toolbox.
- Add an "OK" button to the UserForm for the user to confirm the selected date.
- Add a TextBox on your main form to display the selected date.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.