Search code examples
excelvbatextboxuserform

VBA Userform textbox default value and highlighted


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?


Solution

  • 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