- 1) Although the clock appears to be working when you open the workbook, there's a hidden error somewhere. You can tell this because if you go to VBA editor, the code is frozen, can't be edited until you reset it. Once you reset, of course, the clock goes away.
2) If I do not havein the form's initialize event, the clock opens blank, and doesn't show anything until "prompted" by calling the "Clock" sub.Code: Select all
ClockTextBox = Format(Now, "mm-dd-yy, h:mm:ss AM/PM")
3) If I attempt to call the Clock sub directly from UserForm_Initialize, either the whole thing freezes, or I get an error message saying the macro cannot be used, depending on exactly how I go about it. However, I can summon it elsewhere and it works—except for the hidden error noted in (1), above.
4) My guess is the hidden error is related to the "Loop" command—but how does one fix that? I've tried "While" and "Until" conditions, but can't find a way to make them work.
5) I tried adding a MsgBox that might tell me what error was occurring—but got nothing
a clock in a userform
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
a clock in a userform
Once again, this is primarily for educational purposes. I have no immediate need for this to work; I just want to understand the principles involved
You do not have the required permissions to view the files attached to this post.
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- Administrator
- Posts: 78788
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: a clock in a userform
Updating the text box in the after Update event of the text box causes the event to call itself over and over again, which will eventually freeze Excel. It's better to use Application.OnTime. See the attached version.
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: a clock in a userform
I suspected the endless loop was the problem. But I guess I'll have to study your code side by side with mine to understand why yours doesn't do that.HansV wrote:Updating the text box in the after Update event of the text box causes the event to call itself over and over again, which will eventually freeze Excel. It's better to use Application.OnTime.
Once again, my thanks!
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- Administrator
- Posts: 78788
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: a clock in a userform
Basically, Clock uses Application.OnTime to run itself again after one second.
The rest is mostly to ensure that the code stops gracefully when the form is closed and/or the workbook is closed.
The rest is mostly to ensure that the code stops gracefully when the form is closed and/or the workbook is closed.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: a clock in a userform
Ahh. So with the loop, it was —looping— continuously, but with OnTime, it's stopping/restarting every second. And if I delete "ss", I could have it restart every 60 seconds. Would that make any appreciable difference in processor or application load?HansV wrote:Basically, Clock uses Application.OnTime to run itself again after one second.. . .
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- 4StarLounger
- Posts: 596
- Joined: 14 Nov 2012, 16:06
Re: a clock in a userform
You'd better replace all 'select' and 'Activate' for educational purposes.
-
- Administrator
- Posts: 78788
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: a clock in a userform
If you want to update the clock every minute, you can use DateAdd("s", 60, Now) or DateAdd("n", 1, Now) - for minutes, we use "n" to avoid confusion with "m" for months.
Updating every minute will make Excel respond much better than if you update every second.
Updating every minute will make Excel respond much better than if you update every second.
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: a clock in a userform
snb wrote:You'd better replace all 'select' and 'Activate' for educational purposes.
![Smile :smile:](./images/smilies/smile.gif)
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)
-
- 4StarLounger
- Posts: 475
- Joined: 21 Feb 2020, 21:27
- Location: Vermont (USA)
Re: a clock in a userform
Great!HansV wrote:If you want to update the clock every minute, you can use DateAdd("s", 60, Now) or DateAdd("n", 1, Now) - for minutes, we use "n" to avoid confusion with "m" for months.
Updating every minute will make Excel respond much better than if you update every second.
—Nick
I’m only an egg (but hard-boiled)
I’m only an egg (but hard-boiled)