Поместите DisplayCurrentTime
внутри модуля UserForm. Затем вы можете вызвать сабвуфер следующим образом
Private Sub UserForm_Initialize()
Book2.UserForm1.DisplayCurrentTime
End Sub
Private Sub DisplayCurrentTime()
Dim nextSecond As Date
nextSecond = DateAdd("s", 1, Now())
Label8.Caption = Time()
Application.OnTime _
Procedure:="UserForm1.DisplayCurrentTime", _
EarliestTime:=nextSecond, _
LatestTime:=nextSecond + 1
End Sub
Кроме того, вы можете поместить DisplayCurrentTime
в другой модуль. Однако вы заполняете элементы управления в UF, поэтому вам придется указать UF. Процедура ниже подходит для любой пользовательской формы.
Private Sub UserForm_Initialize()
DisplayCurrentTime Me, "Label8"
End Sub
Sub ниже в другом модуле
Sub DisplayCurrentTime(UF As UserForm, Cntrl_Name As String)
Dim nextSecond As Date
nextSecond = DateAdd("s", 1, Now())
UF.Controls.Item(Cntrl_Name).Caption = Time()
'If the control you want to paste the date to is Label8 for every UF, use the commented out code instead..
'..and remove ", Cntrl_Name As String" from this sub, and...
'..remove the specified control in the UserForm_Initialize sub
'UF.Controls.Item("Label8").Caption = Time()
'Module1 is true in my case, edit to suit your project
Application.OnTime _
Procedure:="Module1.DisplayCurrentTime", _
EarliestTime:=nextSecond, _
LatestTime:=nextSecond + 1
End Sub