Syhchronous scrolling of two userform texboxes

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
877
Office Version
  1. 365
Platform
  1. Windows
It's easy to use the scrollbar control to get two or more userform Listboxes to scroll down simultaneously
as below:
Code:
Private Sub ScrollBar1_Change()
If ScrollBar1.Value <= ListBox1.ListCount - 1 Then
    ListBox1.ListIndex = ScrollBar1.Value
    ListBox2.ListIndex = ScrollBar1.Value
End If
End Sub
How would I do this with 2 textboxes,, Textbox1 and Textbox2, side by side on a userform?
Individual vertical scrollbars allow this with each Textbox independently. But how
would you do it with ONE vertical scrollbar for both textboxes and having them
move down simultaneously ? Image below

Thanks for anyone's help.
cr
 

Attachments

  • SCROLLING TOGETHER.jpg
    SCROLLING TOGETHER.jpg
    211.7 KB · Views: 14

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
The code for textboxes is similar. You have to set focus to each TextBox to access its LineCount and CurLine properties.
VBA Code:
Private Sub ScrollBar1_Change()

    Dim tb As Variant
    
    For Each tb In Array(Me.TextBox1, Me.TextBox2)
        With tb
            .SetFocus
            If ScrollBar1.Value < .LineCount Then .CurLine = ScrollBar1.Value
            ScrollBar1.Value = Application.WorksheetFunction.Min(ScrollBar1.Value, .LineCount - 1)
        End With
    Next

End Sub
 
Upvote 0
Thanks John. Will give this a try.
cr
Hi John, wanted to get back to you for trying to help me. The scroll bar code doesn't work exactly as it should. All I did was copy and paste your code
It scrolls down once or twice then stops. I've included code below in the click event of Listbox1.. When your code sses this, it may get Excel confused. This may or may not be a reason. All this does is separate each row(on the sheet) with a space between for easier reading and studying on the userform textboxes.

Code:
Private Sub ListBox1_Click()
Dim n As Long
n = ListBox1.ListIndex
Application.EnableEvents = False
Application.ScreenUpdating = False
TextBox1.Value = ListBox1.List(n, 1) _
& vbCrLf _
& vbCrLf _
& ListBox1.List(n + 1, 1) _
& vbCrLf _
& vbCrLf _
& vbCrLf + ListBox1.List(n + 2, 1) _
& vbCrLf _
& vbCrLf + ListBox1.List(n + 3, 1) _
& vbCrLf + ListBox1.List(n + 4, 1) _
& vbCrLf _
& vbCrLf + ListBox1.List(n + 5, 1) _
& vbCrLf _
& vbCrLf + ListBox1.List(n + 7, 1) _
& vbCrLf + ListBox1.List(n + 8, 1) _
& vbCrLf _
& vbCrLf + ListBox1.List(n + 9, 1) _
& vbCrLf + ListBox1.List(n + 10, 1) _
End Sub
done 4x with 4 Textboxes. To work correctly, pressing the scrollbar control should advance 1 row for each Textbox, so the same verse(row)
will be visible in each textbox for each of 4 different translations. Again, thanks for all your help.

Not to overkill this, but this code in a NExt Button on the form does advance each row correctly. I'd prefer one vertical scrollbar, if that's doable:
Code:
Private Sub cmdNXTVERSE_Click()
Dim count As Integer
Dim n As Long
Dim lastrow As Long
On Error Resume Next
n = ANEWPARALLELVIEW.ListBox1.ListIndex
lastrow = Sheets("NEWRESULT").Cells(Rows.count, "B").End(xlUp).Offset(-1, 0).row
If ANEWPARALLELVIEW.ListBox1.ListIndex = lastrow Then
     MsgBox "At last row"
    Exit Sub
Else
    n = Me.ListBox1.ListCount - 1
    Select Case Me.ListBox1.ListIndex
    Case Is < n
        Me.ListBox1.ListIndex = Me.ListBox1.ListIndex + 1
     Case Else
        Me.ListBox1.ListIndex = 0
    End Select
End If
end Sub

Thanks again for all your help.
cr









cr
Kingwood, Tx
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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