Reputation: 3315
I have two Excel picture objects linked to different forms, each with a text box and OK/Cancel buttons. In one form, the text cursor is in the textbox when clicking the object which is what I want:
but in another it selects the OK command button rather than have the text cursor in the textbox:
I've gone through the form and textbox/command button properties and see nothing about selection, and the 'correct' macro properties appear to be the same as the 'incorrect' one.
What do I do to change the form such that when it's opened the text cursor goes to the textbox instead of the command button being selected?
Upvotes: 5
Views: 47855
Reputation: 53623
Before the form is displayed, you can do soemthing like:
TextBox1.SetFocus
Obviously, replace "TextBox1" with whatever the name of your textbox object is.
This should go in whatever event or macro causes the form to .Show
, immediately before the .Show
statement.
Upvotes: 1
Reputation: 19067
Quite easy solution is to change TabIndex Property
to 0.
So, 1) go to VBA Editor, 2) select your TextBox in your UserForm and 3) change TabIndex
Property in Property window as presented below.
Upvotes: 12
Reputation: 3585
Add an event to the form so that when it is initialized it selects the correct texbox.
Private Sub UserForm_Initialize()
TextBox2.SetFocus
End Sub
Upvotes: 4