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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,215,223
Messages
6,123,714
Members
449,118
Latest member
MichealRed

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