Excel VBA Highlight text in textbox

Hello Experts,

I have 2 issues to resolve.

I have a UserForm that is initialized.  I want the userform to show then go to textbox1 and highlight the text for editing. (The cursor defaults to the end of the text). I have that textbox populate from another worksheet.range but I want to set focus and highlight for easy editing.

Then next issue is that I have another text box on the same UserForm that populates from another worksheet.range and must be a numeric value.  I am using a Public Function IntegerValidation(ByVal MyTextBox As Control) to check it's contents - This works fine, but I want it to highlight the text.value to easily edit it correctly if the enduser enters a non-numeric value.

Thanks
mike637Asked:
Who is Participating?
 
SiddharthRoutCommented:
Here is the screencast :)

Sid
SiddharthRout-440008.flv
0
 
SiddharthRoutCommented:
Check out this sample. Please amend it for your actual file.

Sid

Code Used

Private Sub UserForm_Initialize()
    TextBox1.Text = "This Text is highlighted"
    With TextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub

Open in new window

Textbox-Sample.xls
0
 
mike637Author Commented:
Experts,

Here is the code I have in my UserForm, but it is not setting focus or highlighting the text for editing in the TextBox1 named Manager1 when the UserForm is initialized. What am I doing wrong???

I am using Sheets 3 - 12 for the UserForm and have the data on Sheet1

Private Sub UserForm_Initialize()
   
    Manager1.Text = Sheet1.Range("A20")
    With Manager1
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
   
End Sub
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
SiddharthRoutCommented:
Can I see your file?

Sid
0
 
mike637Author Commented:
As requested.
2011-Weekly-Call-In.xls
0
 
SiddharthRoutCommented:
I just tried it and it does highlight :)

Please see snapshot

Sid
Untitled.jpg
0
 
mike637Author Commented:

I saw that too.  However, when I go to the other worksheets - I do not always get the same result.

It is sort of hit or miss.

mike637
0
 
SiddharthRoutCommented:
Which worksheet you don't see it in?

Sid
0
 
SiddharthRoutCommented:
I checked every worksheet and it is working just fine.

Sid
0
 
mike637Author Commented:

I guess I am missing something.  When I go to February or any other month - it is not highlighted.

Did you close the workbook - reopen and try each worsheet??

Very confused.
0
 
SiddharthRoutCommented:
Yes I did :)

Sid
0
 
SiddharthRoutCommented:
Posting video in a moment.

Sid
0
 
mike637Author Commented:
Thanks.

I added Unload me on exit and then each time I opened it, it loaded fine.

Thanks again!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.