I have text in a textbox, which says "NEW NAME HERE"
Is it possible with in VBA to clear this text as soon as the user clicks in that textbox?
If so could anyone please let me know how?
THANKS-
Clear() or TextBox. Text = string. Empty.
You may use JavaScript form reset() method or loop throw all textboxes and set Text property to empty string. Save this answer.
Use Clear to explicitly clear the Err object after an error has been handled, for example, when you use deferred error handling with On Error Resume Next. The Clear method is called automatically whenever any of the following statements is executed: Any type of Resume statement. Exit Sub, Exit Function, Exit Property.
Assuming the TextBox is named TextBox1
, the following should work
Private Sub TextBox1_GotFocus()
TextBox1.Text = ""
End Sub
I know it's an old post, but I just stumbled upon it, and thought I'd add an answer.
Maybe someone will find it useful some day :-)
I would check the current value before clearing the content. That way it clears the content when you first click it, but if you click it again later (to edit the name perhaps) it does nothing, allowing edits without having to start over.
Assuming the TextBox is named TextBox1
and the placeholder text is NEW NAME HERE
, the following should work
Private Sub TextBox1_Enter()
If TextBox1.Text = "NEW NAME HERE" Then
TextBox1.Text = ""
End If
End Sub
To restore the placeholder text if the textbox is left empty:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(TextBox1.Text) = "" Then
TextBox1.Text = "NEW NAME HERE"
End If
End Sub
Tested in Word 2010 VBA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With