Highlight Textbox text when user clicks inside the textbox

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
I got the below code from another thread but it only works when you click the textbox, not the actual text:

<font face=Calibri><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> HighPercent_Enter()<br>  <br>    <SPAN style="color:#00007F">With</SPAN> HighPercent<br>        .SetFocus<br>        .SelStart = 0<br>        .SelLength = Len(HighPercent.Text)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
When I use the MouseDown event I get the following compile error: "Procedure declaration does not match description of event or procedure having the same name"

<font face=Calibri><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> HighPercent_MouseDown()<br>  <br>    <SPAN style="color:#00007F">With</SPAN> HighPercent<br>        .SetFocus<br>        .SelStart = 0<br>        .SelLength = Len(HighPercent.Text)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Select the event from the right hand dropdown above the code window and you will get:

Code:
Private Sub HighPercent_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

End Sub
 
Upvote 0
Are there other events that you have to pull down from the dropdown? I've never had to do that with any others that I've used. Also, is there a way to cover 4 individual textboxes with this code, or do I need to make a separate MouseDown event for each textbox? Thanks for the help!
 
Upvote 0
It's always best to select from the dropdown. Then you will never get the error you saw.

You could use a Class module to apply the same code to multiple TextBoxes.
 
Upvote 0
Good advice, I will use that dropdown menu from now on. Could you give me an example of how to use a class module? I've never used one before.
 
Upvote 0
Thanks for the example code. I tried it out and I keep getting a "User-defined type not defined" on this line:

Code:
Dim TBs() as New TBClass

Class Module (Named: Analysis_Events)

<font face=Calibri><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">WithEvents</SPAN> TBGroup <SPAN style="color:#00007F">As</SPAN> MSForms.TextBox<br><SPAN style="color:#007F00">' Anytime a MouseDown event occurs to any TextBox, the sub fires</SPAN><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> TBGroup_MouseDown(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, _<br>    <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)<br>    <br>    <SPAN style="color:#00007F">With</SPAN> TBGroup<br>        .SetFocus<br>        .SelStart = 0<br>        .SelLength = Len(TBGroup.Text)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

UserForm Module (Named: Analysis)

<font face=Calibri><SPAN style="color:#00007F">Dim</SPAN> TBs() <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">New</SPAN> TBClass<br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> userform_Initialize()<br><br><SPAN style="color:#00007F">Dim</SPAN> TBCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> Ctrl <SPAN style="color:#00007F">As</SPAN> Control<br>    <br>    TBCount = 0<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> Ctrl <SPAN style="color:#00007F">In</SPAN> Analysis.Controls<br>        <SPAN style="color:#00007F">If</SPAN> TypeName(Ctrl) = "TextBox" <SPAN style="color:#00007F">Then</SPAN><br>            TBCount = TBCount + 1<br>            <SPAN style="color:#00007F">ReDim</SPAN> <SPAN style="color:#00007F">Preserve</SPAN> TBs(1 <SPAN style="color:#00007F">To</SPAN> TBCount)<br>            <SPAN style="color:#00007F">Set</SPAN> TBs(TBCount).TBGroup = Ctrl<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> Ctrl</FONT>
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

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