VBA Userform textbox default value and highlighted

blahblahblah picture blahblahblah · Jan 15, 2014 · Viewed 63.7k times · Source

In my UserForm, I want to set a default value for my TextBox, that will highlight when focused upon.

Private Sub UserForm_Initialize()

NameTextBox.Value = "Your Name Here"

NameTextBox.SetFocus

End Sub

When this code runs, the cursor should set at the end of the default text, i.e. after "...Here". I want "Your Name Here" to be highlighted so that when the form is generated, the user can start replacing that default/placeholder text.

Can you help me write the code to set default values that are editable?

Answer

Doug Glancy picture Doug Glancy · Jan 15, 2014

This will select all the text in the TextBox:

Private Sub UserForm_Initialize()
With Me.NameTextBox
    .Value = "Your Name Here"
    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
End With
End Sub