0

How would I incorporate "on error" into my code so that if a user enters something into a textbox that is not a date formatted to MM/DD/YYYY or left blank it will not exit out of my userform. Other solutions are appreciated as well!

The code:

Private Sub CommandButton1_Click()
    Dim s As String
    s = Me.TextBoxx25.Text
    
    If s = "" Then
    ElseIf DateDiff("d", Me.TextBoxx25.Text, "11/12/2020") > 0 Then
        Doc1.Bookmarks("bmrow1").Range.Cells.Delete
        Doc2.Variables("TextBoxx25").Value = Me.TextBoxx25.Text
        Doc2.Variables("TextBoxx1").Value = Me.TextBoxx1.Text
        Doc2.Variables("TextBoxx7").Value = Me.TextBoxx7.Text
        Doc2.Variables("TextBoxx13").Value = Me.TextBoxx13.Text
        Doc2.Variables("TextBoxx19").Value = Me.TextBoxx19.Text
        Doc2.Variables("TextBoxx31").Value = Me.TextBoxx31.Text
    Else
        Doc1.Variables("TextBoxx25").Value = Me.TextBoxx25.Text
        Doc1.Variables("TextBoxx1").Value = Me.TextBoxx1.Text
        Doc1.Variables("TextBoxx7").Value = Me.TextBoxx7.Text
        Doc1.Variables("TextBoxx13").Value = Me.TextBoxx13.Text
        Doc1.Variables("TextBoxx19").Value = Me.TextBoxx19.Text
        Doc1.Variables("TextBoxx31").Value = Me.TextBoxx31.Text
        Doc2.Bookmarks("bmrow1").Range.Cells.Delete
    End If

It would have to display a msgbox that says please format as (MM/DD/YYYY)

braX
  • 11,506
  • 5
  • 20
  • 33
mr-walrus
  • 31
  • 3
  • [`IsDate`](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/isdate-function) might be a good start. – BigBen Aug 10 '21 at 20:49
  • 1
    Or just use a calendar input to begin with? https://stackoverflow.com/questions/54650417/how-can-i-create-a-calendar-input-in-vba-excel – BigBen Aug 10 '21 at 20:51
  • Look up code to validate data. *(There is lots out there.)* This is not really an error, as such, but rather a failure by you user to follow instructions. (Generally, in coding, an error is code that throws an error message.) – Charles Kenyon Aug 11 '21 at 19:13

0 Answers0