Setting focus to end of 'notes' textbox

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
416
Office Version
  1. 365
Platform
  1. Windows
Hi all

I have 2 userforms each containing the same text box 'txtNotes' If some data is added to txtNotes on userform1 it is automatically copied over to txtNotes on userform2.

Could anyone let me know if there is a way to set the cursor to the last character plus a space once the txtNotes gets the focus in Userform2 ?

Many thanks

Paul
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
We need to see all the code for UserForm2. In particular when is UserForm2.txtNotes updated relative to when UserForm2.Show is called?

If the text is copied prior to Show then add this to the code for UserForm2:
VBA Code:
Private Sub UserForm_Activate()

   Me.txtNotes =Me.txtNotes & " "
   Me.txtNotes.SetFocus

End Sub
But that is just an example. To give you code that will work for your specific situation, we need to see your existing code to understand what you are doing.
 
Upvote 0
Hi thanks so much for the reply. The relevent code I'm using is below ( I've shortened it for ease of reading and please bear in mind this is just some test code before adding it to the live worksheet).

VBA Code:
Private Sub UserForm_Initialize()
  
textboxNotes.Value = UserForm1.textboxNotes.Value

End Sub

VBA Code:
Private Sub textboxMiscExpDesc_Exit(ByVal Cancel As MSForms.ReturnBoolean)

        textboxMiscExpDesc.BackColor = RGB(255, 255, 255)
        textboxNotes.SetFocus

End Sub

This all works fine for what I need but I thought it would be better is when the 'Notes' gets the focus for the cursor to go to the end of any text already there. Although saying that there may be a numerical value as the last character if that makes any difference to the code you kindly posted.

Thanks again Paul
 
Upvote 0
I'm not clear on your question. You already have code that will set focus, and in my testing this puts the cursor at the end of any text already there. So your code should do what you want iwth no changes. Not sure why you asked for "plus a space."

How are your current code's results different from what you want?
 
Upvote 0
Ok so when the textbox gets the focus all the text already there is highlighted this mean if a user presses a key then the highlighted text is deleted.

re the 'plus a space' bit - this is just me trying to make it obvious to a user where any new text should start e.g.
'Enter NEW Text Here' (space) then the cursor

hopefully that makes more sense

Thanks again Paul
 
Upvote 0
I use this code for the UserForm

VBA Code:
Private Sub UserForm_Activate()

   Me.TextBox1 = "ABCDEFG"
   Me.TextBox2 = Me.TextBox1
   
   Me.TextBox2.SetFocus

End Sub

and when I Show the form I get this. The cursor appears at the end of the text for TextBox2 and the text already there is not highlighted. So I can't reproduce what you are describing.

1706216774429.png
 
Upvote 0
Hi Jeff (sorry just saw you name at the end of your post )
I completely understand what the last code you posted does but for some reason mine still highlights all the text that is already in the textbox - I'm thinking perhaps it's because the textbox already has some text/value in it when the focus is set.

It's not a real big issue in reality although it would be nice to try and eliminate any possible user input mistakes.

Thanks for your replies though - appreciated

Paul
 
Upvote 0
Maybe you should use the SelStart Property as follows (based on 6StringJazzer answer in post#6)
VBA Code:
Private Sub UserForm_Activate()
    Me.TextBox1 = "ABCDEFG"
    Me.TextBox2 = Me.TextBox1 & " "
    Me.TextBox2.SelStart = Len(Me.TextBox2)
    Me.TextBox2.SetFocus
End Sub
:
 
Upvote 1

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top