Clear text in textbox upon clicking the textbox

hanikas

New Member
Joined
Jun 18, 2015
Messages
17
I'm trying to create a text box that, when clicked, deletes the contents that I have put in it. For example, I have a text box that says "HH:MM". As it suggest, this textbox would be for users to enter the time. When it is clicked, I want my text to disappear so they can enter what they want. Thank you
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm trying to create a text box that, when clicked, deletes the contents that I have put in it. For example, I have a text box that says "HH:MM". As it suggest, this textbox would be for users to enter the time. When it is clicked, I want my text to disappear so they can enter what they want. Thank you
Where is the TextBox located... on a UserForm or on a Worksheet? If on a Worksheet, what kind of TextBox is it... an ActiveX or Forms (from the Insert tab, Text panel, Text Box button) one?
 
Upvote 0
The textbox is on a UserForm
I am not sure of your complete, intended interface for the UserForm, but for the question you asked, as you asked it, just set the Text property of the Text to the empty string in the MouseDown event...
Code:
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
                               ByVal X As Single, ByVal Y As Single)
  TextBox1.Text = ""
End Sub
 
Upvote 0
You could do something like this
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    With Me.TextBox1
        If .Text = "HH:MM" Then
            .Text = vbNullString
            .ForeColor = RGB(0, 0, 0)
        End If
    End With
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With Me.TextBox1
        If .Text = vbNullString Then
            .Text = "HH:MM"
            .ForeColor = RGB(130, 130, 130)
        End If
    End With
End Sub

Private Sub UserForm_Initialize()
    Rem can be set a design time
    TextBox1.Text = "HH:MM"
    TextBox1.ForeColor = RGB(130, 130, 130)
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,667
Messages
6,056,651
Members
444,880
Latest member
Kinger1968

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
Back
Top