Userform Textbox

Nine Zero

Well-known Member
Joined
Mar 10, 2016
Messages
625
Hello,

Im wondering why my .SetFocus is not working properly

In the code below i have if the user does not input a valid date, clear the box then i want to setfocus back into that textbox, but what it does is it DOES clear out the textbox but then it just jumps ahead to my next Tab Index ...any ideas

Edit: could it be that my first If statement is exiting the sub after clearing it?

Code:
Private Sub startdatebox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If startdatebox = vbEmptyString Then Exit Sub
If IsDate(startdatebox) Then
    startdatebox = Format(startdatebox, "dd/mm/yyyy")
Else
    MsgBox "Please enter a valid date!" & vbNewLine & vbNewLine & "Remember you must have the forward slashes in your date." _
    & vbNewLine & vbNewLine & "Example: 01/01/2018", vbCritical
    startdatebox.Value = ""
    startdatebox.SetFocus
End If
End Sub
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Don't mind at all :) . I *think* why setfocus don't work, but cancel does is because you're doing it on the exit event (after you've clicked or tabbed into a different textbox...at least that's how I tested it)...and if that code would set the focus back to itself, it wouldn't really have "exited" at all. That's my best guess on that part.

The Cancel=True works because you're cancelling the trigger event (the exit) so it doesn't complete that event...therefore, it stays focused on that textbox.
 
Last edited:
Upvote 0
Don't mind at all :) . I *think* why setfocus don't work, but cancel does is because you're doing it on the exit event (after you've clicked or tabbed into a different textbox...at least that's how I tested it)...and if that code would set the focus back to itself, it wouldn't really have "exited" at all. That's my best guess on that part.

The Cancel=True works because you're cancelling the trigger event (the exit) so it doesn't complete that event...therefore, it stays focused on that textbox.

Ahhhh i see so its like if you were to have an event being ready to trigger like "Are you sure you want to do this?" then you have an OK and a CANCEL button ... if you hit cancel you are just sent back to the previous location which in this case would be the textbox.

Very cool.

Thanks again!
 
Upvote 0
That would just cancel whatever you're asking about with the msgbox, but depending on how you coded it, it might leave the focus on the textbox also...not sure.

I just had another thought about your code above....let me do some playing :) ...might have a better explanation why setfocus didn't work.

EDIT: Nevermind, it wasn't what I was thinking it might be. My best guess remains unchanged :cool:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top