Search code examples
excelvbauserform

How to force textbox exit event when closing a userform


I have a userform with input textboxes. I need to force validation on the textboxes, which I use the Exit event to do. The issue I have is that someone can enter data into one of the textboxes and then click my Close button without moving to another field. This bypasses the exit event. Here is a sample textbox exit event.

Private Sub tbRate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'Do validation stuff
    'Save value to worksheet
End Sub

My idea was to call the exit events of the active textbox as part of the code in the close button. The issue is what to put in the call statement.

Call tbRate_Exit(False) yields a type mismatch error. What should I use? Or is there a better method?


Solution

  • Instead of trying to call the Exit event manually, put the code for the event into its own routine, and then call that routine from the Exit event and from the other place.