How do I set focus (and select text) on a text box on a modeless userform?

MK64

New Member
Joined
Jan 31, 2021
Messages
7
I have a userform which I have had to make modeless so that I can access and use other excel workbooks while the form is active. Before changing it to modeless (i.e. it was initially the default modal) I could set the focus on a textbox and select the default text to highlight it. However, once I made it modeless the text is no longer highlighted.

I've searched the internet high and low and I cannot find a solution to this, even though the problem seems to have existed for 10 years or more! I have found a few forums where the same question was asked, but nowhere have I found a solution, hence why I am asking again in case anyone has managed to find a solution since then.

The userform is called from a command button on the speadsheet, and the setfocus is done in the UserForm_Initialise event. This is the code, which is the standard way of setting focus:

With txtTextBox
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With

Like I said, this worked fine while the userform was modal, but it doesn't work when the userform is modeless. And again, I know this question has been asked many times on many forums, but I have yet to find a solution for it. Any help would be appreciated.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi, @MK64 Welcome to the Forum:
Try:
VBA Code:
Private Sub UserForm_Activate()
txtTextBox.SetFocus

End Sub
 
Upvote 0
Solution
Initialize event occurs before the form is loaded. Instead of Initialize, use Activate or Layout events.
 
Upvote 0
Thank you both, I have put it in the Activate event and it worked. The strange thing is, I'd already tried that as I'd read it elsewhere, but it didn't work, so really can't understand why it worked this time. Anyway, thank you for 'forcing' me to do it again :)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback. :)
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

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