ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Highlight text in Textbox (https://www.excelbanter.com/excel-programming/403535-highlight-text-textbox.html)

jnf40

Highlight text in Textbox
 
How do you get the text in a textbox to be highlighted when the userform
activates?

Rick Rothstein \(MVP - VB\)

Highlight text in Textbox
 
Normally, you would put this code...

TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
TextBox1.SetFocus

in the UserForm's Activate event. HOWEVER, there seems to be a problem if
TextBox1 was the last control to have had focus when the UserForm was
deactivated... for some reason, the text will not be highlighted under these
conditions when you return to the UserForm. So, either make sure some other
control has focus when the UserForm is deactivated or simply use this code
in place of the above code...

SendKeys "{TAB}+{TAB}"
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)

Of course, this requires at least one other control capable of taking focus
to be on the UserForm.

Rick


"jnf40" wrote in message
...
How do you get the text in a textbox to be highlighted when the userform
activates?



Don[_30_]

Highlight text in Textbox
 
On Jan 2, 2:58*pm, jnf40 wrote:
How do you get the text in a textbox to be highlighted when the userform
activates?


Set the index number of that text box to zero so it is the first
active object when the userform loads.

jnf40

Highlight text in Textbox
 
Rick, Don
Thank you both for your replies, works great.

"Don" wrote:

On Jan 2, 2:58 pm, jnf40 wrote:
How do you get the text in a textbox to be highlighted when the userform
activates?


Set the index number of that text box to zero so it is the first
active object when the userform loads.



All times are GMT +1. The time now is 09:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com