Share via

Insert Point in Textbox

Anonymous
2016-04-30T05:35:39+00:00

Hi,

I have a textbox which has some default text when the program starts.

When the textbox gets focus, is it possible to get the insert point to be located at a particular point in the text? 

Best Regards to all

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2016-04-30T18:17:10+00:00

    If you know what text will be in the text box when the userform appears (because your code is creating that text), you can assign a specific number to the text box's .SelStart property. The correct number is the number of characters to the left of the insertion point.

    Private Sub UserForm_Initialize()

        With TextBox1

            .Text = "This is the text when the userform starts."

            .SelStart = 12  'before the t in "text"

            .SetFocus

        End With

    End Sub

    If you want one or more characters to be selected instead of just an insertion point, also set the .SelLength property's value.

    If you don't know what text will be there (for example, you're copying in text from the document body), you can use the InStr function on the box's .Text property to search the string for a particular substring. To put the insertion point at the end of the text, use

       .SelStart = Len(.Text)

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-04-30T08:25:58+00:00

    If you want to edit the text in the text box, I think that the only thing you can do is to use code such as

    .TextBox1.Text = Left(.Textbox1.Text, 10) & " some new text " & mid(.Textbox1.Text, 11)

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-04-30T07:04:10+00:00

    Thanks for your quick response.  I'm sorry I didn't make myself clear. I have a macro that has userform with a textbox in it. The textbox has text in it when it gets focus.

    The idea is how to get the insertion point to a particular point in the text using vba.

    Thanks

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2016-04-30T06:25:06+00:00

    Do you have to use a textbox?  It is much easier to work with tables.

    Was this answer helpful?

    0 comments No comments