How to Use VBA Code for Creating Digital Clock in Excel

Sometimes you find yourself in a difficult situation because your deadline is approaching. So to use your time more efficiently, you can set a digital clock along with your Excel worksheet. How will you do that? Well, no problem at all. Here we will provide a VBA code for a digital clock in Excel and later we will demonstrate how to use the VBA code so that you can create one of your own.


How to Use VBA Code for Making a Digital Clock in Excel: 2 Steps

Now, look at the screenshot we have provided below. We will demonstrate in the following section how to create this digital image with adequate aesthetic features. So let’s begin. Here we will provide a VBA code for a digital clock in Excel and later we will demonstrate how to use the VBA code so that you can create one of your own.

How to Use VBA Code for Creating Digital Clock in Excel

Here, we have used the Microsoft Excel 365 version; you may use any other version according to your convenience.


Step 1: Employ VBA Code in Module

The very first step of this process is to incorporate the VBA code into our Excel file. We will apply the VBA TimeValue function within our VBA code so that the code can take data from the built-in function to keep the time updated.

  • Press Alt + F11 to open your Microsoft Visual Basic.
  • Then press Insert > Module to open a blank module.

Employ VBA Code in Module

  • Now, write the following VBA code in your Module1.
Dim DigitalClock As Date
Sub MakingClock()
With Sheet1.Range("B4")
.Value = Format(Time, "hh:mm:ss AM/PM")
End With
Call SetTime
End Sub
Sub SetTime()
DigitalClock = Now + TimeValue("00:00:01")
Application.OnTime DigitalClock, "MakingClock"
End Sub
Sub Disable()
On Error Resume Next
Application.OnTime EarliestTime:=DigitalClock, Procedure:="MakingClock", Schedule:=False
End Sub

Employ VBA Code in Module

  • Now, we will explain how the given VBA code works.

 

Code Breakdown:

The code is divided into three steps.

  • In the first portion, a variable named Digitalclock has been created. Later a sub-function MakingClock was assigned to the B4
  • In the second portion, sub-function SetTime will keep the addition of TimeValue and present value (from when the code will be run) in Digitalclock
  • In case the user wants to disable his clock, another sub-function Disable() has been created in the last part of the code.

  • Now, press F5 to run your VBA Code. Subsequently, the macro window will appear.
  • Click on MakingClock > Run.

  • Now see the output of the code, as given below.

Read More: How to Make a Running Clock in Excel


Step 2: Format Alignment and Color Grading

So far, so good. Now we will make an arrangement to give our clock a better look. Not to mention how important it is to combine art and beauty in our day-to-day lives. Here we make a typical choice of color and presentation. However, you can use anything according to your individual preference.

  • First of all, select the B4:E7 cells, then press Merge & Center.

  • Following this, move the clock in center alignment by clicking the feature as shown in box 1.
  • To make the border, select All Borders from the borders option.

  • After that, change the font to Digital 7.

  • Increase the font size to 48. You may choose the font size according to your own preference.

How to Use VBA Code for Creating Digital Clock in Excel

  • Now we will change the Font Color as well as the background.
  • To do so, select the Font Color and Fill Color features as depicted below.

How to Use VBA Code for Creating Digital Clock in Excel

  • So, the final result will appear below. Looks cool, right?

Read More: How to Create Analog Clock in Excel 


Things to Remember

  • To stop your clock, select the Disable option from the Macro You can find this option by going to Developer > Macros > Disable > Run.

Things to Remember How to Use VBA Code for Creating Digital Clock in Excel


Practice Section

We have provided a Practice section on the right side of the sheet so that you can practice yourself.

Do it yourself How to Use VBA Code for Creating Digital Clock in Excel


Download Practice Workbook


Conclusion

In this article, we have discussed the application VBA code for creating the digital clock in Excel. It will be fun, we can tell you when you do it yourself. However, if you have any queries, feel free to comment below, and we will get back to you soon.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mohammad Shah Miran
Mohammad Shah Miran

Mohammad Shah Miran has a professional background spanning over a year at Softeko. Initially starting as an Excel and VBA writer, he authored more than 50 articles for the ExcelDemy project. Currently, Miran is engaged in the LinuxSimply project as a Linux content developer, completed over 40 articles. His analytical approach extends across various domains, including Excel, VBA, Bash scripting, Linux, data analysis, and Python programming. In his leisure time, Miran enjoys watching movies and series or listening... Read Full Bio

2 Comments
  1. Dear Sir

    I have to convert the MS formula into VBA code can you help me?

    =IF(ISBLANK(E13),””,MAX(D$12:$D12)+1)

    Thanks

    • Reply Avatar photo
      Mohammad Shah Miran Dec 29, 2022 at 1:10 PM

      Hello Imran,
      Thanks for the query. What I understand from your comment is that you want to incorporate a VBA code for the formula instead of going to the formula editor. Here are my two cents which might help you in this regard. Look at the dataset attached below.

      After pressing the ALT+F11 short key to open your VBA window, paste the following code in the Module box.

      Save & Close your VBA window. Then, press F8 to open the Macro dialog box and click on Options.

      Create a shortcut key to make the process fast, Crtl+W for instance.

      Now see the output as given below.

      Hope you have got your answer. Good Luck!

      Regards
      Miran
      Excel & VBA Content Developer

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo