Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SetFocus to select text (VBA Excel 2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: SetFocus to select text (VBA Excel 2003)

    The following code will set focus to TextBox1 and select the entire contents of the text box:
    <code>
    With Me.TextBox1
    .SetFocus
    .SelStart = 0
    .SelLength = Len(.Text)
    End With</code>

  3. #3
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: SetFocus to select text (VBA Excel 2003)

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

    As always, thank you for your knowledge and time,

    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •