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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
SiddharthRoutCommented:
Can I see your file?

Sid
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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
SiddharthRoutCommented:
Here is the screencast :)

Sid
SiddharthRout-440008.flv
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mike637Author Commented:
Thanks.

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

Thanks again!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.