Katie
Katie

Reputation: 308

VBA Userform Text Box Scroll Start at top

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.

Upvotes: 2

Views: 11392

Answers (1)

Katie
Katie

Reputation: 308

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

Upvotes: 3

Related Questions