ScrollBar Control, Scroll Event Example

The following example demonstrates the stand-alone ScrollBar and reports the change in its value as the user moves the scroll box. The user can move the scroll box by clicking on either arrow at the ends of the control, by clicking in the region between scroll box and either arrow, or by dragging the scroll box. When the user drags the scroll box, the Scroll event displays a message indicating that the user scrolled to obtain the new value.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains:

Dim ScrollSaved As Integer   
'Previous ScrollBar setting

Private Sub UserForm_Initialize()
    ScrollBar1.Min = -225
    ScrollBar1.Max = 289
    ScrollBar1.Value = 0

    Label1.Caption = "-225  -----Widgets-----   289"
    Label1.AutoSize = True
    
    Label2.Caption = ""
End Sub

Private Sub ScrollBar1_Change()
    Label2.Caption = " Widget Changes " _
        & (ScrollSaved - ScrollBar1.Value)
End Sub

Private Sub ScrollBar1_Exit(ByVal Cancel as MSForms.ReturnBoolean)
    Label2.Caption = " Widget Changes " _
        & (ScrollSaved - ScrollBar1.Value)
    ScrollSaved = ScrollBar1.Value
End Sub

Private Sub ScrollBar1_Scroll()
    Label2.Caption = (ScrollSaved – ScrollBar1 _
        .Value) & " Widget Changes by Scrolling"
End Sub