Updating a label of a form (by use of timer)

Wil Moosa

Well-known Member
Joined
Aug 11, 2002
Messages
893
Every second the time is changed in a specific cell. A label of a specific form is linked to this cell with the code:

Label3 = Sheets("Dagteller").Range("A50").Text

The result is that the label only shows the time as it was when the form was opened (it does no updating, counting or whatever). How do I get the changing time changing in the label too?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Here's the code I used. It works fine:

Code:
Sub time()
Application.OnTime Now + TimeValue("00:00:01"), "my_Procedure"
End Sub

Sub my_procedure()
UserForm1.Show
Range("A1") = Now()
UserForm1.Label1 = Format(Range("a1"), "hh:mm:ss")
time
End Sub
 
Upvote 0
I played a bit with the above code but did not get it functional the way it should. What did happen is that the form turns up, shows a static time and when the form is closed it comes back immediately with a new time.

In response to your question I add my code:

Dim SchedRecalc As Date

Sub SetTime()
SchedRecalc = Now + TimeValue("00:00:01")
Application.OnTime SchedRecalc, "Recalc"

End Sub

Sub Recalc()
Range("A50").Value = time

Call SetTime
End Sub

Sub Disable()
On Error Resume Next
Application.OnTime EarliestTime:=SchedRecalc, Procedure:="Recalc", Schedule:=False
End Sub

The following code is placed in the sheet:

Private Sub UserForm_Initialize()
Label1 = Sheets("Dagteller").Range("A48").Text
Label2 = Sheets("Dagteller").Range("A49").Text
Label3 = Sheets("Dagteller").Range("A50").Text
End Sub
 
Upvote 0
tbardoni said:
Select your form and in the Properties select ShowModal=False.

Well, this made the difference... The only problem is that I can not cancel the form!
 
Upvote 0
I have been playing with your code and other submissions. With the form set to showmodal false, I placed the following snippet of code in the form.

Code:
Private Sub UserForm_Activate()
    Call UpdateLabel
End Sub

Private Sub UserForm_Deactivate()
    UserForm1.Hide
End Sub

The second routine will of course close the form when the X is clicked

The rest of the code just updates the label of the form that I used.

Code:
Sub UpdateLabel()
    UserForm1.Label1.Caption = Range("A1").Text
End Sub

Sub StartIt() 'sole function to start the ball rolling
    UserForm1.Show
    Call Recalc
End Sub

Sub SetTime()
    Dim SchedRecalc As Date
    
    SchedRecalc = Now + TimeValue("00:00:01")
    Application.OnTime SchedRecalc, "Recalc"
End Sub

Sub Recalc()
    Range("A1").Value = Time
    Call UpdateLabel
    Call SetTime
End Sub

HTH :)
 
Upvote 0
Modifications to the code above to stop the timer as well as the form.

Code:
Private Sub UserForm_Activate()
    Call Recalc
End Sub
Private Sub UserForm_Deactivate()
    UserForm1.Hide
End Sub


Code:
Sub StartIt()
    UserForm1.Show
    Recalc
End Sub

Sub Recalc()
    Dim SchedRecalc As Date
    
    Range("A1").Value = Time
    UserForm1.Label1.Caption = Range("A1").Text
    If UserForm1.Visible = True Then '
        SchedRecalc = Now + TimeValue("00:00:01")
        Application.OnTime SchedRecalc, "Recalc"
    End If
End Sub
[/code]
 
Upvote 0

Forum statistics

Threads
1,221,691
Messages
6,161,322
Members
451,696
Latest member
Senthil Murugan

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