Full Version: Userform textbox highlighting text on setfocus
UtterAccess Discussion Forums > Microsoft® Office > Microsoft Excel
AnneT
Is there an equivalent to the VB textbox.selectall in Excel VBA? I want to have all the text in a userform textbox highlighted when focus is set back to the texbox after an input error. I have tried the selstart and sellength methods with no luck. Thanks
4fit
Give this a shot:
CODE
Me.TextBox1.SetFocus
Me.TextBox1.SelStart = 0
Me.TextBox1.SelLength = Len(Me.TextBox1.Text)
4fit
Actually, if this could happen with multiple text boxes, it would be best to create a Function and just call that function with the specified control when need be. Something like:
CODE
Function SelectText(frmTxt As Control)
    With frmTxt
        .SetFocus
        .SelStart = 0
        .SelLength = Len(frmTxt)
    End With
End Function


Then, you could just call this function whenever needed like :
CODE
Call SelectText(Me.Textbox1)


HTH
AnneT
Thank you so much. I was setting the sellength with the textbox.textlength fine. I just didn't use the selstart to set the insertion point, so nothing was getting selected. It was driving me crazy last night - knew it was something straight forward that I was missing.

Anne
4fit
Glad to help. wink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.