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.
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,958
Office Version
  1. 365
Platform
  1. Windows
Hi, @MK64 Welcome to the Forum:
Try:
VBA Code:
Private Sub UserForm_Activate()
txtTextBox.SetFocus

End Sub
 
Solution

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,343
Initialize event occurs before the form is loaded. Instead of Initialize, use Activate or Layout events.
 

MK64

New Member
Joined
Jan 31, 2021
Messages
7
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 :)
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,958
Office Version
  1. 365
Platform
  1. Windows
You're welcome, glad to help & thanks for the feedback. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,093
Messages
5,622,664
Members
415,917
Latest member
kungsleden

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
Top