Excel VBA Digital Clock on UserForm: A Comprehensive Guide

In Excel VBA, we can create a Digital Clock on UserForm to show the current time. A digital clock on UserForm can be useful for various practical purposes. This clock will serve as a visual reference for the current time. Also, it can be used to track the duration of the current task. We will create a UserForm and use the relevant VBA code to create a digital clock.

This overview video shows how to initiate a real-time Excel VBA Digital Clock on UserForm.


Excel VBA Digital Clock on UserForm: Create with 4 Easy Steps

We will show you a step-by-step procedure to use Excel VBA to show the Digital Clock on UserForm. There are many things involved in this procedure. We will try to give you proper guidance to create a Digital Clock with UserForm.


Step 1: Create UserForm

To show the Digital Clock we need some UserForm.

  • First, go to the Developer tab and click on the Visual Basic.

Steps to open VBA window

The Microsoft Visual Basic for Applications window will open.

  • Now, click on the UserForm option from the Insert tab.

Steps for inserting UserForm for excel vba digital clock

UserForm1 is created. There is also a Toolbox which we will use to modify the UserForm.

Showing initial UserForm with Toolbox.


Step 2: Modify UserForm

We need to modify the UserForm. We want to insert two labels in the UserForm. You can do it very easily using the Toolbox.

In the following video, we have shown you how to insert labels. The first one is for showing “Date”, “Weekday”, “Year” etc. The second one is for showing the “Time”.

We also need to modify the properties of each label and the UserForm.

  • Click on the Label1 area in the UserForm and press F4.

The Properties window of Label1 will open up.

You can see that there are many properties that you can modify.

Showing properties window.

  • You can change the BackColor from the Palette.

Changing BackColor from Palette.

  • Also, you can change the Font. Select a suitable font combination.

Setting the font combination.

The properties of our Label1 are shown in the image below.

  • We deleted the Caption and the TextAlign is set to center.

Showing properties for Label1.

The properties of our Label2 are set as shown in the below image.

Showing properties for Label2.

And the properties of UserForm are shown in the following image.

  • The Caption is changed to Digital Clock.

Showing properties for UserForm1 for excel vba digital clock

And, in the end, our UserForm looks like this.

Showing the modified UserForm.


Step 3: Write VBA Codes in UserForm and Module

Now we have to assign VBA codes that will create the Digital Clock.

  • First, copy the following code to a new Module.
Public runclock As Double
Sub StartClock()
With UserForm1.Label1
.Caption = Format(Now, "dddd, mmmm dd, yyyy")
.AutoSize = True
End With

With UserForm1.Label2
.Caption = Format(Now, "h:mm:ss AM/PM")
.AutoSize = True
End With
runclock = Now + TimeSerial(0, 0, 1)
Application.OnTime runclock, "StartClock", , True
End Sub
Sub StopClock()
Application.OnTime runclock, "StartClock", , False
End Sub

VBA Code Breakdown

  • This code has two subroutines. The StartClock subroutine does the main work for creating Digital Clock.
  • With Label1, it sets the current day, month, and year as Label1 captions.
  • With Label2, it sets the current hour:minute:seconds and AM/PM as Label2 captions.
  • Then it creates a runclock to show time, utilizing VBA Now and TimeSerial The runclock will add 1 second and will keep showing time on UserForm.
  • The StopClock subroutine stops the clock when the Excel file is terminated.

excel vba code for digital clock on userform

Now we have to assign two Private Sub in the UserForm.

  • Double click on any part of the UserForm(Label1, Label2, UserForm area). The window for your code will open up.
  • The VBA suggests us with a click Event code. But we can ignore this and copy the following two codes in that region.
Private Sub UserForm_Initialize()
Call StartClock
End Sub
Private Sub UserForm_Terminate()
Call StopClock
End Sub

The first one calls subroutine StartClock when the UserForm is initialized (when the UserForm is run).

The second one calls StopClock when the UserForm is terminated.


Step 4: Run UserForm to Get Digital Clock

Now we have to run the UserForm.

  • Click anywhere on the UserForm to make it active.
  • Then press the Run button.

And we will have our Digital Clock running on the UserForm.

We can also modify this Excel file in such a way that when we open the file, only the Digital Clock will run.

For doing that,

  • Click on ThisWorkbook.

Opening ThisWorkbook window.

A window will open.

  • Copy the following code in this window.
Private Sub Workbook_Activate()
Application.Visible = False
UserForm1.Show
End Sub

excel vba code to show digital clock on userform only

And make sure you have saved the file properly. Then, close the Excel file.

Saving the modified Excel file.

  • Go to your file location and double-click to open the Excel file.

open Excel file to show only digital clock.

The Excel file will first open, and then a Digital Clock will continue to run. This time, you can only see the Digital Clock in UserForm, and you can not access the original Excel file.

To access the Excel file,

  • Close the Digital Clock and open the Excel file again.

Frequently Asked Questions

  • How can we change the Font size of the Digital Clock?

To change the font size of the digital clock, you can modify the Font property of the label control in the Properties window. First, select the label control on the UserForm and then go to the Properties window. In the Font section, you can change the font name, size, style, and color.

  • How can we customize the appearance of the label control in the Digital Clock?

You can customize the appearance of the label control on the digital clock. For this, you have to modify its properties in the Properties window. For example, you can change the font, size, color, and alignment of the label text. To access the Properties window, right-click on the label control in the UserForm and select Properties from the context menu.

  • How can we center the digital clock on the UserForm?

You can center the digital clock on the UserForm by using the properties of the label control. Select the label control on the UserForm and then go to the Properties window. In the Alignment section, change the TextAlign property to “center”. You can also adjust the position of the label control on the UserForm by dragging it to the desired location.


Things to Remember

  • The UserForm will contain the clock area. So, it is important to modify the UserForm with proper Caption and Font.
  • Make sure to save the UserForm and Excel workbook after making any changes.
  • You need to use proper code. Otherwise, the program could run into an infinite loop.

Download Practice Workbook

You can download and practice this workbook.


Conclusion

So, we have shown you how to create an Excel VBA Digital Clock on UserForm. We hope you find the content of this article useful and hope you can create a digital clock with Excel VBA. If you have further queries or suggestions mention them in the comment section. You can find similar articles on our website ExcelDemy.com.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Sourav Kundu
Sourav Kundu

Sourav Kundu, BSc, Naval Architecture & Marine Engineering, Bangladesh University of Engineering and Technology, is a dedicated technical content creator of the ExcelDemy project. He has a keen interest in Excel and he leverages his problem-solving skills to provide solutions on user interface with Excel. In his position as an Excel & VBA Content Developer at ExcelDemy, Sourav Kundu not only adeptly addresses challenging issues but also demonstrates enthusiasm and expertise in navigating complex situations. Apart from creating... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo