[VBA] How to Display Long Scrollable Text in a UserForm TextBox (Read-Only)

In VBA UserForms, there are times when you need to display long text—such as Terms of Service, application help, or detailed notes—that the user should read but not edit.

In such cases, using a multi-line TextBox as a “read-only display area” is very convenient.

This article explains the necessary property settings to display long, read-only text with a scroll bar and ensures it always starts displaying from the top.

目次

Sample Code: Settings for Long Text Display

The best place to configure these settings is within the UserForm_Initialize event, which runs just before the form appears.

In this example, we populate a TextBox named TermsTextBox (placed on AgreementForm) with a prepared long text string.

UserForm Code

' Event executed when the form is initialized
Private Sub UserForm_Initialize()
    Dim termsText As String
    
    ' Prepare long text (In a real app, loading this from a sheet or file is better)
    termsText = "Article 1 (Purpose)" & vbCrLf
    termsText = termsText & "This Terms of Service is..." & vbCrLf
    termsText = termsText & "(... long text continues ...)" & vbCrLf
    termsText = termsText & "End."

    With Me.TermsTextBox
        ' --- Display Settings ---
        .MultiLine = True      ' Enable multiple lines
        .WordWrap = True       ' Wrap text automatically at the right edge
        .ScrollBars = fmScrollBarsVertical ' Show vertical scroll bar
        
        ' --- Editing/Selection Settings ---
        .Locked = True         ' Prevent user from editing the text
        .EnterKeyBehavior = True ' Allow Enter key (Optional if Locked=True)
        
        ' --- Content and Position Settings ---
        .Text = termsText      ' Set the text content
        
        ' Use .SetFocus to bring focus to the TextBox
        .SetFocus
        ' Use .CurLine property to set the display line to the top (Line 0)
        .CurLine = 0
    End With

End Sub

Explanation of the Code (Properties)

.MultiLine, .WordWrap, .ScrollBars

These are the fundamental settings for displaying long text.

  • .MultiLine = True: Enables the TextBox to display multiple lines.
  • .WordWrap = True: Automatically wraps text to the next line when it reaches the right edge of the control.
  • .ScrollBars = fmScrollBarsVertical: Displays a vertical scroll bar if the text content exceeds the height of the TextBox.

.Locked = True

Setting this property to True makes the TextBox read-only. Users cannot edit or delete the content. This effectively turns the input field into a “display area.”

.SetFocus and .CurLine

When you assign long text to a TextBox, it often defaults to showing the bottom (end) of the text when the form loads. To prevent this and ensure the user sees the beginning of the document, we use these two commands:

  1. .SetFocus: First, we programmatically set the focus to the target TextBox.
  2. .CurLine = 0: Next, we set the .CurLine property to 0. This property represents the line number where the cursor is located. Setting it to 0 moves the cursor (and the view) to the very first line.

By executing these in order, the scroll position is forced to the top when the form appears, ensuring a natural reading experience.

Summary

In this article, we covered how to configure a TextBox to serve as a read-only display area for long documents.

  1. Use .Locked = True to disable editing.
  2. Use .ScrollBars to allow scrolling.
  3. Combine .SetFocus and .CurLine = 0 to ensure the text starts at the top.

By combining these techniques, you can smartly implement features like “Terms of Service Agreement” or “Operation Manuals” directly within your UserForms.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次