Link to home
Start Free TrialLog in
Avatar of mike637
mike637Flag for United States of America

asked on

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
Avatar of SiddharthRout
SiddharthRout
Flag of India image

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
Avatar of mike637

ASKER

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

Sid
Avatar of mike637

ASKER

As requested.
2011-Weekly-Call-In.xls
I just tried it and it does highlight :)

Please see snapshot

Sid
Untitled.jpg
Avatar of mike637

ASKER


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
Which worksheet you don't see it in?

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

Sid
Avatar of mike637

ASKER


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.
Yes I did :)

Sid
Posting video in a moment.

Sid
ASKER CERTIFIED SOLUTION
Avatar of SiddharthRout
SiddharthRout
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mike637

ASKER

Thanks.

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

Thanks again!!