Home » excel » excel – VBA Userform textbox default value and highlighted

excel – VBA Userform textbox default value and highlighted

Posted by: admin March 9, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

Set the EnterFieldBehavior property to frmEnterFieldBehaviorSelectAll so that when the focus goes to that field, the whole field is selected.

Answer:

how about this line of code when your form loads?

NameTextBox.SelectAll()