0
votes

I am using the following code (in Excel 2013 Windows 8.1):

Private Sub txtDateBox_exit(ByVal cancel As msforms.ReturnBoolean)
    If IsDate(txtDateBox.Value) = False Then
        MsgBox "Enter a Valid Date, such as Jan 23, 2015"
        txtDateBox.SetFocus
   End If
End Sub

If I enter an invalid date msgbox pops up but when the sub is exited the cursor has moved to the next textbox on my userform. How can I get the cursor to believe me that I want it to go back to the txtDateBox for the user to enter a valid date? The codes work fine, its just the cursor that I'm having trouble with.

Same thing happens when the user puts invalid data into a 'bin' text box i.e. the cursor jumps to the next textbox (when the function returns a "no") instead of where I ask the cursor to 'SetFocus".

Private Sub txtbin1_exit(ByVal cancel As msforms.ReturnBoolean)
    IsItABin (val(txtbin1))
    If binNumFound = "no" Then
         MsgBox "Bin not found, please re-enter"
        txtbin1.SetFocus
    End If
End Sub

Thanks in advance for any help.

1

1 Answers

1
votes

If you write in Cancel = True, it prevents the cursor from leaving the textbox