2
votes

I have an Excel user form with multiple multi-line text boxes that need scroll bars. When I click in a textbox to scroll, it starts at the bottom of the text. When this happened with just one textbox on the user form I used this:

 Userform1.TextBox1.SelStart = 0

and everything worked. If I try to use that on multiple text boxes in the same form, the scroll bar never appears for any of the boxes. Does anyone know how to fix this?

Update:

Found a quirk that my help narrow down the problem: with multiple textboxes, selstart=0 works on the first box, but then I need a much bigger number for the selstart of the next textbox. Example. The code below puts the scrollbar at the top of both textboxes. The form is shown through a double click on sheet 1 and the the values of the textboxes are create in the initialize sub.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

 UserForm1.Show

End Sub
--------------
Private Sub UserForm_Initialize()

 UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
 UserForm1.TextBox1.SelStart = 0

 UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
 UserForm1.TextBox2.SelStart = 200

End Sub

But I could only find that textbox2 had to start at 200 through guess and check. I dont know how to determine where that textbox should start.

1
Once you click into the textbox with no scrollbar and enter enough text (more than the size of the box) does the scrollbar appear ? - Robin Mackenzie
Not if I add the line of code above. Otherwise yes, all of the long text boxes appear and allow me to scroll. They just scroll starting from the bottom. - Katie
Katie I deleted my answer because you edited your question. Perhaps you could add more code into the question so people can understand how you are initialising the text boxes and other relevant detail. Understood you don't want to 'flood' the question with too much extra code but a little more rather than a little less would help at this point. - Robin Mackenzie
I have updated the code - Katie

1 Answers

3
votes

I had a breakthrough. If I use SetFocus then do selstart= 0 everything seems to work.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

 UserForm1.Show

End Sub

Private Sub UserForm_Initialize()

 UserForm1.TextBox1.Value = Sheets("Sheet1").Cells(1, 1).Value
 UserForm1.TextBox1.SetFocus
 UserForm1.TextBox1.SelStart = 0

 UserForm1.TextBox2.Value = Sheets("Sheet1").Cells(2, 1).Value
 UserForm1.TextBox2.SetFocus
 UserForm1.TextBox2.SelStart = 0

End Sub