How to select the contents of a textbox once it is activated?

前端 未结 10 2240
灰色年华
灰色年华 2020-12-06 05:16

I have this simple Userform, where I only have TextBox1 and TextBox2. I enter some text in both of them. Assume the focus is on (the cursor is in)

10条回答
  •  情书的邮戳
    2020-12-06 05:46

    This is somewhat an enhancement of what @vacip posted. The benefit you get is that you don't need to add a separate method in the Module for each new textbox.

    The following code in your User Form:

    '===== User Form Code ========
    
    Option Explicit
    
    Private Sub TextBox1_Enter()
        OnTextBoxEnter
    End Sub
    
    Private Sub TextBox2_Enter()
       OnTextBoxEnter
    End Sub
    
    Private Sub TextBox3_Enter()
       OnTextBoxEnter
    End Sub
    

    The following code goes in a Module:

    '===== Module Code ========
    
    Sub SelectAllText()
        SendKeys "{HOME}+{END}", True
    End Sub
    
    Sub OnTextBoxEnter()
       Application.OnTime Now + 0.00001, "SelectAllText", Now + 0.00002
    End Sub
    

提交回复
热议问题