Remove Text in Textbox on Keypress Event

In Distress

New Member
Joined
Mar 12, 2003
Messages
37
Howdy all,

I've got another amateur question for you all.

I have a textbox on a userform called Screen001_TB_EmployeeNameEntry that has a default value of "Enter your name here". My question (and probably the solution) is simple, on the Screen001_TB_EmployeeNameEntry_Enter event, I have the text in the textbox highlighted. Now, when a user starts to enter their name, I want the default text to be deleted and the name the user enters to be placed in the textbox. I'm gathering it can be done by using the KeyPress event but I'm not sure how.

Now, I hear you thinking - if the text is highlighted then any key pressed will delete the highlighted text and replace it with the key pressed, but my problem is, what if someone simply places the cursor half way through the default text and starts typing..........instant disaster. Hence the reason I want the default value to disappear regardless of wether or not the default text is highlighted or not.

Hopefully that all makes sense.

Thanks in advance again.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

In Distress

New Member
Joined
Mar 12, 2003
Messages
37
It's all good folks - managed to figure it out. For those interested here's how I fixed it.

Code:
Private Sub Screen001_TB_EmployeeNameEntry_Keypress(ByVal KeyAscii as MSForms.ReturnInteger)

If Screen001_TB_EmployeeNameEntry.value = "Enter your name here" then
Screen001_TB_EmployeeNameEntry.selstart = 0
Screen001_TB_EmployeeNameEntry.selLength = 150
End if

End sub
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
Hi In Distress,
Give this a shot.
Code:
Private Sub Screen001_TB_EmployeeNameEntry_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Screen001_TB_EmployeeNameEntry.Text = "Enter your name here" Then _
  Screen001_TB_EmployeeNameEntry.Text = ""
End Sub
 

Forum statistics

Threads
1,136,926
Messages
5,678,604
Members
419,774
Latest member
MooseWinooski

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
Top