Getting my text box scrollbar in VBA to stay at the top when clikc inside text box.

Anonymous
2019-05-13T16:08:05+00:00

I have  a textbox with a verticals scrollbar.

I want the scroll bar to remain at the top at start.

So that when users click inside the text box, it doesn't scroll to the bottom of the text, which is what is currently happening.

'

I found this online but don't know how to implement:

In the UserForm's Initialize event, set the starting point of the cursor to TextBox's start using .SelStart

For example.

Private Sub UserForm_Initialize()
    Dim sSample As String
    Dim i As Long

    For i = 1 To 10
        sSample = sSample & "Blah Blah" & i & vbNewLine
    Next i

    TextBox1.Text = sSample

    '~~> Set to starting point
    TextBox1.SelStart = 0
End Sub

Not sure this works or where to find the Initialize event...

If someone can help,

Thanks,

Faycal

Microsoft 365 and Office | Excel | 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
{count} votes

10 answers

Sort by: Most helpful
  1. Anonymous
    2019-05-14T14:25:58+00:00

    Thanks Andreas,

    I made the change but doesn't seem to work either.

    0 comments No comments
  2. Andreas Killer 144K Reputation points Volunteer Moderator
    2019-05-14T15:10:02+00:00

    Make a new Excel file

    Open the VBA editor

    Insert a Userform

    Add 2 Textboxes

    Paste in the code below

    Run the form

    Works for me.

    Andreas.

    Private Sub UserForm_Initialize()
      Dim sSample As String
      Dim i As Long
    
      For i = 1 To 10
        sSample = sSample & "Blah Blah" & i & vbNewLine
      Next i
    
      With TextBox2
        .Text = sSample
        .SelStart = 0
        .MultiLine = True
        .ScrollBars = fmScrollBarsVertical
        .EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection
        .SetFocus
        .SelStart = 0
      End With
      With TextBox1
        .Text = sSample
        .MultiLine = True
        .ScrollBars = fmScrollBarsVertical
        .EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection
        .SetFocus
        .SelStart = 0
      End With
    End Sub
    
    0 comments No comments
  3. Anonymous
    2019-05-21T13:22:49+00:00

    Thanks Andreas,

    I will try that today.

    Best,

    Faycal

    0 comments No comments
  4. Anonymous
    2019-05-24T15:04:56+00:00

    I am still trying to get my textbox to scroll to top when I initially click inside the textbox.

    I have the following code ,

    It still doesn't work,

    When I use the .setFocus it comes up with an error in the code saying "Object doesn't support this property or method".

    Private Sub UserForm_Initialize()

    TextBox1.SetFocus

    TextBox1.SelStart = 0

    End Sub

    If someone can help troubleshoot.

    Thanks,

    Faycal

    0 comments No comments
  5. Anonymous
    2019-05-24T15:34:18+00:00

    Faycal,

    I think you have to use the On_Enter event:

    Private Sub TextBox1_Enter()

        Me.TextBox1.SelStart = 0

    End Sub

    Then the scrollbar doesn't display until going to lines initial outside the textbox.

    Jan

    0 comments No comments