I am doing VBA coding for the first time as of yesterday. I having some issues. I am wanting to add a user form that opens when the workbook is opened and prompts the user to input a date and then selects something from a drop down list. When they have done this and click "okay" i want the date to be checked to ensure it is in the proper format. If it is not i want it to re-prompt the user with a notification of their error without populating any fields. If the date field is in the correct format however i want the date and the selected text from the drop down to populate into two cells on the sheet. Following successful population of those fields i want the user form to close automatically. The code i have so far doesn't work. If the user inputs anything but a date into the date field it goes into an infinite failure that doesn't allow the user to reenter a date to correct the issue. I end up having to close excel with the command prompt.
Please help!
Private Sub CommandButton1_Click()
Dim dateString As String, TheDate As Date
Dim valid As Boolean: valid = True
Do
dateString = TextBox1.Text
If IsDate(dateString) Then
TheDate = DateValue(dateString)
valid = True
Worksheets("Time Sheet").Range("G3").Value = ComboBox1.Text
Worksheets("Time Sheet").Range("X3").Value = TextBox1.Text
Else
MsgBox "Invalid date"
valid = False
End If
If valid = True Then
Me.Hide
Loop Until valid = True
End If
End Sub
Private Sub UserForm_Initialize()
With ComboBox1
Me.ComboBox1.List = Worksheets("Sheet1").Range("A1:A10").Value
End With
End Sub
Bookmarks