0
votes

I am fairly new to VBA so any help would be greatly appreciated.

I have a UserForm that contains 3 labels and 3 text boxes. 2 of the text boxes (including the first one that is activated upon the form's activation) are empty. The third has 3 characters pre-filled into it. When the userform starts, I would like to fill in the first text box as normal then when I tab into the second text box, I would like the cursor to automatically go to the end of that 3 letters instead of highlighting the entire text box. I have looked around but cannot seem to find anything that can do this easily. Could you please let me know where exactly (which sub) to enter this code into as well?

My UserForm is called 'GetBIInfo2' and my text box is called 'NBIDText'

Thanks!

3
Look into the textbox control's SelStart property.Mathieu Guindon

3 Answers

2
votes
  1. Select the textbox.
  2. Go to property (F4).
  3. Scroll till you find EnterFieldBehavior property.
  4. Change it to 1 - FmEnterFieldBehaviorRecallSelection.
2
votes

The direct approach is via the textbox'es .SelStart property; possibly you might call it via a sub procedure like this:

Sub fillAnyTextBox(myTextBox As MSForms.TextBox, ByVal firstCharacters As String)
With myTextBox
    .Value = firstCharacters
    .SetFocus
    .SelStart = Len(firstCharacters)
    .SelLength = Len(.Text) - Len(firstCharacters)
End With
End Sub

0
votes

A simple alternative would be to make another text box to hold the first three characters and have the user enter the following characters into a separate textbox. Then you can write code that will take the contents of both and append them into a single string like this:

Dim finalString As String

finalString = TextBox1.Value & TextBox2.Value