Auto highlight the text in a TextBox on a userform

ttt123

Board Regular
Joined
May 31, 2006
Messages
120
Office Version
  1. 365
Platform
  1. Windows
When I have a userform displayed, I want a specific textbox to be activated in that userform, I want its default value to be "yyyy/mm/dd" and I want that default value to be highlighted so that the user can just type over it without having to delete the default value.

Right now, I can get the default value fine by changing the .Value property of the textbox, I can get it to be auto-selected by switching it to the first tab index, but what I can't figure out how to do is to auto-highlight the text in the textbox. When I press the tab button to move to the next textbox, all the text in that box is automatically highlighted but I just can't figure out how to do it on the first textbox.

Any help would be greatly appreciated,
Thanks,
Taylour
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Code:
Private Sub TextBox1_Enter()
    With Me.TextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub

Private Sub UserForm_Initialize()
    With Me
        .TextBox1 = "abcd"
        .TextBox2 = "efgh"
        .TextBox3 = "wxyz"
    End With
End Sub
 
Upvote 0
Try:

Code:
Private Sub UserForm_Initialize()
    With TextBox1
        .Text = "yyyy/mm/dd"
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,991
Messages
6,175,820
Members
452,672
Latest member
missbanana

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