VBA ScrollBar on Frame problem

ttt123

Board Regular
Joined
May 31, 2006
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hi, I am having a problem with ScrollBars as part of a frame object in that objects in the frame seem to be calling the scroll event when they get focus.

I have a bunch of dynamically created controls on my userform that exist within a Frame.
But when certain controls are clicked within the frame, the frame's scroll event seems to get called automatically. I only want the scroll event to be called when the user actually drags the bar themselves and not when certain controls are clicked.

I have created a simple example to illustrate this problem (just paste the code below into a new userform):
Private Sub UserForm_Initialize()

With Me
.Width = 500
.Height = 300
End With

Dim Frame As MSForms.Frame
Set Frame = Me.Controls.Add("Forms.Frame.1")
With Frame
.Left = 0
.Top = 0
.Width = Me.Width - 10
.Height = Me.Height - 10
.ScrollBars = fmScrollBarsVertical
End With

Dim i As Long
Dim TextBox As MSForms.TextBox
For i = 1 To 10
Set TextBox = Frame.Controls.Add("Forms.TextBox.1")
With TextBox
.Left = 5
.Top = 45 * (i - 1)
.Width = 400
.Height = 45
.Text = i
End With
Next i

Dim max As Long, ctrl As MSForms.Control
max = 0
For Each ctrl In Frame.Controls
If ctrl.Top + ctrl.Height > max Then
max = ctrl.Top + ctrl.Height
End If
Next ctrl
Frame.ScrollHeight = max

End Sub

If you scroll all of the way down to the bottom and then click the textbox numbered 4, you'll see the scrollbar move.

Of course these controls could be created manually and if you put a breakpoint on the Frame's scroll event, you'll see that it is called when textbox 4 is clicked when scrolled all the way down to the bottom.

Is there any way around this?
Thanks in advance.
Taylour
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,978
Latest member
rrauni

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