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. DaveM121 817.4K Reputation points Independent Advisor
    2019-05-13T16:11:54+00:00

    Hi Faycal

    Try replacing:

    TextBox1.SelStart = 0

    with

    TextBox1.SetFocus

    TextBox1.CurLine = 0

    0 comments No comments
  2. Andreas Killer 144K Reputation points Volunteer Moderator
    2019-05-13T16:23:52+00:00

    Set the EnterFieldBehavior property of the TextBox to fmEnterFieldBehaviorRecallSelection

    https://docs.microsoft.com/en-us/office/vba/lan...

    Andreas.

    0 comments No comments
  3. Anonymous
    2019-05-13T20:34:27+00:00

    Thanks Andreas,

    I will try.

    0 comments No comments
  4. Anonymous
    2019-05-13T20:35:02+00:00

    Thanks Dave,

    I will see if that works.

    Best

    Faycal

    0 comments No comments
  5. Anonymous
    2019-05-14T14:25:27+00:00

    Hi Dave,

    I m still having the same issue.

    Code doesn't seem to work or maybe I m not editing correctly.

    following all the code I have added to my app:

    Private WithEvents app As Application

    Private Sub app_WorkbookOpen(ByVal Wb As Workbook)

        For Each Sheet In ActiveWorkbook.Worksheets

            Sheet.Select

            Range("A1").Select

        Next

        app.Goto Wb.Worksheets(1).Range("A1")

        app.ScreenUpdating = True

    End Sub

    Private Sub Workbook_Open()

      Set app = Application

    End Sub

    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

        TextBox1.SetFocus

        TextBox1.CurLine = 0

    End Sub

    0 comments No comments