I'm trying to use multiple DTPickers within a form. When the user clicks on the submit button, I would like the form to copy the end time, and when the form refreshes, put the end time as the new start time.
I've declared the variable LastTime as a public variable as I would like to reset it when the workbook is opened. To accomplish that, here's my current code in ThisWorkbook under general declarations:
Option Explicit
Public LastTime As String
Private Sub Workbook_Open()
LastTime = "12:00:00 AM" 'Reset variable to default time
frmTimeInput.Show 'Open Form when workbook opens
End Sub
Near the end of my code for the submit button, I have a line of code that sets the LastTime variable to the value of the end time DTPicker (in my code it's DTPicker3, and the start time is DTPicker2). Since it's a public variable, I do not have it as a declared variable within the code for the submit button. After this line of code, I'm using Unload Me to reset the form, followed by frmTimeInput.Show to redisplay the form. To try and use the variable to update the time, I'm trying this code, but it doesn't seem to be working. When I debug it, it shows LastTime as empty within the code below, so not sure why it's not grabbing the value from the public variable as during debugging I can see the LastTime variable is set to 12 AM in the workbook open sub.
Private Sub UserForm_Activate()
DTPicker2.Value = LastTime
End Sub
I'm not sure if the problem is in my variables, or just in how I'm trying to set values during the activation of the form.
I've declared the variable LastTime as a public variable as I would like to reset it when the workbook is opened. To accomplish that, here's my current code in ThisWorkbook under general declarations:
Option Explicit
Public LastTime As String
Private Sub Workbook_Open()
LastTime = "12:00:00 AM" 'Reset variable to default time
frmTimeInput.Show 'Open Form when workbook opens
End Sub
Near the end of my code for the submit button, I have a line of code that sets the LastTime variable to the value of the end time DTPicker (in my code it's DTPicker3, and the start time is DTPicker2). Since it's a public variable, I do not have it as a declared variable within the code for the submit button. After this line of code, I'm using Unload Me to reset the form, followed by frmTimeInput.Show to redisplay the form. To try and use the variable to update the time, I'm trying this code, but it doesn't seem to be working. When I debug it, it shows LastTime as empty within the code below, so not sure why it's not grabbing the value from the public variable as during debugging I can see the LastTime variable is set to 12 AM in the workbook open sub.
Private Sub UserForm_Activate()
DTPicker2.Value = LastTime
End Sub
I'm not sure if the problem is in my variables, or just in how I'm trying to set values during the activation of the form.