• SetFocus to select text (VBA Excel 2003)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » SetFocus to select text (VBA Excel 2003)

    Author
    Topic
    #437524

    I’m trying to resolve the difference between the operation of the SetFocus method in Access and in Excel. In Access, issuing a SetFocus method on a form textbox will position the cursor at the beginning of any text already in that box and select (or highlight) that text. This way the next and subsequent keys pressed will automatically replace those that were selected. In Excel VBA this does not seem to happen. Instead, SetFocus on a textbox control will simply position the cursor at the end of any text already in that box selecting nothing. Highlighting to replace any text has to be down manually.

    Is there any way in Excel VBA code to select (or highlight) any text already in a text box when bringing it into focus?

    As always, thank you for your time,

    John Littell

    Viewing 0 reply threads
    Author
    Replies
    • #1040351

      The following code will set focus to TextBox1 and select the entire contents of the text box:

      With Me.TextBox1
      .SetFocus
      .SelStart = 0
      .SelLength = Len(.Text)
      End With

      • #1040385

        Hans,
        Two new methods I’ve never seen before. This is really neat.

        As always, thank you for your knowledge and time,

        John

    Viewing 0 reply threads
    Reply To: SetFocus to select text (VBA Excel 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: