Since the slowdown loop (For i=1 To 5M) heats up to the red the processor and such a loop on different computers will cause scrolling at different speeds I propose minor changes:
Instead of
For i = 1 To 5000000
i = i + 1
Next
type
and at the beginning of Module1 insert the API function declaration:
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'For 64-Bit versions of Excel
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'For 32-Bit versions of Excel
#End If
You can control the scrolling speed by changing the Sleep value.
Below is a dynamic version of this task.
Full code in the UserForm module:
Option Explicit
Dim blnQuit As Boolean
Private Sub UserForm_Initialize()
Dim i As Long
Dim strTxt As Variant
With Sheet1
Me.Label1.Caption = .Range("B4").Value
'It is assumed that at least two cells are filled (E9 and E10)
For i = 9 To .Range("E9").End(xlDown).Row
strTxt = strTxt & (.Cells(i, "E").Value & String(2, vbLf))
Next i
End With
strTxt = Left(strTxt, Len(strTxt) - 2)
With Me.Label2
.Width = Me.InsideWidth
.Left = 0
.Caption = strTxt
.AutoSize = True
.AutoSize = False
'centered horizontally
.Left = (Me.Width - .Width) / 2
End With
End Sub
Private Sub UserForm_Activate()
Dim snH As Single
Dim snEnd As Single
Dim snStart As Single
snEnd = -Me.Label2.Height
snStart = Me.InsideHeight
snH = snStart
Me.Label2.Top = snH
Do While blnQuit = False
If snH <= snEnd Then
snH = snStart
End If
snH = snH - 0.75
Me.Label2.Top = snH
Sleep 1
DoEvents
Loop
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
blnQuit = True
End Sub
Artik
Bookmarks